用牛顿迭代法求方程的实根

用do-while循环语句和牛顿迭代法来求方程 4 * x * x * x + 3 * x * x + 2 * x + 1 = 0 在1附近的一个实根。

#include <iostream>
#include <cmath>
using namespace std;
#define A (4 * x * x * x + 3 * x * x + 2 * x + 1)
#define B (12 * x * x + 6 * x + 2)
int main()
{ 
    double x = 1, a;
 do
    {
        a = x;
        x = a - A / B;
    }while (fabs(x - a) > (1e-5));
    cout << x << endl;
}
发布了44 篇原创文章 · 获赞 35 · 访问量 802

猜你喜欢

转载自blog.csdn.net/huangziguang/article/details/104448685