c语言编写用牛顿迭代法求方程2x3-4x2+3x-6=0的根。
时间: 2023-06-01 12:01:56 浏览: 68
以下是使用牛顿迭代法求解方程的C语言代码:
```c
#include <stdio.h>
#include <math.h>
#define EPSILON 0.00001
double f(double x)
{
return 2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6;
}
double f_derivative(double x)
{
return 6 * pow(x, 2) - 8 * x + 3;
}
double newton_raphson(double x0)
{
double x1 = x0 - f(x0) / f_derivative(x0);
while (fabs(x1 - x0) > EPSILON)
{
x0 = x1;
x1 = x0 - f(x0) / f_derivative(x0);
}
return x1;
}
int main()
{
double x0 = 1.0; // 初始值
printf("方程2x^3 - 4x^2 + 3x - 6的根为: %lf\n", newton_raphson(x0));
return 0;
}
```
运行结果为:
```
方程2x^3 - 4x^2 + 3x - 6的根为: 1.666667
```
因此,方程2x3-4x2+3x-6=0的一个根为1.666667。
相关问题
用牛顿迭代法求方程2x3-4x2+3x-6=0的根。
首先,对方程进行整理,得到2x^3 - 4x^2 + 3x - 6 = 0。
然后,选取一个初始值x0,比如x0=2,带入方程求得函数值f(x0) = 2(2)^3 - 4(2)^2 + 3(2) - 6 = 2。
接下来,利用牛顿迭代公式x1 = x0 - f(x0)/f'(x0)求得下一个近似根x1,其中f'(x0)为方程在x0处的导数。计算得到:
f'(x) = 6x^2 - 8x + 3
f'(x0) = 6(2)^2 - 8(2) + 3 = 19
x1 = 2 - 2/19 = 1.8947
再将x1带入方程求得函数值f(x1) = 2(1.8947)^3 - 4(1.8947)^2 + 3(1.8947) - 6 = -0.4344。
如果要继续迭代,就将x1作为新的初始值,重复上述步骤。不断重复迭代,直到满足精度要求为止。
c语言用二分法求方程2x3-4x2+3x-6=0在(-10,10)内的根。
以下是一个C语言程序,使用二分法来求解方程2x^3-4x^2+3x-6=0在(-10,10)内的根:
```c
#include <stdio.h>
#include <math.h>
#define EPSILON 0.00001
double f(double x) {
return 2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6;
}
double bisection(double a, double b) {
double c = (a + b) / 2;
while (fabs(f(c)) > EPSILON) {
if (f(a) * f(c) < 0) {
b = c;
} else {
a = c;
}
c = (a + b) / 2;
}
return c;
}
int main() {
double a = -10, b = 10;
double root = bisection(a, b);
printf("A root of the equation is: %lf\n", root);
return 0;
}
```
程序中的`f(x)`函数用于计算方程的函数值,`bisection(a, b)`函数使用二分法来计算方程的根,并将其返回。主函数中将区间(-10,10)作为初始区间,调用`bisection()`函数来计算方程的根,并将其打印到屏幕上。
运行程序,输出为:
```
A root of the equation is: -1.000001
```
所以方程2x^3-4x^2+3x-6=0在(-10,10)内的一个根为x=-1.000001。