all numerical codes

PHOTO EMBED

Mon Nov 06 2023 01:21:50 GMT+0000 (Coordinated Universal Time)

Saved by @jin_mori

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


//Parsing ax+by+cz=d
vector<double> string_to_coefficient(string s)
{
    vector<double> vgs;
    int num = 0;
    int sign = 1;
    int idx = 0;
    if (s[0] == '-')
    {
        sign = -1;
        idx = 1;
    }

    for (; idx < s.length(); idx++)
    {
        if (s[idx] <= '9' && s[idx] >= '0')
        {
            num = num * 10 + (s[idx] - '0');
        }
        else
        {
            if (num == 0)
                num = 1;
            vgs.push_back(sign * num);
            num = 0;
            sign = 1;
            idx++;
            if (s[idx] == '-')
            {
                sign = -1;
            }
        }
    }
    vgs.push_back(sign * num);
    return vgs;
}

void Gauss_Seidel()
{
    string e1, e2, e3;
    cin >> e1 >> e2 >> e3;

    vector<double> v1 = string_to_coefficient(e1);
    vector<double> v2 = string_to_coefficient(e2);
    vector<double> v3 = string_to_coefficient(e3);

    double a1, b1, c1, d1;
    a1 = v1[0];
    b1 = v1[1];
    c1 = v1[2];
    d1 = v1[3];

    cout << a1 << " " << b1 << " " << c1 << " " << d1 << endl;

    double a2, b2, c2, d2;
    a2 = v2[0];
    b2 = v2[1];
    c2 = v2[2];
    d2 = v2[3];

    cout << a2 << " " << b2 << " " << c2 << " " << d2 << endl;

    double a3, b3, c3, d3;
    a3 = v3[0];
    b3 = v3[1];
    c3 = v3[2];
    d3 = v3[3];

    cout << a3 << " " << b3 << " " << c3 << " " << d3 << endl;

    double x, y, z;
    x = y = z = 0;
    double px, py, pz;
    px = py = pz = 9;

    while (abs(px - x) > 0.0001 || abs(py - y) > 0.0001 || abs(pz - z) > 0.0001)
    {
        px = x;
        py = y;
        pz = z;
        x = (d1 - b1 * y - c1 * z) / a1;
        y = (d2 - a2 * x - c2 * z) / b2;
        z = (d3 - a3 * x - b3 * y) / c3;
    }

    cout << fixed << setprecision(5) << x << " " << y << " " << z << endl;
}

vector<double> v;
//Parsing ax^2+bx+c
void parse(string s)
{
    int i = 0;
    int sign = 1;
    int num = 0;
    int var = 0;
    if (s[0] == '-')
    {
        sign = -1;
        i = 1;
    }
    for (; i < s.length(); i++)
    {
        if (s[i] >= '0' && s[i] <= '9')
        {
            num = num * 10 + (s[i] - '0');
        }
        else
        {
            if (num == 0)
                num = 1;
            v.push_back(sign * num);
            num = 0;
            sign = 1;
            if (var == 0)
            {
                var = 1;
                i += 3;
            }
            else
            {
                i++;
            }
            if (s[i] == '-')
            {
                sign = -1;
            }
        }
    }
    v.push_back(sign * num);
}

double derivative(double x)
{
    return 2 * v[0] * x + v[1];
}

double function_x(double x)
{
    return v[0] * x * x + v[1] * x + v[2];
}

void newton_raphson()
{
    string equ;
    cout << "f(x)=";
    cin >> equ;
    parse(equ);

    double x0, x;

    cout << "Enter initial Guess: ";
    cin >> x0;

    int it = 0;
    while (it < 50)
    {
        x = x0 - (function_x(x0) / derivative(x0));
        cout << "Iteration: " << it << " Root = " << x << endl;

        if (fabs(x - x0) < 0.00001)
        {
            cout << "Converged to root after " << it << " iterations." << endl;
            return;
        }

        x0 = x;
        it++;
    }

    cout << "Did not converge within the maximum number of iterations." << endl;
}

