迭代法方程求根

求根的算法,除了迭代法,还有牛顿迭代法,用切线(一阶泰勒)近似,弦截法

设方程为 f(x)=0,用某种数学方法导出等价的形式 x=g(x),然后按以下步骤执行:

(1)选一个方程的近似根,赋给变量x。

(2)将x的值记录到 last( last=x ),计算x的新值 g(x)(x = g(x))

(3)当 x 与 last 差的绝对值还不满足指定的精度要求时,回到步骤(2)。

   x=初始近似根;  

   do {  

      last=x;  

      x=g(x); //  按特定的方程计算新的近似根

      } while ( fabs(x-last)>Epsilon);

   return x;

迭代法求方程x=cos(x)一个根的源程序为:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    float x=0.0,last;
    do
    {
          last=x;
          x=cos(x);
    }while((fabs(x-last)<1e-6))
    cout<<x<<endl;
    return 0;
}
posted @ 2023-03-05 15:55  ecnu_lxz  阅读(102)  评论(0编辑  收藏  举报