计算方法-基于Python实现牛顿法求非线性方程的根

如题,以下为代码

from sympy import *
import random

x = symbols('x')
func = x*x-2*x
Derivatives = diff(func,x)

begin = -1
end = 0.5

MAXSTEP = 100
step_count = 0
x0=-0.25
temp = func.subs(x,x0)
while step_count<MAXSTEP and abs(temp)>0.01:
    x0 = x0-(temp/(Derivatives.subs(x,x0)))
    temp = func.subs(x,x0)
    step_count += 1
print(x0)
print(step_count)

你可能感兴趣的:(一个津门带学生的计算机学习旅程)