用牛顿迭代法求方程,精确到某个值

用迭代法求解方程,精确到一个值
用迭代法求方程的正根
精确到10的-5次方

img

img


#include<stdio.h>
#include<math.h>
int main( )
{
    float m,n,x1=0.5,t;
    while(1)
    {
        m=2*x1*x1+2*x1+1-pow(2.71,2*x1);
        n=4*x1+2-2*pow(2.71,2*x1);
        t=x1;
        x1=x1-m/n;
        if(fabs(x1-t)<pow(10,-4))
        {
            printf(" %f.",x1);
            break;
        }
    }
    return 0;
} 
#include <bits/stdc++.h>

using namespace std;

double Newton(double x = 0.5) {
    cout << "牛顿迭代法:";
    double f = 1, df = 1;
    while (abs(f) > 1e-5) {
        f = 2*x*x + 2*x + 1 - exp(2*x);  // f(x)
        df = 4*x + 2 - 2*exp(2*x);    //  f'(x)
        x = x - f/df;
        cout << setprecision(8) << x << " ";
    } 
    cout << endl;
    return x;
}

int main() {
    Newton(0.5); 
    return 0;
}

运行结果:

牛顿迭代法:0.3480528 0.23905645 0.16264256 0.1099289 0.073966939 0.049618202 0.033216475 0.022205883 0.014831397 0.0098998427 

其他方程需要修改代码中对应的f和df,分别为x对应的函数值和x对应的导数值

请看👉 :用牛顿迭代法求方程的根