all numerical codes 2

PHOTO EMBED

Mon Nov 06 2023 01:22:35 GMT+0000 (Coordinated Universal Time)

Saved by @jin_mori

// bisection false position newton rhapson

#include <bits/stdc++.h>
using namespace std;


double func(double a) {
    return 3 * a - cos(a) - 1;
}

double first_derivative(double a) {
    return 3 + sin(a);
}

void bisection() {
    double a, b, c;
    for (int i = -100; i <= 100; i++) {
        if (func(i)* func(i + 1) < 0) {
            a = i, b = i + 1;
            int n = 100;
            while (n--) {
                c = (a + b) / 2;
                if (func(c) == 0)
                    break;
                if (func(a) * func(c) < 0)
                    b = c;
                else if (func(b) * func(c) < 0)
                    a = c;
            }
            cout << c << endl;
        }
    }
}

void false_position_method() {
    double a, b, c;
    for (int i = -100; i <= 100; i++) {
        if (func(i)* func(i + 1) < 0) {
            a = i, b = i + 1;
            int n = 100;
            while (n--) {
                c = (a * func(b) - b * func(a)) / (func(b) - func(a));
                if (func(c) == 0)
                    break;
                if (func(a) * func(c) < 0)
                    b = c;
                else if (func(b) * func(c) < 0)
                    a = c;
            }
            cout << c << endl;
        }
    }
}

void newton_rapson() {
    double a, b = 0;

    for (int i = -100; i <= 100; i++) {
        a = i;
        int n = 10000, f = 0;
        while (n--) {
            if (first_derivative(a) == 0) {
                f = 1;
                break;
            }
            a = a - (func(a) / first_derivative(a));
        }
        if (a != b && f == 0 && abs(a - b) > 0.1)
            cout << a << endl;
        b = a;

    }
}


int main() {
    //bisection();
    //false_position_method();
    newton_rapson();

}



//euler


#include<bits/stdc++.h>
using namespace std;

int main() {
	double x, y, h, n, X;
	cout << "x0 = ";
	cin >> x;
	cout << "y0 = ";
	cin >> y;
	cout << "N = ";
	cin >> n;
	cout << "x = ";
	cin >> X;
	h = (X - x) / n;
	cout << "h :" << h << endl;
	for (int i = 1; i <= n; i++) {
		y = y + h * (3 * x * x + 1);
		cout << "y" << i << " = " << y << endl;
		x += h;
	}
}




//factorization

#include<bits/stdc++.h>
using namespace std;

void file_write() {
    ofstream outf("equations.txt");
    char s[50];
    for (int i = 0; i < 3; i++)
    {
        cin >> s;
        outf << s << endl;
    }
    outf.close();
}

void file_read() {
    ifstream inf;
    inf.open("equations.txt");
    string s;
    int r = 0;
    double mat[3][4];
    while (inf)
    {
        getline(inf, s);
        //cout<<s<<endl;

        int c = 0;
        for (int i = 0; i < s.size(); i++)
        {
            int num = 0, cou = 0, j = i, b, a = 0;
            while (s[i] >= '0' && s[i] <= '9')
            {
                if (s[i - 1] == '-' && cou == 0)
                {
                    a = -1;
                }
                cou++;
                i++;
            }
            while (cou)
            {
                if (a == -1)
                    b = -1 * (s[j] - '0');
                else
                    b = s[j] - '0';
                num += b * pow(10, cou - 1);
                j++;
                cou--;
            }
            if (num != 0)
            {
                mat[r][c] = num;
                c++;
            }

        }
        r++;
    }
    for (int r = 0; r < 3; r++)
    {
        for (int c = 0; c < 4; c++)
        {
            cout << mat[r][c] << " ";
        }
        cout << endl;
    }
    double L[3][3], U[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            L[i][j] = 0;
            U[i][j] = 0;
        }
    }

    L[0][0] = 1;
    L[1][1] = 1;
    L[2][2] = 1;

    U[0][0] = mat[0][0];
    U[0][1] = mat[0][1];
    U[0][2] = mat[0][2];
    L[1][0] = mat[1][0] / mat[0][0];
    U[1][1] = mat[1][1] - mat[1][0] * mat[0][1] / mat[0][0];
    U[1][2] = mat[1][2] - L[1][0] * U[0][2];
    L[2][0] = mat[2][0] / U[0][0];
    L[2][1] = (mat[2][1] - L[2][0] * U[0][1]) / U[1][1];
    U[2][2] = mat[2][2] - L[2][0] * U[0][2] - L[2][1] * U[1][2];

    cout << "L : " << endl;
    for (int r = 0; r < 3; r++)
    {
        for (int c = 0; c < 3; c++)
        {
            cout << L[r][c] << " ";
        }
        cout << endl;
    }
    cout << "U : " << endl;
    for (int r = 0; r < 3; r++)
    {
        for (int c = 0; c < 3; c++)
        {
            cout << U[r][c] << " ";
        }
        cout << endl;
    }

    double y1, y2, y3;
    y1 = mat[0][3];
    y2 = mat[1][3] - y1 * L[1][0];
    y3 = mat[2][3] - y1 * L[2][0] - y2 * L[2][1];
    cout << "y1 = " << y1 << ", y2 = " << y2 << ", y3 = " << y3 << endl;

    double x, y, z;
    z = y3 / U[2][2];
    y = (y2 - U[1][2] * z) / U[1][1];
    x = (y1 - U[0][1] * y - U[0][2] * z) / U[0][0];
    cout << "x = " << x << ", y = " << y << ", z = " << z << endl;

    inf.close();
}


