#include<stdio.h>
#include<math.h>
float fun(float
x)
{
float fun;
fun=(2*sin(x))-x;
return fun;
}
void main()
{
float
xl,xu,fl,fu,eps,xr,fr,d,xro=0;
int i,n;
printf("Enter
xl & xu = ");
scanf("%f
%f",&xl,&xu);
printf("Enter
error limit=");
scanf("%f",&eps);
fl=fun(xl);
fu=fun(xu);
printf("fl=%f
,fu=%f",fl,fu);
if(fl*fu>0)
{
printf("xl & xu do not bracket any
rule");
return;
}
i=1;
xr=((xu*fl)-(xl*fu))/(fl-fu);
d=fabs(xr-xro);
printf("\nxl=%f,xu=%f,\nroot=%f ,
d=%f, i=%d",xl,xu,xr,d,i);
do
{
i=i+1;
fr=fun(xr);
if(fl*fr>=0)
{
xl=xr;
fl=fr;
}
else
{
xu=xr;
fu=fr;
}
xr=((xu*fl)-(xl*fu))/(fl-fu);
d=fabs(xr-xro);
printf("\nxl=%f,xu=%f,\nroot=%f ,
d=%f, i=%d",xl,xu,xr,d,i);
xro=xr;
}
while(d>=eps);
if(d<=eps)
{
printf("\nAns:The root of the
equation is x = %f with error of %f",xr,d);
}
}
No comments:
Post a Comment