我刚编的一个关于弦截法的程序,评价一下!
#include <stdio.h>#include <math.h>
double fuc (double x)
{
double y;
y=((5*x-3)*x+1)*x-1;
return y;
}
double poi (double x1,double x2)
{
double x,a,b;
a=x1*fuc(x2)-x2*fuc(x1);
b=fuc(x2)-fuc(x1);
x=a/b;
return x;
}
void main()
{
double x1,x2,x,y,y1,y2;
do
{scanf("%lf%lf",&x1,&x2);
y1=fuc(x1);
if(fabs(y1)<1e-4) {x=x1;break;}
y2=fuc(x2);
if(fabs(y2)<1e-4) {x=x2;break;}
}while (y1*y2>1e-4);
if ( fabs(y1)>1e-4&&fabs(y2)>1e-4)
{
x=poi(x1,x2);
y=fuc(x);
while (fabs(y)>=1e-4)
{
if (fuc(x1)*fuc(x)>1e-4) x1=x;
else x2=x;
x=poi(x1,x2);
y=fuc(x);
}
}
printf("%lf\n",x);
}