#include<stdio.h>
#include<stdlib.h>
#include<math.h>
float
fun(
float
x)
{
x=(x*
sin
(x))-1;
return
x;
}
float
iteration(
float
a,
float
b)
{
float
x;
x=((a*fun(b))-(b*fun(a)))/(fun(b)-fun(a));
return
x;
}
int
main()
{
int
i,n;
int
p[20];
float
a,b,x[10];
printf
(
"\nQuestion is : 4x^3-2x-6=0 \n"
);
printf
(
"\nEnter how many iteration : "
);
scanf
(
"%d"
,&n);
for
(i=0;i<n;i++)
{
p[i]=fun(i);
printf
(
"\n f[%d] : %d "
,i,p[i]);
}
for
(i=0;i<n;i++)
{
if
(p[i]*p[i+1]<0)
{
a=i;
b=i+1;
printf
(
"\nThe root lies between : [%0.4f %0.4f]"
,a,b);
break
;
}
}
x[0]=a;
x[1]=b;
printf
(
"\n x[0] : %f "
,x[0]);
printf
(
"\n x[1] : %f "
,i,x[1]);
for
(i=2;i<n;i++)
{
x[i]=iteration(x[i-2],x[i-1]);
printf
(
"\n x[%d] : %f "
,i,x[i]);
}
return
0;
}
by meet kheni
No comments:
Post a Comment