目录

一、牛顿迭代公式

二、利用牛顿迭代公式求平方根

C 语言实现

Python 语言实现

三、利用牛顿迭代公式求立方根

C 语言实现

Python 语言实现



一、牛顿迭代公式

多数方程不存在求根公式,因此求精确根非常困难,甚至不可解,从而寻求方程的近似根就显得尤为重要。牛顿就提出了一种用迭代求方程近似根的方法,思路是不断取切线,用线性方程的根逼近非线性方程 f(x) = 0 的根

具体过程

设 x* 是 f(x) = 0 的根,选取 x0 作为 x*的初始近似值,过点 (x0,f(x0)) 作曲线 y = f(x) 的切线 L,L:y = f(x0) + f'(x0)(x – x0),则 L 与 x 轴交点的横坐标为:,称 x1 为 x* 的一次近似值。过点 (x1, f(x1)) 作曲线 y = f(x) 的切线,切线与 x 轴的交点横坐标为:,称 x2 为 x* 的二次近似值。重复上述过程,得 x*的近似值序列,其中:称为 x* 的 n + 1 次近似值,上式称为牛顿迭代公式

概述图

二、利用牛顿迭代公式求平方根

求数 a 的平方根,即求二次方程 f(x) = x^2 – a = 0(a >= 0)的根,f'(x) = 2x,利用牛顿迭代公式,则有:

C 语言实现

#include #include double square_root(double a){if (a  1e-10){t = (t + a / t) / 2.0;}return t;}int main(){double a = 0.0;scanf("%lf", &a);double ret = square_root(a);printf("%lf\n", ret);return 0;}

fabs(t * t – a) > 1e-10:是将方程的近似值 t 代入方程 f(x) = x^2 – a 中,判断绝对误差是否小于等于 1e – 10。

Python 语言实现

def square_root(a):if a  1e-10:t = (t + a / t) / 2.0return tprint(square_root(3))# 1.7320508075688772print(square_root(5))# 2.236067977499978print(square_root(10))# 3.162277660168379print(square_root(-10))# -1

三、利用牛顿迭代公式求立方根

同理,求数 a 的立方根,即求三次方程 f(x) = x^3 – a = 0 的根,f'(x) = 3×2,利用牛顿迭代公式,则有:

C 语言实现

#include #include double cube_root(double a){double t = a;while (fabs(t * t * t - a) > 1e-10){t = (2 * t + a / (t * t)) / 3.0;}return t;}int main(){double a = 0.0;scanf("%lf", &a);double ret = cube_root(a);printf("%lf\n", ret);return 0;}

Python 语言实现

def cube_root(a):t = awhile abs(t * t * t - a) > 1e-10:t = (2 * t + a / (t * t)) / 3.0return tprint(cube_root(3))# 1.4422495703074112print(cube_root(5))# 1.7099759466766973print(cube_root(10))# 2.154434690031893print(cube_root(-10))# -2.154434690031893