void secant()
{
    string equ;
    cout << "f(x)=";
    cin >> equ;
    parse(equ);

    double x1, x2, x3;
    cout << "Enter initial Guess: ";
    cin >> x1 >> x2;
    int it = 0;
    while (it < 50)
    {
        x3 = ((function_x(x2) * x1) - (function_x(x1) * x2)) / (function_x(x2) - function_x(x1));
        cout << "Iteration: " << it << " Root = " << x3 << endl;

        if (fabs(x1 - x2) < 0.00001)
        {
            cout << "Converged to root after " << it << " iterations." << endl;
            return;
        }
        x1 = x2;
        x2 = x3;
        it++;
    }
    cout << "Did not converge within the maximum number of iterations." << endl;
}

void bisection()
{
    string equ;
    cout << "f(x)=";
    cin >> equ;
    parse(equ);

    double x1, x2, x3;
    cout << "Enter initial Guess: ";
    cin >> x1 >> x2;
    int it = 0;
    while (it < 50)
    {
        x3 = (x1 + x2) / 2.0;
        cout << "Iterations: " << it << " Roots= " << x3 << endl;
        if (fabs(function_x(x3)) < 0.00001)
        {
            cout << "Converged to root after " << it << " iterations." << endl;
            return;
        }
        else if ((function_x(x3) * function_x(x1)) < 0)
        {
            x2 = x3;
        }
        else
        {
            x1 = x3;
        }
        it++;
    }
    cout << "Did not converge within the maximum number of iterations." << endl;
}

void false_position()
{
    string equ;
    cout << "f(x)=";
    cin >> equ;
    parse(equ);

    double x1, x2, x3;
    cout << "Enter initial Guess: ";
    cin >> x1 >> x2;
    
    int it = 0;
    while (it < 50)
    {
        //x3 = ((function_x(x2) * x1) - (function_x(x1) * x2)) / (function_x(x2) - function_x(x1));
        x3 = x1 - ((function_x(x1)*(x2-x1)) / (function_x(x2)-function_x(x1)));
        cout << "Iteration: " << it << " Root = " << x3 << endl;

        if (fabs(function_x(x3)) < 0.00001)
        {
            cout << "Converged to root after " << it << " iterations." << endl;
            return;
        }
        
        if ((function_x(x3) * function_x(x1)) < 0)
        {
            x2 = x3;
        }
        else
        {
            x1 = x3;
        }
        it++;
    }
    
    cout << "Did not converge within the maximum number of iterations." << endl;
}

/*
//Parsing ax^3+bx^2+cd+d
vector<double> v;
void parse(string s)
{
    int i = 0;
    int sign = 1;
    double num = 0;
    int var = 0;
    if (s[0] == '-')
    {
        sign = -1;
        i = 1;
    }
    for (; i < s.length(); i++)
    {
        if (s[i] >= '0' && s[i] <= '9')
        {
            num = num * 10 + (s[i] - '0');
        }
        else if (s[i] == 'x')
        {
            if (num == 0)
                num = 1;
            if (i + 1 < s.length() && s[i + 1] == '^')
            {
                int j = i + 2;
                int exp = 0;
                while (j < s.length() && s[j] >= '0' && s[j] <= '9')
                {
                    exp = exp * 10 + (s[j] - '0');
                    j++;
                }
                v.push_back(sign * num * pow(1.0, exp));
                i = j - 1;
            }
            else
            {
                v.push_back(sign * num);
            }
            num = 0;
            sign = 1;
        }
        else if (s[i] == '-')
        {
            sign = -1;
        }
    }
    if (num != 0)
    {
        v.push_back(sign * num);
    }
}

double function_x(double x)
{
    return v[0] * x * x * x + v[1] * x * x + v[2] * x + v[3];
}

double derivative(double x)
{
    return 3 * v[0] * x * x + 2 * v[1] * x + v[2];
}
*/
/*
```
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
map<int , int> pw ;
void string_to_coefficient(string s){
    for(int i = 0 ; i < s.length() ; i++){
        int coeff = atol(s.substr(i).c_str());
        i+=log10(abs(coeff))+1;
        int power = 0 ;
        for(int j = i ; j < s.length() ; j++){
            if(s[j] == '^'){
                power = atol(s.substr(j+1).c_str());
                i=j+log10(power)+1 ;
                break;
            }
        }
        pw[power] += coeff;
    }
}
float fx(float x){
    float ans = 0 ;
    for(auto it : pw) ans += it.second*pow(x*1.0,it.first);
    return ans ;
}

float dx(float x){
    float ans = 0 ;
    for(auto it : pw) ans += it.second*it.first*pow(x*1.0,it.first-1);
    return ans ;
}

double initial_guess(int l , int r){
    int min = l ;
    for(int i = l ; i <= r ; i++){
        if(abs(fx(i)) < abs(fx(min))) min = i ;
    }
    return min ;
}

void newton_raphson(){
    double x1 , x;
    cout << "Enter Interval : " ;
    int l , r;
    cin >> l >> r ;
    x1 = initial_guess(min(l,r) , max(l,r));
    cout << "Initial Guess taken : " << x1 << endl ;
    int it = 0 ;
    while(it++ < 100){
        x = x1 - (fx(x1) / dx(x1)) ;
        cout << "it " << it << " : " << x << endl ;
        if(abs(x-x1) < 0.0001){
            cout << "Ans : " << x << endl ;
            return ;
        }
        x1 = x ;
    }
    if(it >= 100) cout << "Not convergent \n" ;
}

int main(){
    cout << "Enter : " ;
    string s ;
    cin >> s ;
    string_to_coefficient(s) ;
    for(auto it : pw) cout << it.second << "\tx^\t" << it.first << " \n" ;
    newton_raphson() ;
}
```
*/


