牛顿法解方程

来源:互联网 发布:查看centos系统补丁 编辑:程序博客网 时间:2024/06/05 10:18

牛顿法的关键在于知道哪一个才是真正的函数

  • 我们需要的是 构建 $f(x)=0$的函数

这里写图片描述

C code:

#include <stdio.h>#include <math.h>const double EPS = 1E-9;double mysqrt(double s){    double x0 = 0.01;     // the denominator should be non-zero     double f = s;    while (fabs(f) > EPS){        x0 = (x0 + s / x0) / 2;        f = s - x0 * x0;    }    return x0;}void main(){    double s = 2;    scanf("%lf", &s);    printf("the root of sqrt(%lf) is %lf", s, mysqrt(s));    getchar(); getchar();    return;}