牛顿法(作业用)

#include <cmath>
#include <iostream>
#include <stdio.h>
const double eps = 1e-5;
using namespace std;
double ans;int cnt;
void newton1(){
    double x0,x1;cnt =1;
    x0 = 0.5;
    double fx = x0*x0 - exp(x0);
    double fx1 = 2*x0- exp(x0);
    x1 = x0 - fx/ fx1;
    while (fabs(x1 - x0)> eps){
        x0 = x1;cnt++;
        double fx = x0*x0 - exp(x0);
        double fx1 = 2*x0- exp(x0);
        x1 = x0 - fx/fx1;
    }
    ans= x1;
}

int main (){
    newton1();
    printf("牛顿法迭代次数为: %d\n",cnt);
    printf("牛顿法求得第一题的根为: %lf\n",ans);
    return 0;
}
阅读更多

没有更多推荐了,返回首页