Showing posts with label Numerical Method. Show all posts
Showing posts with label Numerical Method. Show all posts

Tuesday, April 18, 2017

C++ code for Derivative using Newton Backward Difference Formula

This is the solution for finding Derivative using Newton's Backward Difference Formula in C++


#include <iostream>
#include <conio.h>
using namespace std;

class DerivativeBackward
{
public:
    void askN();
    void askX();
    void askF();
    void askXX();
    void forwardTable();
    void calcd1();
    void calcd2();
    void findH();
    void solve();
    void fillDelF();
    void findS();
private:
    double XX, x[10] , f[10][10] , p[10],diff[10][10],P1,delF[10],f1,f2;
    int n;
    double h,s;

};

void DerivativeBackward::askX()
{
    cout << endl;
    for(int i = 0; i<n; i++ )
    {
        cout << "ENter X[" << i  << "] : ";
        cin >> x[i];
    }
    cout << endl;
}

void DerivativeBackward::askF()
{

    for(int j = 0; j<n; j++ )
    {
        cout << "ENter F[" << j  << "] : ";
        cin >> f[0][j];
    }
    cout << endl;
}
void DerivativeBackward::askXX()
{

    cout << "Enter X for which the value is to be found: ";
    cin >> XX;
}

void DerivativeBackward::forwardTable()
{
    for(int i = 1; i < n; i++)
    {
        for(int j = 0; j< n-i;j++)
        {
            f[i][j] = (f[i-1][j+1]-f[i-1][j]);
            if(f[i][j] < 0.0000009 && f[i][j] > 0 || f[i][j] >-0.0000009 && f[i][j]<0)
            {
                    f[i][j] = 0;
            }
        }
    }
    cout << endl;
    cout << "Sn\tXi\tf(Xi)\t";
    for(int i = 0; i <n-1;i++)
    {
        cout << i+1 << " diff\t";
    }
    cout << endl;
    for(int i = 0; i < n; i++)
    {
         cout <<i+1 <<"\t" << x[i]<< "\t";
        for(int j = 0; j< n-i;j++)
        {
            cout  << f[j][i] << "\t";
        }
        cout << endl;
    }
}

void DerivativeBackward::findH()
{
    h = x[1] - x[0];
    cout << "h = " <<h;
}
void DerivativeBackward::findS()
{
    s = (XX - x[n-1])/h;
    cout << "s = " <<s;
}
void DerivativeBackward::solve()
{
    findH();
    findS();
    fillDelF();
    calcd1();
    calcd2();


    cout <<endl << "The value of f1 :" << f1;
    cout <<endl << "The value of f2 :" << f2;
    cout  << endl << endl;
}


void DerivativeBackward::fillDelF()
{
    for(int i = 1;i<10;i++)
    {
        if(i<n)
         delF[i] = f[i][n-i-1];
        else
            delF[i] = 0;
    }
    for(int i = 1;i<10;i++)
    {
        cout<< delF[i];
    }
}

void DerivativeBackward::calcd1()
{
    f1 = 1 / h * ( delF[1] + 1/2.0 * (2 * s +1 ) * delF[2] + 1 / (6.0) * (3*s*s + 6 *s +2)*delF[3]
        + 1 /(24.0) *( 4*s*s*s+18*s*s+22*s+6)*delF[4]);
}

void DerivativeBackward::calcd2()
{
    f2 = 1 / (h*h) * (delF[2] + 1/6.0 * (6*s+6) * delF[3] + 1/24.0 *(12*s*s+36*s+22)*delF[4]);

}
void DerivativeBackward::askN()
{
    cout << "Enter the number of values: ";
    cin >> n;
}
int main()
{

    DerivativeBackward d1;
    d1.askN();
    d1.askX();
    d1.askF();
    d1.askXX();
    d1.forwardTable();
    d1.solve();
}

C++ code for Derivative using Newton Forward Difference Formula

This is the solution for finding Derivative using Newton Forward Difference Formula in C++.


#include <iostream>
#include <conio.h>
using namespace std;

class DerivativeForward
{
public:
    void askN();
    void askX();
    void askF();
    void askXX();
    void forwardTable();
    void calcd1();
    void calcd2();
    void findH();
    void solve();
    void fillDelF();
    void findS();
private:
    double XX, x[10] , f[10][10] , p[10],diff[10][10],P1,delF[10],f1,f2;
    int n;
    double h,s;

};

void DerivativeForward::askX()
{
    cout << endl;
    for(int i = 0; i<n; i++ )
    {
        cout << "ENter X[" << i  << "] : ";
        cin >> x[i];
    }
    cout << endl;
}

void DerivativeForward::askF()
{

    for(int j = 0; j<n; j++ )
    {
        cout << "ENter F[" << j  << "] : ";
        cin >> f[0][j];
    }
    cout << endl;
}
void DerivativeForward::askXX()
{

    cout << "Enter X for which the value is to be found: ";
    cin >> XX;
}

void DerivativeForward::forwardTable()
{
    for(int i = 1; i < n; i++)
    {
        for(int j = 0; j< n-i;j++)
        {
            f[i][j] = (f[i-1][j+1]-f[i-1][j]);
            if(f[i][j] < 0.0000009 && f[i][j] > 0 || f[i][j] >-0.0000009 && f[i][j]<0)
            {
                    f[i][j] = 0;
            }
        }
    }
    cout << endl;
    cout << "Sn\tXi\tf(Xi)\t";
    for(int i = 0; i <n-1;i++)
    {
        cout << i+1 << " diff\t";
    }
    cout << endl;
    for(int i = 0; i < n; i++)
    {
         cout <<i+1 <<"\t" << x[i]<< "\t";
        for(int j = 0; j< n-i;j++)
        {
            cout  << f[j][i] << "\t";
        }
        cout << endl;
    }
}