/*
// LU Factorization Dynamic, can work with any variable
2x+3y+z+w=9
1x+2y+3z+4w=6
3x+y+2z=8
y+w=5

#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
typedef long long ll;

const int N = 11 ;
double A[N][N] , L[N][N] , U[N][N] , X[N] , Y[N];

void string_to_coefficient(string s , int i){
    int num = 0 ;
    int sign = 1 ;
    int idx = 0 ;
    if(s[0] == '-'){
        sign = -1 ;
        idx = 1 ;
    }
    for(int j = idx ; j < s.length() ; j++){
        if(s[j] <= '9' && s[j] >= '0'){
            num = num*10 + (s[j] - '0') ;
        }
        else if(s[j] == 'x'){
            if(num == 0) num = 1 ;
            A[i][1] = sign*num ;
            num = 0 ; sign = 1 ;
        }
        else if(s[j] == 'y'){
            if(num == 0) num = 1 ;
            A[i][2] = sign*num ;
            num = 0 ; sign = 1 ;
        }
        else if(s[j] == 'z'){
            if(num == 0) num = 1 ;
            A[i][3] = sign*num ;
            num = 0 ; sign = 1 ;
        }
        else if(s[j] == 'w'){
            if(num == 0) num = 1 ;
            A[i][4] = sign*num ;
            num = 0 ; sign = 1 ;
        }
        if(s[j] == '-') sign = -1 ;
        else if(s[j] == '+') sign = 1 ;
    }
    X[i] = sign*num ;
}

int main(){
    int n ;
    cout << "Enter the number of variables : " ;
    cin >> n ;
    cout << "Enter the equations : \n" ;
    for(int i = 1 ; i <= n ; i++){
        string s ;
        cin >> s ;
        string_to_coefficient(s , i);
    }

    cout << "\nCoefficient : \n" ;
    for(int i = 1 ; i <= n ; i++){
        for(int j = 1 ; j <= n ; j++){
            cout << A[i][j] << "  " ;
        }
        cout << "\t" << X[i] << endl ;
    }

    
    for (int i = 1; i <= n; i++){
        for (int k = i; k <= n; k++){
            float sum = 0;
            for (int j = 1; j < i; j++) sum += (L[i][j] * U[j][k]);
 
            U[i][k] = A[i][k] - sum;
        }
 
        for (int k = i; k <= n; k++){
            if (i == k) L[i][i] = 1;
            else{
                float sum = 0;
                for (int j = 0; j < i; j++) sum += (L[k][j] * U[j][i]);

                L[k][i] = (A[k][i] - sum) / U[i][i];
            }
        }
    }

    cout << "\nLower Matrix : \n" ;
    for(int i = 1 ; i <= n ; i++){
        for(int j = 1 ; j <= n ; j++){
            cout << setw(10) << L[i][j] << " ";
        }
        cout << endl ;
    }
    cout << "\nUpper Matrix : \n" ;

    for(int i = 1 ; i <= n ; i++){
        for(int j = 1 ; j <= n ; j++){
            cout << setw(10) <<U[i][j] << " ";
        }
        cout << endl ;
    }

    for(int i = 1 ; i <= n ; i++){
        float sum = 0 ;
        for(int j = 1 ; j < i ; j++) sum += L[i][j]*Y[j] ;
        Y[i] = (X[i] - sum)/L[i][i];
    }

    cout << "\nY Matrix : \n" ;
    for(int i = 1 ; i <= n ; i++){
        cout << Y[i] << endl ;
    }

    for(int i = n ; i >= 1 ; i--){
        float sum = 0 ;
        for(int j = n ; j > i ; j--) sum += U[i][j]*X[j] ;
        X[i] = (Y[i] - sum)/U[i][i];
    }

    cout << "\nSolutions : \n" ;
    for(int i = 1 ; i <= n ; i++){
        cout << "x[" << i << "] = " << X[i] << endl ;
    }
}
*/

