69,382
社区成员
发帖
与我相关
我的任务
分享
# include <stdio.h>
# include <math.h>
float f(float x)// 用于求f(x) = x 3 - 5x 2 + 16x - 80的值!
{
float y;
y = ((x - 5.0) * x + 16.0) * x - 80.0;//
return (y);
}
float xpoint (float x1, float x2)//用于求 弦与x轴的交点
{
float y;
y = (x1*f(x2)) - x2*f(x1) / (f(x2)-f(x1));
return (y);
}
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) >= 1E-6);//?????????????这里的 fabs (y)>= 1E-6 是什么意思??
//???????fabs (y) 是系统函数???还是什么??
return (x);
}
int main(void)
{
float x1, x2,f1, f2, x;
do
{
printf ("input x1, x2:");
scanf ("%f %f", &x1, &x2);
f1 = f(x1);
f2 = f(x2);
}
while (f1*f2 >= 0);
x = root(x1, x2);
printf ("A root of equation is %8.4f\n", x);//A root of equation is 一根方程
return 0;
}