#include<stdio.h>
#include<math.h>
double f(double x)
{
return x*x*x-4*x-9;
}
double fd(double x)
{
return 3*x*x-4;
}
void main()
{
int i=1;
double a,b,tol;
printf("Enter value of y0:");
scanf("%lf",&a);
printf("Enter tolerance:");
scanf("%lf",&tol);
while(1)
{
b=a;
a=a-f(a)/fd(a);
printf(" y%d ---> %f\n",i++,a);
if(fabs(a-b)<tol)
break;
}
printf("\n Solution=%f",a);
}
Output:
Enter value of y0:2
Enter tolerance:0.00001
y1 ---> 3.125000
y2 ---> 2.768530
y3 ---> 2.708196
y4 ---> 2.706529
y5 ---> 2.706528
Solution=2.706528
Enter value of y0:3
Enter tolerance:0.00001
y1 ---> 2.739130
y2 ---> 2.706998
y3 ---> 2.706528
y4 ---> 2.706528
Solution=2.706528
Enter value of y0:5
Enter tolerance:0.00001
y1 ---> 3.647887
y2 ---> 2.953279
y3 ---> 2.730187
y4 ---> 2.706777
y5 ---> 2.706528
y6 ---> 2.706528
Solution=2.706528
No comments:
Post a Comment