You are on page 1of 3

// NEWTON RAPHTION METHOD #include<iostream.h> //header file #include<iomanip.h> //for manipulation #include<math.h> // math lib. #include<conio.

h> // for clear screen float f(float x); float f1(float x); void newton(float *x,float x0,int*itr); //mathematics funs int main() { int itr=0, maxitr; float x, x0, aerr,e; clrscr(); cout<<"enter the value of x0, allowed error and maximum iteration\n"; cin>>x0>>aerr>>maxitr; do { newton(&x,x0,&itr); e=fabs((x0-x)/x)*100; //for absolute error cout<<"error in this iteration="<<e<<"%"<<"\n"; x0=x; } while(itr<maxitr&&e>aerr); getch(); return 0; } float f(float x) { return (sin(x)+x-3); // fncs to find root } float f1(float x) { return(sin(x)+1); } void newton(float *x, float x0,int*itr) { *x=x0-(f(x0)/f1(x0)); ++(*itr);

cout<<"x0="<<x0<<",f((x0)="<<f(x0)<<" in the "<<*itr<<"iteration value of x="<<setw(5)<< setprecision(4)<<*x<<"\n"; }

For 1000 iteration

You might also like