Iteration And Newton Rapshon Method C++


#include <bits/stdc++.h>
using namespace std;
double f(double x)
{
    return (sqrt(2+(5/x)));
}
int iteration()
{
    double  x0,x1,x2;
    int c=0;
    cout<<"Enter Initial Value="<<endl;
    cin>>x0;
    cout<<"n\t\tXn\t\tf(x)"<<endl;
    cout<<".........................................................\n"<<endl;
    do
    {
        c++;
        x1=f(x0);
        x2=x0;
        x0=x1;
        cout<<c<<"\t\t"<<x2<<"\t\t"<<x1<<endl;
        cout<<".......................................................\n"<<endl;

    }while(fabs(x2-x1)>=0.0001);
    cout<<"\nTotal Count="<<c<<endl;
    cout<<"\nRoot is="<<x0<<endl<<endl;
}
double f1(double a)
{
    return (a*a*a-2*a-5);
}
double f2(double b)
{
    return(3*b*b-2);
}
int nr()
{
    double a,b,y,h,d=0;
    cout<<"Enter Value="<<endl;
    cin>>a;
    cout<<"\nn\t\tXn\n"<<endl;
    cout<<".......................................\n"<<endl;
    do
    {
        d++;
        h=-(f1(a)/f2(a));
        y=a+h;
        a=y;
        cout<<d<<"\t\t"<<y<<endl;
        cout<<"......................................\n"<<endl;

    }while(fabs(h)>=0.0001);
    cout<<"\nTotal Count="<<d<<endl;
    cout<<"\nRoot is="<<y<<endl<<endl;


}
int main()
{

    int choice;

    while(1)
    {
         cout<<"1. Iteration Method\n2.Newton-Rapson Method\n3.Stop\n"<<endl;
        cin>>choice;
        if(choice==1)
        {
            iteration();
        }
        else if(choice==2)
        {
            nr();
        }
        else
        {
            break;
        }
    }


}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment