Friday, July 20, 2012

C Program for Newton Raphson Method















/*Newton Raphson Method*/




#include<stdio.h>




#include<conio.h>




#include<math.h>




#include<alloc.h>




#include<process.h>




float value(float);




float **e,n;




float deri(float);




void main()




{




int i,p,it=50;




float v,xo,es=0,s,x1,ea;




char ch;




clrscr();










printf("Enter Degree of the Equation\n");




scanf("%f",&n);




puts("--------------------------------------------------");




e=(float**)malloc(((n+1)*2)*sizeof(float));




printf("Enter coeff.in decreasing order of Degree\n");




for(i=0,p=n;i<=n;i++,p--)




{




scanf("%f",&e[i][0]);




e[i][1]=p;




}




puts("-------------------------------------------------");




printf("Enter Value of Xo\n");




scanf("%f",&xo);




puts("-------------------------------------------------");




printf("What would You Like to enter Es or Sig.fig or Iteration =>E/S/I\n");




ch=getche();




puts("\n----------------------------------------------“);




if(ch=='E'||ch=='e')




{




printf("Enter value of Es\n");




scanf("%f",&es);




}




else if(ch=='s'||ch=='S')




{




printf("Enter number of Sig. figure\n");




scanf("%f",&s);




es=0.5*pow(10,(s-2));




}




else




{




printf("Enter no. of Iteration\n");




scanf("%d",&it);




}




i=0;




puts("-------------------------------------------------");




printf("Iteration\tX\t\tEa");




while(1)




{




i++;




x1=xo-value(xo)/deri(xo);




ea=(x1-xo)*100/x1;




if(ea<0)




ea=ea*(-1);




if(ea<es||value(x1)==0||i==it)




{




printf("\n%3d\t\t%6.5f\t\t%5.3f",i,x1,ea);




puts("\n---------------------------------------------");




getch();




exit(0);




}




printf("\n%3d\t\t%6.5f\t\t%5.3f",i,x1,ea);




xo=x1;




}




float value(float x)




{




int i;




float s=0;




for(i=0;i<=n;i++)




{




s=s+e[i][0]*pow(x,e[i][1]);




}




return s;




}




float deri(float x)




{




int i;




float s=0,t,h;




for(i=0;i<=n;i++)




{




t=e[i][0]*e[i][1];




if(e[i][1]>0)




h=e[i][1]-1;




else




h=0;




s=s+t*pow(x,h);




}




return s;













Output Of Program:-













First:-




Enter Degree of the Equation




2




--------------------------------------------------




Enter coeff.in decreasing order of Degree




-1 1.8 2.5




-------------------------------------------------




Enter Value of Xo




5




-------------------------------------------------




What would You Like to enter Es or Sig.fig or Iteration =>E/S/I




e




-------------------------------------------------




Enter value of Es




0.05




-------------------------------------------------




Iteration X Ea




1 3.35366 49.091




2 2.80133 19.717




3 2.72111 2.948




4 2.71934 0.065




5 2.71934 0.000




-----------------------------------------------






===============================


Second:-

Enter Degree of the Equation

3

--------------------------------------------------

Enter coeff.in decreasing order of Degree

2 -11.7 17.7 -5

-------------------------------------------------

Enter Value of Xo

3

-------------------------------------------------

What would You Like to enter Es or Sig.fig or Iteration =>E/S/I

i

-------------------------------------------------

Enter no. of Iteration

3

-------------------------------------------------

Iteration X Ea

1 5.13333 41.558

2 4.26975 20.226

3 3.79293 12.571

-----------------------------------------------

No comments:

Post a Comment