2023年6月29日发(作者:)
⽜顿迭代及其python实现⽜顿迭代法:实数域和复数域上近似求解⽅程的⽅法,具体看图:⼤写字母为线,⼩写字母为点。线A 切函数任意 点a,交x轴于 点d;过 线d 做垂线交函数于 点b,过 点b 做切线 线D,交x轴于 点e。通过反复做切线,取交点(点d,点e...),得到的点会⽆限趋近函数的x轴交点,于是可以求得函数的近似解。数学表达式为:x1 = x0 - f(x0) / f'(x0), 这⾥就不做推导了,下⾯是⼀个 python 的实现,可⽤来解⾼阶函数:from sympy import *def newton0(exp, dot_x_val, times, total): """ newton iteration to compute the result of higher-order function invoke like: newton0(4*x**2 + 5*x, 1, 0, 5) :param exp: your function expression, like: 4*x**2 + 5*x :param dot_x_val: The x coordinate values, you need pass a init value to ensure first dot :param times: current compute times, pass 0 when invoke :param total: total compute times, bigger value make a more precision result :return: """ if times >= total: return dot_x_val x = symbols('x') y = exp # function derivation diff0 = diff(y, x) # f'(x0) diff_val = (subs={x: dot_x_val}) # f(x0) origin_val = (subs={x: dot_x_val}) # x1 = x0 - f(x0)/f'(x0) dot_x_val = dot_x_val - origin_val / diff_val return newton0(exp, dot_x_val, times + 1, total)if __name__ == '__main__': x = symbols('x') print(newton0(x**6 + 2*x**2 + 10*x - 20, 1, 0, 5))⼏个参数分别是 函数表达式,第⼀个采样点的 x 坐标,当前的计算次数(调⽤传0),需要迭代的次数(次数越⾼计算精度越⾼),测试效果图如下:最终的值在1.266左右,和函数图象基本吻合。就说这么多了,祝各位策码奔腾!
发布者:admin,转转请注明出处:http://www.yc00.com/web/1687977363a62853.html
评论列表(0条)