int main() {
    file_write();
    file_read();


}


//2x+3y+1z=9
//1x+2y+3z=6
//3x+1y+2z=8







//gaus sidel

#include<bits/stdc++.h>
using namespace std;

void file_write()
{
	ofstream outf("equations.txt");
	char s[50];
	for (int i = 0; i < 3; i++)
	{
		cin >> s;
		outf << s << endl;
	}
	outf.close();
}

void file_read()
{
	ifstream inf;
	inf.open("equations.txt");
	string s;
	int r = 0;
	int mat[3][4];
	while (inf)
	{
		getline(inf, s);
		//cout<<s<<endl;

		int c = 0;
		for (int i = 0; i < s.size(); i++)
		{
			int num = 0, cou = 0, j = i, b, a = 0;
			while (s[i] >= '0' && s[i] <= '9')
			{
				if (s[i - 1] == '-' && cou == 0)
				{
					a = -1;
				}
				cou++;
				i++;
			}
			while (cou)
			{
				if (a == -1)
					b = -1 * (s[j] - '0');
				else
					b = s[j] - '0';
				num += b * pow(10, cou - 1);
				j++;
				cou--;
			}

			if (num != 0)
			{
				mat[r][c] = num;
				c++;
			}

		}
		r++;
	}
	for (int r = 0; r < 3; r++)
	{
		for (int c = 0; c < 4; c++)
		{
			cout << mat[r][c] << " ";
		}
		cout << endl;
	}

	int n = 1000;
	float x, y = 0, z = 0;
	while (n--)
	{
		x = (mat[0][3] - mat[0][1] * y - mat[0][2] * z) / mat[0][0];
		y = (mat[1][3] - mat[1][0] * x - mat[1][2] * z) / mat[1][1];
		z = (mat[2][3] - mat[2][0] * x - mat[2][1] * y) / mat[2][2];
	}
	cout << x << " " << y << " " << z << endl;

	inf.close();
}


int main()
{


	file_write();
	file_read();


}




// 27x+6y-1z=85
// 6x+15y+2z=72
// -1x+1y+54z=110




//inverse

#include<bits/stdc++.h>
using namespace std;

int main()
{
	double mat[3][3];
	float d = 0;
	cout << "the elements of matrix : " << endl;

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
			cin >> mat[i][j];
	}

	for (int i = 0; i < 3; i++)
	{
		d = d + (mat[0][i] * (mat[1][(i + 1) % 3] * mat[2][(i + 2) % 3] - mat[1][(i + 2) % 3] * mat[2][(i + 1) % 3]));
	}
	cout << "Determinent :" << d << endl;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
			cout << ((mat[(j + 1) % 3][(i + 1) % 3] * mat[(j + 2) % 3][(i + 2) % 3]) - (mat[(j + 1) % 3][(i + 2) % 3] * mat[(j + 2) % 3][(i + 1) % 3])) / d << "\t";
		cout << "\n";
	}
}



//modified euler

#include<bits/stdc++.h>
using namespace std;

double func(double x, double y, double h) {
	return y + h * (2 * y / x);
}

