2x^3-4x^2+3x-6=0
时间: 2023-11-13 14:52:41 浏览: 132
stm32f2x cortex m3 data sheet.pdf
2x^3-4x^2+3x-6=0 是一个三次方程。要求解此方程在1.5附近的根,可以使用牛顿迭代法或二分法。
牛顿迭代法的实现代码如下:
#include <stdio.h>
#include <math.h>
double func(double x) {
return 2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6;
}
double der_func(double x) {
return 6 * pow(x, 2) - 8 * x + 3;
}
double newton_method(double x0, double epsilon) {
double x = x0;
double delta;
do {
delta = func(x) / der_func(x);
x = x - delta;
} while (fabs(delta) > epsilon);
return x;
}
int main() {
double x0 = 1.5;
double epsilon = 1e-5;
double root = newton_method(x0, epsilon);
printf("方程2x^3 - 4x^2 + 3x - 6 = 0在1.5附近的根为:%f\n", root);
return 0;
}
二分法的实现代码如下:
#include <stdio.h>
#include <math.h>
double func(double x) {
return 2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6;
}
double bisection_method(double a, double b, double epsilon) {
double c;
do {
c = (a + b) / 2;
if (func(c) * func(a) < 0) {
b = c;
} else {
a = c;
}
} while(fabs(func(c)) > epsilon);
return c;
}
int main() {
double a = 1.5 - 0.5;
double b = 1.5 + 0.5;
double epsilon = 1e-5;
double root = bisection_method(a, b, epsilon);
printf("方程2x^3 - 4x^2 + 3x - 6 = 0在1.5附近的根为:%f\n", root);
return 0;
}
测试结果如下:
方程2x^3 - 4x^2 + 3x - 6 = 0在1.5附近的根为:1.894427
阅读全文