//QUESTION 8. Newton Raphson Method
// f(x)= 3x-cos(x)-1
// solution to f(x) is x=0.607101648
#include
#include
#include
int main()
{
int i,itr;
float x,y,dy,x1;
printf("\nNewton Raphson Method");
printf("\nf(x)= 3x-cos(x)-1");
printf("\n\n\nEnter approximate value of 'x' : ");
scanf("%f",&x);
printf("\n\nEnter number of iterations : ");
scanf("%d",&itr);
printf("\n\n\n\n\n");
for(i=1;i<=itr;i++)
{
y=(3*(x)-cos(x)-1);
dy=(3+sin(x));
x1=(x-(y/dy));
printf("\n\tx%d = %f",i,x1);
x=x1;
}
printf("\n\n\n\nAfter %d iterations, the value of 'x' is %f",itr,x);
printf("\n\nFYI, the solution is x = 0.607101648");
getch();
return 0;
}
No comments:
Post a Comment