Newton Raphson Method Code in c++

 Solution in C++: 

///******Bismillahir-Rahmanir-Rahim******///

///AH Tonmoy

///Department of CSE,23rd batch

    ///Islamic University,Bangladesh

    #include <bits/stdc++.h>
    using namespace std;
    double f(double x) //give the equation ,if you are use another function you can put a=..... this function
    {
        double a=pow(x,3.0)-x-11.0// fx equation
        return a;
    }
    double fprime(double x)
    {
        double b=3*pow(x,2.0)-1.0;        // first derivative of the equation
        return b;
    }
    int main()
    {
        int t,n,i;
        cout<<"Enter the test case : "<<endl;
        cin>>t;
        while(t--)
        {
            double x,x1,e,fx,fx1;

            cout<<"Enter the initial guess\n";
            cin>>x1;
            cout<<"Enter accuracy\n";
            cin>>e;
            fx=f(x);
            fx1=fprime(x);
            cout <<"x{i}"<<"    "<<"x{i+1}"<<"        "<<"|x{i+1}-x{i}|"<<endl;
            do
            {
                x=x1;              //copy x1 to x
                fx=f(x);            // f(x)to fx
                fx1=fprime(x);           // fprime(x) to fx1
                x1=x-(fx/fx1);            //calculate x[i+1] according to equation
                cout<<x<<"     "<<x1<<"           "<<abs(x1-x)<<endl;
            }
           while (fabs(x1-x)>=e);
                cout<<"The root of the equation is "<<x1<<endl;

        }
        return 0;
    }



    No comments

    Most View Post

    Recent post

    Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

      Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

    Powered by Blogger.