0% found this document useful (0 votes)
29 views2 pages

Lab 3

This code uses Newton's method to find the roots of polynomials. The user inputs the degree of the polynomial, its coefficients, and an initial guess. The code then calculates the functional and derivative values at each iteration using Newton's method formula. It continues iterating until the error is below a set threshold. Finally, it prints the roots of the polynomial that were found. It provides examples finding the roots of x^3+x^2-3x-3=0 and x^2-3x+2=0.

Uploaded by

Pawan Subedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Lab 3

This code uses Newton's method to find the roots of polynomials. The user inputs the degree of the polynomial, its coefficients, and an initial guess. The code then calculates the functional and derivative values at each iteration using Newton's method formula. It continues iterating until the error is below a set threshold. Finally, it prints the roots of the polynomial that were found. It provides examples finding the roots of x^3+x^2-3x-3=0 and x^2-3x+2=0.

Uploaded by

Pawan Subedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Newton’s method of Polynomials

Problems:
1. x^3+x^2-3x-3=0 2. x^2-3x+2=0
Code:
// Newton's Method of polynomial

#include<stdio.h> //header files


#include<conio.h>
#include<math.h>
#define E 0.0001 //error
void main() //main program begins
{
float a[10],b[10],fx[10],fdx,root[10],xr,x0,error;
int n,i,j;
clrscr();
printf("Enter degree of polynomial: "); // takes degree of polynomial
scanf("%d",&n);
printf("Enter coefficient of polynomial:\n"); // takes coefficient of polynomial
for(i=0;i<=n;i++)
{ printf("a [%d]=",i);
scanf("%f",&a[i]);
}
printf("\nEnter initial guess x0="); // takes initial guess
scanf("%f",&x0);
printf("Entered equation is:\n"); // display entered equation
for(i=n;i>=0;i--)
printf("+(%fx^%d)",a[i],i);
printf("=0\n");
for(j=n;j>=2;j--) //Calculating functional, derivative value using NR method
{
do
{ fx[n]=a[n];
for(i=n-1;i>=0;i--)
{ fx[i]=fx[i+1]*x0+a[i];
}
fdx=a[n];
for(i=n-1;i>=1;i--)
{ fdx=(fdx*x0+fx[i]);
}
xr=x0-(fx[0]/fdx);
error=(xr-x0)/xr;
x0=xr;
}while(fabs(error)>=E);
root[j]=xr;
b[n]=0;
for(i=n-1;i>=0;i--)
{ b[i]=(a[i+1]+xr*b[i+1]);
}
for(i=n;i>=0;i--)
{ a[i]=b[i]; }
}

Rajesh / Numerical Method Lab 3


root[1]=-a[0]/a[1];
printf("\nRoots of given polynomial are:\n");
for(i=1;i<=n;i++)
printf("\nRoot%d=%f",i,root[i]); //display roots of polynomial
getch(); // main program end
}

Outputs:

1. Enter degree of polynomial:3


Enter coefficient of polynomial:
a [0]=-3
a [1]=-3
a [2]=1
a [3]=1

Enter initial guess x0=2


Entered equation is:
+ (1.000000x^3) + (1.000000x^2) + (-3.000000x^1) + (-3.000000x^0) =0

Roots of given polynomial are:

Root1= -1.732051
Root2= -1.000000
Root3= 1.732051

2.
Enter degree of polynomial:2
Enter coefficient of polynomial:
a [0]=2
a [1]= -3
a [2]=1

Enter initial guess x0=0


Entered equation is:
+ (1.000000x^2) + (-3.000000x^1) + (2.000000x^0) =0

Roots of given polynomial are:

Root1=2.000000
Root2=1.000000

Rajesh / Numerical Method Lab 3

You might also like