C C++ code : Newton - Horner's method for solution of polynomial equation

Newton - Horner's method for finding solution of polynomial equation
/***************** Newton horner's method ******************/
#include<iostream.h>
#include<conio.h>
#include<complex.h>
#include<math.h>
int main()
{
    complex a[20],b[20],c[20];
    complex x;
    int n,i;
    clrscr();

    cout<<"Enter the degree of equations : ";cin>>n;
    cout<<"Enter all coefficients \n";
    for(i=0;i<=n;i++)
        cin>>a[i];
    cout<<"enter initial guess:";cin>>x;

    while(n>0)
    {
        c[0]=b[0]=a[0];
        for(i=1;i<=n;i++)
            b[i]=a[i]+b[i-1]*x;
        for(i=1;i<n;i++)
            c[i]=b[i]+c[i-1]*x;
        if(abs(c[n-1])==0.0)   // for complex "abs" instead of "fabs"
        {
            cout<<"Divide by Zero : ERROR !! ";
            break;
        }
        x-=b[n]/c[n-1];
        if(abs(b[n]/c[n-1])<0.00001)
        {
            cout<<"\nRoot is: "<<x<<"f(x) "<<b[n];
            for(i=1;i<n;i++)
                a[i]=b[i];
            n--;
        }
    }
getch();
return 0;
}

No comments :

Post a Comment

Your Comment and Question will help to make this blog better...