void DerivativeForward::findH()
{
    h = x[1] - x[0];

}
void DerivativeForward::findS()
{
    s = (XX - x[0])/h;

}
void DerivativeForward::solve()
{
    findH();
    findS();
    fillDelF();
    calcd1();
    calcd2();


    cout <<endl << "The value of f1 :" << f1;
    cout <<endl << "The value of f2 :" << f2;
    cout  << endl << endl;
}


void DerivativeForward::fillDelF()
{
    for(int i = 1;i<10;i++)
    {
        if(i<n)
         delF[i]=f[i][0];
        else
            delF[i] = 0;
    }
}

void DerivativeForward::calcd1()
{
    f1 = 1 / h * ( delF[1] + 1/2.0 * (2 * s -1 ) * delF[2] + 1 / (6.0) * (3*s*s - 6 *s +2)*
        delF[3] + 1 /(24.0) *( 4*s*s*s-18*s*s+22*s-6)*delF[4]);
}

void DerivativeForward::calcd2()
{
    f2 = 1 / (h*h) * (delF[2] + 1/6.0 * (6*s-6) * delF[3] + 1/24.0 *(12*s*s-36*s+22)*delF[4]);

}
void DerivativeForward::askN()
{
    cout << "Enter the number of values: ";
    cin >> n;
}
int main()
{

    DerivativeForward d1;
    d1.askN();
    d1.askX();
    d1.askF();
    d1.askXX();
    d1.forwardTable();
    d1.solve();
}

Monday, April 17, 2017

C++ code for Simpson's 1/3 Rule

This is the solution for finding Integration using Simpson's 1/3 Rule


#include <iostream>
#include <math.h>

using namespace std;


class Simpsons1b3
{
public:
    Simpsons1b3()
    {
        k = 2;
    }
    void askInterval();
    void solve();
    double f(double x);
    void findH();
    void findX1();
private:
    double I,h,xn,x0,k,x1;

};

void Simpsons1b3::askInterval()
{

    cout << "Enter Xn: ";
    cin >> xn;
    cout << "Enter X0: ";
    cin >> x0;
}

void Simpsons1b3::findH()
{
    h= (xn - x0)/k;
}

void Simpsons1b3::findX1()
{

    x1 = x0 + h;
}


double Simpsons1b3::f(double x)
{
    return exp(x);
}

void Simpsons1b3::solve()
{
    askInterval();
    findH();
    findX1();
    I = h / 3.0 * ( f(x0) +4 * f(x1) + f(xn) );
    cout << endl << "The required answer is : " << I << endl;
}
int main()
{
    Simpsons1b3 s1;
    s1.solve();
    return 0;
}


C++ code for Least Square Method

This is the solution for Least Square Method

#include <iostream>
#include <math.h>
using namespace std;

class LeastSquare
{

public:
    LeastSquare()
    {
        Ex = Ey = Exy = Exx = 0;
    }
    void askN();
    void askX();
    void askY();
    void findXY();
    void findXX();
    void findA();
    void findB();
    void solve();

private:
    double a, b , x[10],y[10],xy[10],xx[10];
    int num;
    double Ex,Ey,Exy,Exx;

};

void LeastSquare::askN()
{
    cout << "Enter the no of values: ";
    cin >> num ;
}
void LeastSquare::askX()
{
   cout << "Enter the values of X[] : \n";
    for(int i = 0 ; i <num;i++)
    {
        cout << "X[" << i << "]: ";
        cin >>x[i];
        Ex += x[i];
    }
}
void LeastSquare::askY()
{
    cout << "Enter the values of Y[] : \n";
    for(int i = 0 ; i <num;i++)
    {
        cout << "Y[" << i << "]: ";
        cin >>y[i];
        Ey += y[i];
    }
}

void LeastSquare::findXY()
{
    for(int i = 0; i < num; i++)
    {
        xy[i] = x[i] * y[i];
        Exy += xy[i];
    }

}
void LeastSquare::findXX()
{
    for(int i = 0; i < num; i++)
    {
        xx[i] = x[i] * x[i];
        Exx += xx[i];
    }

}
void LeastSquare::findA()
{
    a = Ey/num - b * Ex / num;
}
void LeastSquare::findB()
{
    b = (num * Exy - Ex * Ey) / (num * Exx - pow(Ex,2.0));

}

void LeastSquare::solve()
{
    askN();
    askX();
    askY();
    findXX();
    findXY();
    findB();
    findA();
    cout <<endl << "The Required eqn of straight line is: ";
    cout << endl << "y = " <<  a << " + " << b << "x";
}

int main()
{
    LeastSquare l;
    l.solve();

    return 0;
}

C++ code for Fixed Point Iteration Method

This is the solution for finding Root using Fixed Point Iteration method in C++

#include <iostream>
#include <math.h>
using namespace std;

class FixedPoint
{
public:
    void askEqn();
    double g(double x);
    void solve();
    void findError();
    void askX0();
    void display();
private:
    double a,b,c;
    double Error;
    double x0,x1;


};

void FixedPoint::askX0()
{
    cout << "Enter initial value of X0: ";
    cin >> x0;
}

void FixedPoint::askEqn()
{
    cout << "For the eqn ax*x + bx + c = 0 , Enter a,b,c,d:\n";
    cin >>a>>b>>c;
}
double FixedPoint::g(double x)
{
    return -(a*x*x + c) / b;
}

