#include<stdio.h>
#include<math.h>
float fun(float
x)
{
float fun;
fun=(x*x*x)-(5*x)+1;
return fun;
}
float dfun(float
x)
{
float dfun;
dfun=3*(x*x)-5;
return dfun;
}
void main()
{
float
x,xo,y,yd,d,xao=0,eps;
int i;
printf("Enter
x= ");
scanf("%f",&x);
printf("Enter
error limit=");
scanf("%f",&eps);
i=0;
xo=x;
yd=dfun(x);
if(fabs(yd)<=0)
{
printf("x do not bracket any
rule");
return;
}
do
{
i=i+1;
x=xo;
y=fun(x);
yd=dfun(x);
xo=x-(y/yd);
d=fabs(xo-xao);
printf("\ni=%d\nx=%f\ty=%f\tyd=%f\txa=%f\td=%f",i,x,y,yd,xo,d);
xao=xo;
}
while(d>=eps);
if(d<=eps)
{
printf("\nAns:The root of the
equation is x = %f with error of %f",xo,d);
}
}
No comments:
Post a Comment