用弦截法求非线性方程23-42+3x-6=0在区间[0,3]的一个实根,精度为10-6。
是2x^3+4x^2+3x+6=0吧?
#include<stdio.h>
#include <math.h>
float f(float x)
{
return 2*pow(x,3) + 4*pow(x,2)+3*x+6;
}
float newx(float x1, float x2)
{
float x;
x = (x1*f(x2) - x2*f(x1)) / (f(x2) - f(x1));
return x;
}
float root(float x1, float x2)
{
float x, y, y1;
y1 = f(x1);
for(y = y1; fabs(y) >= 1e-6; )
{
x = newx(x1, x2);
y = f(x);
if(y*y1 > 0)
{
y1 = y;
x1 = x;
}
else
{
x2 = x;
}
}
return x;
}
int main()
{
float x;
x = root(0,3);
printf("%.2f",x);
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!