Python 官方文档:入门教程 => 点击学习
目录 一、牛顿迭代公式 二、利用牛顿迭代公式求平方根 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,利用牛顿迭代公式,则有: 。
#include #include double square_root(double a){if (a < 0){return -1;}double t = a; // t 为近似值while (fabs(t * t - 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。
def square_root(a): if a < 0: return -1 t = a while abs(t * t - a) > 1e-10: t = (t + a / t) / 2.0 return 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) = 3x2,利用牛顿迭代公式,则有: 。
#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;}
def cube_root(a): t = a while abs(t * t * t - a) > 1e-10: t = (2 * t + a / (t * t)) / 3.0 return tprint(cube_root(3)) # 1.4422495703074112print(cube_root(5)) # 1.7099759466766973print(cube_root(10)) # 2.154434690031893print(cube_root(-10)) # -2.154434690031893
来源地址:https://blog.csdn.net/melonyzzZ/article/details/127655972
--结束END--
本文标题: 牛顿迭代法
本文链接: https://www.lsjlt.com/news/391021.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0