2*x*x*x-4*x*x+3*x-6=0
用牛顿迭代法,求下面方程在1.5附近的根
#include<stdio.h>
#include<math.h>
int main()
{
double x1 = 1.0, x2 = 1.5;
double f1 = 0.0, f2 = 0.0;
while (fabs(x1 - x2) >= 1e-5)
{
x1 = x2;
f1 = 2 * x1 * x1 * x1 - 4 * x1 * x1 + 3 * x1 - 6;
f2 = 6 * x1 * x1 - 8 * x1 + 3;
x2 = x1 - f1 / f2;
}
printf("方程在1.5附近的根%.2lf\n",x2);
}
输出结果:
用二分查找法,求这个方程的根:
#include<stdio.h>
#include<math.h>
int main()
{
double f1 = 1.0, x = 0.0;
double left = -10.0, right = 10.0;
double mid = 0.0;
while (fabs(f1)>=1e-5)
{
mid = (left + right) / 2;
x = mid;
f1 = 2 * x * x * x - 4 * x * x + 3 * x - 6;
if (f1 < 0)
left=mid ;
else if (f1 > 0)
right=mid ;
else
break;
}
printf("%.1lf\n",x);
return 0;
}
输出结果: