求方程的根
#include <stdio.h>#include <math.h>
float f(float x)
{
return ((x-5)*x+16)*x-80;
}
float xpoint(float x1,float x2)
{
return(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
}
float root (float x1,float x2)
{
int i;
float x,y,y1;
y1=f(x1);
do
{
x=xpoint(x1,x2);
y=f(x);
if(y*y1>0)
{
y1=y;
x1=x;
}
else
{
x2=x;
}
}
while (fabs(y)>0.00001);
return x;
}
main()
{
float x,x1,x2,y1,y2;
do
{
printf("Input x1,x2:");
scanf("%f,%f",&x1,&x2);
y1=f(x1);
y2=f(x2);
}
while (y1*y2>0);
x=root(x1,x2);
printf("A root is %f\n",x);
}
有没有简单点的方法除了用弦截法