c program for newton raphson algorithm for finding roots of a polynomial
c program to implement newton raphson method for finding roots of a polynomial
Collapse
X
-
Tags: None
-
c program for newton raphson using a for loop
how can i implement a c progran for newton raphson algorithm using a for loop?Comment
-
c program for newton raphson method
#include<stdio. h>
/Calculate j(10) from j(0)
int main()
{
int n;
double j=0.6;
printf("forward iterations\n");
printf("initial estimate:j(0)=% g\n",j);
for(n=1,n<=10,n ++)
j=1-(j*n);
printf("j(%d)=% g\n",n,j);
}
return 0;
}
this code is not helping me find roots of( x*x-2).What do I need to change?Comment
-
Ok, let me see if I understand your code.
I'm a numerical methods and modeling engineer so I should understand the Newton-Raphson Method to be:
j( i+1 ) = j( i ) - [ j( i )^2 - 2 ] / [ 2*j( i ) ] for the function f( j ) = (j*j - 2).
So, you should step i forward until f( j ) is within your tolerance of 0. Then your answer should be your last j.
Here's some pseudocode:
i = 0
while( absoluteValue( f( j ) ) > toleranceValue)
{
j = j - [ f( j ) ] / [ df( j ) ];
i++;
}
printOut("Root of Function is at %g After %d Iterations" j, i );
When you write this out in whatever language you want, should work.
- MilesComment
Comment