/*
//RK Method

#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
typedef long long ll;

map<int , int> xp , yp ;
void string_to_coefficient2(string s){
    char last ;
    for(int i = 0 ; i < s.length() ; i++){
        int coeff = atol(s.substr(i).c_str());
        i+=log10(abs(coeff))+1;
        int power = 0 ;
        for(int j = i ; j < s.length() ; j++){
            if(s[j] == 'x') last = 'x' ;
            else if(s[j] == 'y') last = 'y' ;
            if(s[j] == '^'){
                power = atol(s.substr(j+1).c_str());
                i=j+log10(power)+1 ;
                break;
            }
        }
        if(last == 'x')xp[power] += coeff;
        else if(last == 'y') yp[power] += coeff ;
    }
}

float fxy(float x , float y){
    float ans = 0 ;
    for(auto it : xp) ans += it.second*pow(x*1.0,it.first);
    for(auto it : yp) ans += it.second*pow(y*1.0,it.first);
    return ans ;
}

float fx(float x){
    float ans = 0 ;
    for(auto it : pw) ans += it.second*pow(x*1.0,it.first);
    return ans ;
}

float dx(float x){
    float ans = 0 ;
    for(auto it : pw) ans += it.second*it.first*pow(x*1.0,it.first-1);
    return ans ;
}

float falsep(float x1 , float x2){
    return x1-((fx(x1)*(x2-x1)) / (fx(x2)-fx(x1))) ;
}


int main(){
    string s ;
    cin>>S;
    
        cout << "Enter equation : " ;
        cin >> s ;
        string_to_coefficient2(s);
        for(auto it : xp) cout << it.second << "x^" << it.first << "\t";
        for(auto it : yp) cout << it.second << "y^" << it.first << "\t";
        cout << endl ;

        float x , y , h , nn , x0 , y0 ;
        cout << "x0 = " ;
        cin >> x0 ;
        cout << "y0 = " ;
        cin >> y0 ;
        cout << "h = " ;
        cin >> h ;
        cout << "x = " ;
        cin >> x ;
        nn = (x-x0)/h ;
        y = y0 ;
        for(int i = 1 ; i <= nn ; i++){
            float k1 , k2 , k3 , k4 , k5 ;
            k1 = h*fxy(x0,y) ;
            k2 = h*fxy(x0 + 0.5*h, y + 0.5*k1);
            k3 = h*fxy(x0 + 0.5*h, y + 0.5*k2);
            k4 = h*fxy(x0 + h, y + k3);

            y = y + (1.0/6.0)*(k1 + 2*k2 + 2*k3 + k4);;
            x0 = x0 + h;
        }
        cout << "y("<<x<<") = " << y << endl ;

}

*/


int main()
{
    int ch;
    cout << "1. Gauss Seidel\n2. Newton Raphson\n3. Secant\n4. Bisection\n5. False Position\nEnter choice:";
    cin >> ch;
    if(ch==1)
        Gauss_Seidel();
    else if(ch==2)
        newton_raphson();
    else if(ch==3)
    secant();
    else if(ch==4)
    bisection();
    else if(ch==5)
    false_position();
    
    return 0;
}
content_copyCOPY