int main() {
	double x, y, h, n, X;
	cout << "x0 = ";
	cin >> x;
	cout << "y0 = ";
	cin >> y;
	cout << "N = ";
	cin >> n;
	cout << "x = ";
	cin >> X;
	h = (X - x) / n;
	cout << "h = " << h << endl;
	double xx = x, yy;
	for (int j = 0; j < n; j++) {
		xx += h;
		yy = func(x, y, h);
		for (int i = 1; i <= 3; i++) {
			yy = y + h * (func(x, y, h) + func(xx, yy, h)) / 2;
		}
		x += h;
		y = yy;
		cout << yy << endl;
	}
	cout << yy << endl;

}


//Secant Method
#include<bits/stdc++.h>
using namespace std;

#define Er 0.0001

//function f(x) = x^2 - 4x - 10;

double f(double x)
{
	return x * x - 4 * x - 10;
}

int main()
{
	double x1, x2, x;
	int step = 0;

	cout << "Enter two inital value: ";
	cin >> x1 >> x2;

	while (1)
	{

		cout << "x1 = " << x1 << "\n";
		cout << "x2 = " << x2 << "\n";
		cout << "f(x1) = " << f(x1) << "\n";
		cout << "f(x2) = " << f(x2) << "\n";


		x = x2 - ((f(x2) * (x2 - x1)) / (f(x2) - f(x1)));

		cout << "x = " << x << "\n";

		if (abs(((x - x2) / x)) <= Er)break;
		else {
			x1 = x2;
			x2 = x;
		}
		step++;

		cout << endl << endl;

	}

	cout << "Result: " << x << " after no. of " << step << " steps" << endl;


	return 0;
}





//Runge Kutta Method

#include<bits/stdc++.h>
using namespace std;


double given_func(double x, double y) {
	return x * x + y * y;
}

int main() {
	double x, y, h, n, x1, x2;
	cout << "x0 = ";
	cin >> x;
	cout << "y0 = ";
	cin >> y;
	cout << "h = ";
	cin >> h;
	cout << "x1 = ";
	cin >> x1;
	// cout << "x2 = ";
	// cin >> x2;
	// h = (x2 - x1) / (n - 1);
	for (int i = 1; i <= (x1 ) / h; i++) {
		cout << "Step : " << i << endl;
		double k1, k2, k3, k4;
		k1 = h * given_func(x, y);
		k2 = h * given_func(x + h / 2, y + k1 / 2);
		k3 = h * given_func(x + h / 2, y + k2 / 2);
		k4 = h * given_func(x + h, y + k3);

		double del_y = (k1 + 2 * k2 + 2 * k3 + k4) / 6;
		x = x + h;
		y = y + del_y;
		cout << "x = " << x << ", y = " << y << endl;
	}
}



//trapezoidal simpson


#include<bits/stdc++.h>
using namespace std;

float trapezoidal_Rule(float l_limit, float u_limit)
{

	int n;
	cin >> n;
	float h = (u_limit - l_limit) / n;

	float ans = .5 * (log(l_limit) + log(u_limit));


	for (int i = 1; i < n; i++)
	{
		ans += log(l_limit + h * i);
	}
	ans *= h;
	return ans;
}

float simpsons_one_third_rule(float l_limit, float u_limit)
{
	int n;
	cin >> n;
	float h = (u_limit - l_limit) / n;

	float ans = log(l_limit) + log(u_limit);

	for (int i = 1; i <= n - 1; i += 2)
	{
		ans += 4 * log(l_limit + h * i);
	}
	for (int i = 2; i <= n - 2; i += 2)
	{
		ans += 2 * log(l_limit + h * i);
	}
	ans *= h;
	ans /= 3;
	return ans;
}

float simpsons_three_eights_rule(float l_limit, float u_limit)
{
	int n;
	cin >> n;
	float h = (u_limit - l_limit) / n;

	float ans = log(l_limit) + log(u_limit);

	for (int i = 1; i <= n - 1; i++)
	{
		if (i % 3 != 0)
			ans += 3 * log(l_limit + h * i);
	}
	for (int i = 3; i <= n - 3; i++)
	{
		if (i % 3 == 0)
			ans += 2 * log(l_limit + h * i);
	}
	ans *= h * 3;
	ans /= 8;
	return ans;
}


int main()
{


	float l_limit, u_limit, y;
	cin >> l_limit >> u_limit;

	printf("%.10f\n", trapezoidal_Rule(l_limit, u_limit));
	printf("%.10f\n", simpsons_one_third_rule(l_limit, u_limit));
	printf("%.10f\n", simpsons_three_eights_rule(l_limit, u_limit));

}
content_copyCOPY