void FixedPoint::findError()
{
    Error = fabs( (x1-x0)/x1 );
}

void FixedPoint::solve()
{
    askEqn();
    askX0();
    cout << endl << endl << "S.N.\tXi\tXi+1\tError\n";
    do{
        x1 = g(x0);
        findError();
        display();
        x0 = x1;
    }while(Error>=0.0009);

    cout << endl<< "The root is : " << x1 <<  endl;
}
void FixedPoint::display()
{
    static int i;
    cout << ++i << "\t" << x0 << "\t" << x1 << "\t" << Error << endl;
}
int main()
{
    FixedPoint f1;
    f1.solve();
    return 0;
}

C++ code for Gauss Siedel method

This is the solution for Gauss Siedel method in C++

#include <iostream>

using namespace std;


class GaussSiedel
{

public:
    GaussSiedel()
    {
        x0=x1=x2 = 0;
    }
    void askEqn();
    void no_Iterate();
    void solve();
    void calcX0();
    void calcX1();
    void calcX2();

private:
    double a0,b0,c0,a1,b1,c1,a2,b2,c2,d0,d1,d2;
    double x0,x1,x2;
    double num;

};

void GaussSiedel::askEqn()
{
    cout << "For the eqn a0x0 + b0x1 + c0x2 = d0 , Enter a0,b0,c0,d0:\n";
    cin >>a0 >>b0>>c0>>d0;
    cout << "For the eqn a1x0 + b1x1 + c1x2 = d1 , Enter a1,b1,c1,d1:\n";
    cin >>a1 >>b1>>c1>>d1;
    cout << "For the eqn a2x0 + b2x1 + c2x2 = d2 , Enter a2,b2,c2,d2:\n";
    cin >>a2>>b2>>c2>>d2;
}

void GaussSiedel::no_Iterate()
{

    cout << "Enter up to how much iteration do you want to go: ";
    cin >> num;
}
void GaussSiedel::calcX0()
{
    x0 = ( d0 - b0 * x1 - c0 * x2) /a0;
}

void GaussSiedel::calcX1()
{
    x1 = ( d1 - a1 * x0 - c1 * x2) /b1;
}

void GaussSiedel::calcX2()
{
    x2 = ( d2 - b2 * x1 - a2 * x0) / c2;
}

void GaussSiedel::solve()
{
    for(int i = 0;i<num;i++)
    {
        calcX0();
        calcX1();
        calcX2();

        cout << endl << endl <<"Iteration " << i+1 << ": \n";
        cout << "X0 = " << x0 << endl << "X1 = " << x1 << endl << "X2 = " << x2 ;
    }


}

int main()
{
    GaussSiedel g1;
    g1.askEqn();
    g1.no_Iterate();
    g1.solve();
    return 0;
}

Saturday, April 15, 2017

C++ code for Gauss Jordan method

This is the C++ source code for Gaussian Jordan or Gauss Jordan Method.
#include <iostream>
#include <iomanip>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;


class GaussJordan
{
    public:
        GaussJordan();
        void setMatA();
        void setMatB();
        void solve();
        void setRowCol();
        bool pivotZero(int row);
        void normalize(int row);
        void exchangeRow(int row);
        void makeBelowPivotZero(int row);
        void makeAbovePivotZero(int row);
        void display();

    protected:

    private:
        double a[5][5],x[5];
        int rowA,colA;
};
GaussJordan::GaussJordan()
{
    //ctor
}
void GaussJordan::setRowCol()
{
    cout << "Enter row of Matrix A: ";
    cin >> rowA;
    cout << "Enter column of Matrix A: ";
    cin >> colA;
}
void GaussJordan::setMatA()
{
    cout << "For Matrix A : " << endl;
    for(int i =  0; i < rowA; i++ )
    {
        for(int j = 0; j < colA; j++)
        {

            cout << "Enter A[" << i << "]" << "[" << j << "] : " ;
            cin >> a[i][j];
        }
    }
}
void GaussJordan::setMatB()
{
    cout << "For Matrix B: " << endl;
    for(int i =  0; i < rowA; i++ )
    {

            cout << "Enter B[" << i << "]" << "[" << 0 << "] : " ;
            cin >> a[i][colA];
    }
}

void GaussJordan::solve()
{
    for(int i = 0; i < rowA; i++)
    {
        if(!pivotZero(i))
        {
            normalize(i);
        }
        else
        {
            exchangeRow(i);
            normalize(i);
        }
        makeBelowPivotZero(i);
    }

    for(int i = rowA-1; i > 0; i--)
    {
        makeAbovePivotZero(i);
    }
}

bool GaussJordan::pivotZero(int row)
{
    if(a[row][row] == 0)
    {
        return true;
    }
    else
        return false;
}

void GaussJordan::exchangeRow(int row)
{
    //cout << "Exchanfged\n";
    int k = row+1;
    int great = row+1;
    while (k<rowA)
    {
        if(a[great][row]<a[k][row])
            great  = k;
        k++;
    }
    //cout << great << endl;

    for (int j = 0; j < colA+1; j++)
    {
        double temp;
        temp = a[row][j];
        a[row][j] = a[great][j];
        a[great][j] = temp;
    }
    display();
}
void GaussJordan::normalize(int row)
{
    //cout << "normalized \n";
    double normal = a[row][row];
    for(int j = 0; j < colA+1; j++)
    {
        a[row][j] /= normal;
    }
    display();
}
void GaussJordan::makeBelowPivotZero(int row)
{
    //cout << "made zero" << endl;
    for(int i = row+1; i < rowA; i++)
    {
        double zeroFactor = a[i][row];
      //  cout << "zero = " << zeroFactor<< endl;
        for(int j = 0; j < colA+1; j++)
        {
            a[i][j] -= zeroFactor*a[row][j];
        }
        display();
    }

}
void GaussJordan::makeAbovePivotZero(int row)
{
    //cout << "made zero" << endl;
    for(int i = row-1; i >= 0; i--)
    {
        double zeroFactor = a[i][row];
       // cout << "zero = " << zeroFactor<< endl;
        for(int j = row; j < colA+1; j++)
        {
            a[i][j] -= zeroFactor*a[row][j];
        }
        display();
    }

}

