I'm to compile and run this program to solve an equation using the Newton-Raphson algorithm, but the number of iterations in the do-while loop is always 1, which shouldn't be the case.
Code
#include<math.h>
#include<stdio.h>
float fonc(float x){
float result;
result=2*(pow(x,3))-x-2;
return result;
}
float foncprime(float x){
float result;
result= 6*(pow(x,2))-1;
return result;
}
int main(void)
{
long double x1,x0;
float eps;
eps=0.5*pow(10,-4);
x0=1;
x1=1;
int i=0;
do
{
x0=x1;
x1=x0-(fonc(x0)/foncprime(x0));
i++;
}
while(x1-x0>eps);
printf("%d",i);
}
powto compute integer exponentiations. Usex*xforxsquared,x*x*xforxcubed, and5E-5for5/100000(or0.5E-4, if you prefer.)powis a lot slower and sometimes slightly less accurate. - rici