void GaussJordan::display()
{
    for(int i = 0; i < rowA; i++)
    {
        for (int j = 0; j < colA+1; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }

 cout << endl << endl;
}

int main()
{
    GaussJordan g1;
    cout << setprecision(4);
    g1.setRowCol();
    g1.setMatA();
    g1.setMatB();
    g1.solve();
    getch();
    return 0;
}

C++ code for Gauss Elimination method

This is the C++ Program source code for Gaussian Elimination to find the system of Linear Algebraic equations
#include <iostream>
#include <iomanip>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;


class GaussElimination
{
    public:

        void setMatA();
        void setMatB();
        void solve();
        void setRowCol();
        bool pivotZero(int row);
        void normalize(int row);
        void exchangeRow(int row);
        void makeBelowPivotZero(int row);
        void substitute();

        void display();

    protected:

    private:
        double a[5][5],x[5];
        int rowA,colA;
};

void GaussElimination::setRowCol()
{
    cout << "Enter row of Matrix A: ";
    cin >> rowA;
    cout << "Enter column of Matrix A: ";
    cin >> colA;
}
void GaussElimination::setMatA()
{
    cout << "For Matrix A : " << endl;
    for(int i =  0; i < rowA; i++ )
    {
        for(int j = 0; j < colA; j++)
        {

            cout << "Enter A[" << i << "]" << "[" << j << "] : " ;
            cin >> a[i][j];
        }
    }
}
void GaussElimination::setMatB()
{
    cout << "For Matrix B: " << endl;
    for(int i =  0; i < rowA; i++ )
    {

            cout << "Enter B[" << i << "]" << "[" << 0 << "] : " ;
            cin >> a[i][colA];
    }
}

void GaussElimination::solve()
{
    for(int i = 0; i < rowA; i++)
    {
        if(!pivotZero(i))
       // this is done because if the pivot in any row is zero which creates error
        {
            normalize(i);
        }
        else
        {
            exchangeRow(i);
            normalize(i);
        }
        makeBelowPivotZero(i);
    }
    substitute();
}


void GaussElimination::substitute()
{
    x[rowA-1] = a[rowA-1][colA];


    for(int i = rowA-2; i>=0 ;i--)
    {
        double s = 0;
        for(int j = i+1; j < colA; j++)
        {
            s+= a[i][j] * x[j];
        }
        x[i] = a[i][colA] - s;
    }
    cout << endl << "The Required values are:\n";
    for(int i = 0; i < rowA;i++)
    cout << "X["<<i<<"]: "<< x[i] << endl;

}
bool GaussElimination::pivotZero(int row)
{
    if(a[row][row] == 0)
    {
        return true;
    }
    else
        return false;
}

void GaussElimination::exchangeRow(int row)
{
  //  cout << "Exchanfged\n";
    int k = row+1;
    int great = row+1;
    while (k<rowA)
    {
        if(a[great][row]<a[k][row])
            great  = k;
        k++;
    }
    cout << great << endl;

    for (int j = 0; j < colA+1; j++)
    {
        double temp;
        temp = a[row][j];
        a[row][j] = a[great][j];
        a[great][j] = temp;
    }
    display();
}
void GaussElimination::normalize(int row)
{
    //cout << "normalized \n";
    double normal = a[row][row];
    for(int j = 0; j < colA+1; j++)
    {
        a[row][j] /= normal;
    }
    display();
}
void GaussElimination::makeBelowPivotZero(int row)
{
   // cout << "made zero" << endl;
    for(int i = row+1; i < rowA; i++)
    {
        double zeroFactor = a[i][row];
   //     cout << "zero = " << zeroFactor<< endl;
        for(int j = 0; j < colA+1; j++)
        {
            a[i][j] -= zeroFactor*a[row][j];
        }
        display();
    }

}

void GaussElimination::display()
{
    cout << setprecision(4) << endl;
    for(int i = 0; i < rowA; i++)
    {
        for (int j = 0; j < colA+1; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }

 cout << endl << endl;
}

int main()
{
    GaussElimination g1;


    g1.setRowCol();
    g1.setMatA();
    g1.setMatB();
    g1.solve();
    getch();
    return 0;
}

C++ Program Code for False Position Method

This is the C++ Program source code for finding the root using False Position Method
#include <iostream>
#include <math.h>

using namespace std;

class FalsePosition
{
public:
    FalsePosition()
    {
        Error = 99;

    }
    void askFun();
    void askBrackets();
    double f(double x);
    void display();
    void findError();
    void findX0();
    void solve();
private:
    double a,b,c;
    double x0,x1,x2;
    double Error;
};

void FalsePosition::askFun()
{
    //can only perform for quadratic eqn. change for other equation
    cout.setf(ios::fixed,ios::floatfield);
    cout.precision(4);
    cout << "Enter a,b,c of ax^2+bx+c = 0: " ;
    cin >> a >> b >> c;
}

void FalsePosition::askBrackets()
{
    cout << "Enter two points: ";
    cin >> x1 >> x2;
}

double FalsePosition::f(double x)
{
    return a*x*x+b*x+c; // solves all equation of the form a*x*x+b*x+c;
}

void FalsePosition::findError()
{
    Error = fabs((x2-x1) / x2);
}
void FalsePosition::findX0()
{
    x0 = x1 - (( f(x1) * (x2 - x1)) / ( f(x2) - f(x1)) );
}
void FalsePosition::solve()
{
    int i = 0;
    if(f(x1) * f(x2) < 0)
    {
        cout << "\nS.N.\tx1\tx2\tf(x1)\tf(x2)\tx0\tf(x0)\tError\n";
        while(i<=7) // In fixed point set the precision low
        {
            findX0();
            findError();
            display();
            if(f(x1) * f(x0) < 0)
                x2 = x0;
            else
                x1 = x0;
            i++;
        }


        cout << endl << endl << "The Root is : " << x0 << endl;
    }
    else
        cout << "There lies no root in between the given points" << endl;
}
void FalsePosition::display()
{
    static int i;
    cout << ++i << "\t" << x1 << "\t" << x2 <<
 "\t" << f(x1) << "\t" << f(x2) << "\t" << x0 <<
 "\t" << f(x0) << "\t" << Error << endl;
}
int main()
{
    FalsePosition b1;
    b1.askFun();
    b1.askBrackets();
    b1.solve();
    return 0;
}

Wednesday, April 12, 2017

C++ code to find Interpolated Value using Cubic Splines Interpolation

This is the solution to find Interpolated Value at given point using Cubic Splines Interpolation


#include <iostream>
#include <math.h>
using namespace std;

class cubic
{
public:
    void askAll();
    void askI();
    void askX();
    void askFi();
    void askXi();

    void findA();
    void findH();
    void findS();
    void findU();

    void solve();


private:
    double xi[20],f[20],h[19],s[19],a[20],u[20];
    int Inumber;
    double XX;

};
void cubic::askX()
{
    cout << "Enter X: ";
    cin >> XX;
}
void cubic::askI()
{
    cout << "Enter number of Items: ";
    cin >> Inumber;
}

void cubic::askXi()
{
    for(int i = 0; i < Inumber; i++)
    {
        cout << "Enter X" << i << ": ";
        cin >> xi[i];
    }
}


void cubic::askFi()
{
    for(int i = 0; i < Inumber; i++)
    {
        cout << "Enter F" << i << ": ";
        cin >> f[i];
    }
}

void cubic::findU()
{
    for(int i = 0; i < Inumber; i++)
    {
        u[i] = XX - xi[i];
    }
}

void cubic::findH()
{
    for(int i = 1; i < Inumber; i++)
    {
        h[i] = xi[i] - xi[i-1];
    }
}
void cubic::findA()
{
    for(int i = 1; i < Inumber-1; i++)
    {
        a[i] = ( 6*( (f[i+1]-f[i])/h[i+1] -  (f[i]-f[i-1])/h[i]  )  -  h[i]*a[i-1]  -  
               h[i+1]*a[i+1]  ) / (2*( h[i] + h[i+1] ));
    }
}

void cubic::findS()
{
    int k;
    for(int i = 1; i < Inumber; i++)
    {
        if(XX>=xi[i-1]&& XX<=xi[i]){
        s[i] =  a[i-1]/(6*h[i]) * ( pow(h[i],2)*u[i] - pow(u[i],2) )  +  a[i]/(6*h[i]) * 
               ( pow(u[i-1],3)  - pow(h[i],2)*u[i-1]  )   +  1/h[i] * (f[i]*u[i-1]  -  
               f[i-1]*u[i]);
        k = i;
    }
    }
    cout << "s "<< s[k];

}

void cubic::askAll()
{
    askI();
    askXi();
    askFi();
    askX();
    a[0] = 0;
    a[Inumber-1] = 0;
}

void cubic::solve()
{
    findH();
    findU();
    findA();
    findS();
}

int main()
{

    cubic c1;
    c1.askAll();
    c1.solve();
    return 0;
}

C++ code to find Interpolated Value using Newton Backward Difference Interpolation

This is the solution to find the Interpolated Value at given point using Newton Backward Difference Interpolation
#include <iostream>
#include <conio.h>
using namespace std;

class NewtonBackward
{
public:
    void askN();
    void askX();
    void askF();
    void askXX();
    void forwardTable();
    void calcP();
    void findH();
    void findS();
private:
    double XX, x[10] , f[10][10] , p[10],diff[5][5],P1;
    int n;
    double h,s;

};

void NewtonBackward::askX()
{
    cout << endl;
    for(int i = 0; i<n; i++ )
    {
        cout << "ENter X[" << i  << "] : ";
        cin >> x[i];
    }
    cout << endl;
}

void NewtonBackward::askF()
{

    for(int j = 0; j<n; j++ )
    {
        cout << "ENter F[" << j  << "] : ";
        cin >> f[0][j];
    }
    cout << endl;
}
void NewtonBackward::askXX()
{

    cout << "Enter X for which the value is to be found: ";
    cin >> XX;
}

void NewtonBackward::forwardTable()
{
    for(int i = 1; i < n; i++)
    {
        for(int j = 0; j< n-i;j++)
        {
            f[i][j] = (f[i-1][j+1]-f[i-1][j]);
        }
    }
    cout << endl;
    cout << "Sn\tXi\tf(Xi)\t";
    for(int i = 0; i <n-1;i++)
    {
        cout << i+1 << " diff\t";
    }
    cout << endl;
    for(int i = 0; i < n; i++)
    {
         cout <<i+1 <<"\t" << x[i]<< "\t";
        for(int j = 0; j< n-i;j++)
        {
            cout  << f[j][i] << "\t";
        }
        cout << endl;
    }
}

void NewtonBackward::findH()
{
    h = x[1] - x[0];
}
void NewtonBackward::findS()
{
    s = (XX - x[n-1])/h;
}
void NewtonBackward::calcP()
{
    findH();
    findS();

    P1 = f[0][n-1];
    for(int i = 1;i<n;i++)
    {
        double k = s;
        for(int j = 1; j<i;j++)
        {
            k *= (s+j) * 1/(j+1);
        }
        k*=f[i][n-i-1];
        P1 += k ;

    }
    cout <<endl << "The value of P" << n-1 << "(" << XX << "): "  <<P1 << endl << endl;
}

void NewtonBackward::askN()
{
    cout << "Enter the number of values: ";
    cin >> n;
}
int main()
{

    NewtonBackward d1;
    d1.askN();
    d1.askX();
    d1.askF();
    d1.askXX();
    d1.forwardTable();
    d1.calcP();

}

C++ code to find Interpolated Value using Newton Forward Difference Interpolation

This is the solution to find interpolated value at given point using Newton Forward Difference Interpolation
#include <iostream> #include <conio.h> using namespace std; class NewtonForward { public: void askN(); void askX(); void askF(); void askXX(); void forwardTable(); void calcP(); void findH(); void findS(); private: double XX, x[10] , f[10][10] , p[10],diff[5][5],P1; int n; double h,s; }; void NewtonForward::askX() { cout << endl; for(int i = 0; i<n; i++ ) { cout << "ENter X[" << i << "] : "; cin >> x[i]; } cout << endl; } void NewtonForward::askF() { for(int j = 0; j<n; j++ ) { cout << "ENter F[" << j << "] : "; cin >> f[0][j]; } cout << endl; } void NewtonForward::askXX() { cout << "Enter X for which the value is to be found: "; cin >> XX; } void NewtonForward::forwardTable() { for(int i = 1; i < n; i++) { for(int j = 0; j< n-i;j++) { f[i][j] = (f[i-1][j+1]-f[i-1][j]); } } cout << endl; cout << "Sn\tXi\tf(Xi)\t"; for(int i = 0; i <n-1;i++) { cout << i+1 << " diff\t"; } cout << endl; for(int i = 0; i < n; i++) { cout <<i+1 <<"\t" << x[i]<< "\t"; for(int j = 0; j< n-i;j++) { cout << f[j][i] << "\t"; } cout << endl; } } void NewtonForward::findH() { h = x[1] - x[0]; } void NewtonForward::findS() { s = (XX - x[0])/h; } void NewtonForward::calcP() { findH(); findS(); P1 = f[0][0]; for(int i = 1;i<n;i++) { double k = s; for(int j = 1; j<i;j++) { k *= (s-j) * 1/(j+1); } k*=f[i][0]; P1 += k ; } cout <<endl << "The value of P" << n-1 << "(" << XX << "): " <<P1 << endl << endl; } void NewtonForward::askN() { cout << "Enter the number of values: "; cin >> n; } int main() { NewtonForward d1; d1.askN(); d1.askX(); d1.askF(); d1.askXX(); d1.forwardTable(); d1.calcP(); }

C++ code to find Interpolated Value using Divided Difference Table

This is the solution for finding the Interpolated value at given point using Divided Difference Table in C++
#include <iostream>
#include <conio.h>
using namespace std;

class divide
{
public:
    void askP();
    void askX();
    void askF();
    void askXX();
    void diffTable();
    void calcP();
private:
    double XX, x[10] , f[10][10] , p[10],diff[5][5],P1;
    int n;

};

void divide::askP()
{
    cout << "Enter n: ";
    cin >> n;
}

void divide::askX()
{
    cout << endl;
    for(int i = 0; i<n; i++ )
    {
        cout << "ENter X[" << i  << "] : ";
        cin >> x[i];
    }
    cout << endl;
}

void divide::askF()
{

    for(int j = 0; j<n; j++ )
    {
        cout << "ENter F[" << j  << "] : ";
        cin >> f[0][j];
    }
    cout << endl;
}
void divide::askXX()
{

    cout << "Enter X: ";
    cin >> XX;
}

void divide::diffTable()
{
    for(int i = 1; i < n; i++)
    {
        for(int j = 0; j< n-i;j++)
        {
            f[i][j] = (f[i-1][j+1]-f[i-1][j])/(x[i+j]-x[j]);
        }
    }
    cout << endl;
    cout << "Sn\tXi\tf(Xi)\t";
    for(int i = 0; i <n-1;i++)
    {
        cout << i+1 << " diff\t";
    }
    cout << endl;
    for(int i = 0; i < n; i++)
    {
         cout <<i+1 <<"\t" << x[i]<< "\t";
        for(int j = 0; j< n-i;j++)
        {
            cout  << f[j][i] << "\t";
        }
        cout << endl;
    }
}

void divide::calcP()
{
    P1 = 0;
    for(int i = 0;i<n;i++)
    {
        double k = 1;
        for(int j = 0; j<i;j++)
        {
            k*=  (XX - x[j]);
        }
        P1 += k * f[i][0];

    }
    cout <<endl << "The value of P" << n-1 << "(" << XX << "): "  <<P1 << endl << endl;
}
int main()
{

    divide d1;
    d1.askP();
    d1.askX();
    d1.askF();
    d1.askXX();
    d1.diffTable();
    d1.calcP();

}

C++ code for Bisection Method to find Root

This is the solution for finding the roots of a function by Bisection Method in C++ Object Oriented Approach
#include <iostream>
#include <math.h>

using namespace std;

class Bisection
{
public:
    Bisection()
    {
        Error = 99;

    }
    void askFun();
    void askBrackets();
    double f(double x);
    void display();
    void findError();
    void findX0();
    void solve();
private:
    double a,b,c;
    double x0,x1,x2;
    double Error;
};

void Bisection::askFun()
{
    //can only perform for quadratic eqn. change for other equation
    cout.setf(ios::fixed,ios::floatfield);
    cout.precision(4);
    cout << "Enter a,b,c of ax^2+bx+c = 0: " ;
    cin >> a >> b >> c;
}

void Bisection::askBrackets()
{
    cout << "Enter two points: ";
    cin >> x1 >> x2;
}

double Bisection::f(double x)
{
    return a*x*x+b*x+c; // solves all equation of the form a*x*x+b*x+c;
}

void Bisection::findError()
{
    Error = fabs((x2-x1) / x2);
}
void Bisection::findX0()
{
    x0 = (x1+x2)/2;
}
void Bisection::solve()
{
    if(f(x1) * f(x2) < 0)
    {
        cout << "\nS.N.\tx1\tx2\tf(x1)\tf(x2)\tx0\tf(x0)\tError\n";
        while(Error>=0.00009)
        {
            findX0();
            findError();
            display();
            if(f(x1) * f(x0) < 0)
                x2 = x0;
            else
                x1 = x0;
        }

        cout << endl << endl << "The Root is : " << x0 << endl;
    }
    else
        cout << "There lies no root in between the given points" << endl;
}
void Bisection::display()
{
    static int i;
    cout << ++i << "\t" << x1 << "\t" << x2 << "\t" << f(x1) << "\t" << f(x2) << "\t" << x0 << 
    "\t" << f(x0) << "\t" << Error << endl;
}
int main()
{
    Bisection b1;
    b1.askFun();
    b1.askBrackets();
    b1.solve();
    return 0;
}

Tuesday, April 11, 2017

C++ code for Secant Method to find Root

This is the solution for Secant Method using C++ Object Oriented Aproach.
#include <iostream>
using namespace std;

class solveForRoot
{
    public:
        solveForRoot();
        void askQuadEqn();
        void askPoints();
        double f(double x);
        void findX2();
        void findError();
        void solve();
        void display();
    private:
        double x0,x1,x2,Error,a,b,c;
};
solveForRoot::solveForRoot()
{
    Error = 99;
}
void solveForRoot::askQuadEqn()
{
    cout.setf(ios::fixed,ios::floatfield);
            cout.precision(4);
            cout << "Enter a,b,c of ax^2+bx+c = 0: " ;
            cin >> a >> b >> c;
}

void solveForRoot::askPoints()
{
    cout << "Enter two points: " ;
    cin >> x0 >> x1;
}

double solveForRoot::f(double x)
{
    return a*x*x+b*x+c; // solves all equation of the form a*x*x+b*x+c;
}

void solveForRoot::findX2()
{
    x2 = x1 - (f(x1)*(x1-x0))/(f(x1)-f(x0));
}

void solveForRoot::findError()
{
    Error = (x2-x1)/x2;
        if(Error < 0)
            Error = -Error;

}

void solveForRoot::solve()
{
 cout << "x0\t" << "x1\t" << "x2\t" << "f(x0)\t" << "f(x1)\t" << "f(x2)\t" <<
 "Error\t" << endl;
    while(Error >= 0.00009)
    {
        findX2();
        findError();
        display();
        x0 = x1;
        x1 = x2;
    }
    cout << endl << endl;
    cout << "The value of root is : " << x2 << endl;
}

void solveForRoot::display()
{
 cout << x0 << "\t"<< x1 << "\t" << x2 << "\t" << f(x0) << "\t" << f(x1)
 << "\t" << f(x2)<< "\t" << Error << endl;
}

int main()
{
    solveForRoot no1;
    no1.askQuadEqn();
    no1.askPoints();
 no1.solve();

    return 0;
}

C++ code for Lagrange Interpolation

Solution for Langrange Interpolation using C++ Object Oriented Approach

#include <iostream>

using namespace std;

class lagrange
{
public:
    lagrange()
    {
        p = 0;
    }
    void askF();
    void findL();
    void askX();
    void findP();

    void askN()
    {
        cout << "Enter number of values: ";
        cin >> n;
    }
    void askXX()
    {
        cout << "Enter the value of X to find y: ";
        cin >> xx;
    }

private:
    double Li[10],x[10],xx,Fi[10],p;
    int n;
};

void lagrange::findL()
{
        for(int i = 0; i < n; i ++ )
        {
            Li[i] = 1;
            for(int j = 0; j<n;j++)
            {

                if(j!=i)
                {
                    Li[i] *= (xx-x[j])/(x[i]-x[j]);
                }
            }
        }
        cout << endl << endl;
        for(int i = 0;i<n;i++)
        {
            cout <<"Li[" <<i <<"] : "<<Li[i] << endl;
        }
}

void lagrange::askX()
{
    cout << "ENter given Xs :" << endl;
    for (int i = 0; i<n ;i++)
    {
        cout << "ENter x[" << i << "]: " ;
        cin >> x[i];
    }
}

void lagrange::askF()
{
    cout << "ENter F :" << endl;
    for (int i = 0; i<n ;i++)
    {
        cout << "ENter F[" << i << "]: " ;

        cin >> Fi[i];
    }
}

void lagrange::findP()
{
    for(int i = 0;i<n;i++)
    {
        p += Fi[i]*Li[i];
    }
    cout <<endl<<"The Value of P"<< n-1 << "(" << xx << "): "<< p ;
}

int main()
{
    lagrange l1;
    l1.askN();
    l1.askXX();
    l1.askX();
    l1.askF();
    l1.findL();
    l1.findP();

    return 0;
}

C++ code for Newton Raphson Method

Solution for finding root from Newton Raphson method in C++

#include <iostream>
#include <conio.h>

using namespace std;

class solveNrMethod{
public:
    void getData()
    {
        cout << "Enter xi";
        cin >> xi;
    }

    double f(double x)
    {
        return x*x-3*x+2; // Change the equations for respective problem
    }

    double f2(double x)
    {
        return 2*x-3; // Find  the derivative for respective problem
    }

    double findError()
    {

        Error = (xi2-xi)/xi2;
        if(Error < 0)
            Error = -Error;
        return Error;

    }

    void calcxi2()
    {
        xi2 = xi - f(xi)/f2(xi);
    }

    void exchange()
    {
        xi = xi2;
    }

    void printTable(){
        cout.setf(ios::fixed,ios::floatfield);
        cout.precision(3);
        cout << xi << "\t" << f(xi)<< "\t" << f2(xi) << "\t"<< xi2 << "\t"<< Error <<endl;
    }

private:
    double xi,xi2,Error;

};

int main(){

 solveNrMethod no1;
 no1.getData();

    double e = 99;

    while(e >= 0.00009)
    {
        no1.calcxi2();
        e = no1.findError();
        no1.printTable();
        no1.exchange();
    }

 return 0;
}

C++ code of Gauss Elimination with Pivoting

Solution for Gauss Elimination with pivoting using C++ Program Code
#include <iostream>
#include <iomanip>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;


class GaussPivot
{
    public:

        void setMatA();
        void setMatB();
        void solve();
        void setRowCol();
        bool greatPivot(int row);
        void normalize(int row);
        void exchangeRow(int row);
        void makeBelowPivotZero(int row);
        void substitute();

        void display();

    protected:

    private:
        double a[5][5],x[5];
        int rowA,colA;
};

void GaussPivot::setRowCol()
{
    cout << "Enter row of Matrix A: ";
    cin >> rowA;
    cout << "Enter column of Matrix A: ";
    cin >> colA;
}
void GaussPivot::setMatA()
{
    cout << "For Matrix A : " << endl;
    for(int i =  0; i < rowA; i++ )
    {
        for(int j = 0; j < colA; j++)
        {

            cout << "Enter A[" << i << "]" << "[" << j << "] : " ;
            cin >> a[i][j];
        }
    }
}
void GaussPivot::setMatB()
{
    cout << "For Matrix B: " << endl;
    for(int i =  0; i < rowA; i++ )
    {

            cout << "Enter B[" << i << "]" << "[" << 0 << "] : " ;
            cin >> a[i][colA];
    }
}

void GaussPivot::solve()
{
    for(int i = 0; i < rowA; i++)
    {
        if(greatPivot(i))
        {
            normalize(i);
        }
        else
        {
            exchangeRow(i);
            normalize(i);
        }
        makeBelowPivotZero(i);
    }
    substitute();
}


void GaussPivot::substitute()
{
    x[rowA-1] = a[rowA-1][colA];


    for(int i = rowA-2; i>=0 ;i--)
    {
        double s = 0;
        for(int j = i+1; j < colA; j++)
        {
            s+= a[i][j] * x[j];
        }
        x[i] = a[i][colA] - s;
    }
    cout << endl << "The Required values are:\n";
    for(int i = 0; i < rowA;i++)
    cout << "X["<<i<<"]: "<< x[i] << endl;

}
bool GaussPivot::greatPivot(int row)
{
   // cout << "pivot Check" << endl;
    for(int i = row+1; i < rowA; i++)
    {
        if(fabs(a[row][row]) < fabs(a[i][row]))
        {
           // cout << a[i][row]<<"great" << endl;
            display();

            return false;
        }
    }
    return true;
}

void GaussPivot::exchangeRow(int row)
{
  //  cout << "Exchanfged\n";
    int k = row+1;
    int great = row+1;
    while (k<rowA)
    {
        if(a[great][row]<a[k][row])
            great  = k;
        k++;
    }
    cout << great << endl;

    for (int j = 0; j < colA+1; j++)
    {
        double temp;
        temp = a[row][j];
        a[row][j] = a[great][j];
        a[great][j] = temp;
    }
    display();
}
void GaussPivot::normalize(int row)
{
    //cout << "normalized \n";
    double normal = a[row][row];
    for(int j = 0; j < colA+1; j++)
    {
        a[row][j] /= normal;
    }
    display();
}
void GaussPivot::makeBelowPivotZero(int row)
{
   // cout << "made zero" << endl;
    for(int i = row+1; i < rowA; i++)
    {
        double zeroFactor = a[i][row];
   //     cout << "zero = " << zeroFactor<< endl;
        for(int j = 0; j < colA+1; j++)
        {
            a[i][j] -= zeroFactor*a[row][j];
        }
        display();
    }

}

void GaussPivot::display()
{
    cout << setprecision(4) << endl;
    for(int i = 0; i < rowA; i++)
    {
        for (int j = 0; j < colA+1; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }

 cout << endl << endl;
}

int main()
{
    GaussPivot g1;


    g1.setRowCol();
    g1.setMatA();
    g1.setMatB();
    g1.solve();
    getch();
    return 0;
}