开发法求根:牛顿切线,定点迭代,正割法
切线逼近真值求根。
公式含求导
import math
import numpy as np # 数学库
import matplotlib.pyplot as plt # 图形库
print("题目:用牛顿切线法求x=e^(-x)在3附近的根")
i = 0
def result(x):# 函数表达式
y = (x-math.exp(-x))/(1+x)
return x-y
def Y(x):#精确结果的函数
global i
i=i+1
plt.plot([x, x], [0,(x * np.exp(x) - 1)], "r-")
plt.plot([x, result(x)], [(x*np.exp(x)-1), 0])
temp = round(x-result(x),9)
print(result(x))
if(temp==0.0):
print('第', i, '迭代求得')
print('解为:',round(result(x),9))
x=result(x)
y=(result(x)*np.exp(result(x))-1)
plt.plot(x,y,".")
plt.annotate("(0.567143291,0.56714329)",xy=(result(x), (result(x)*np.exp(result(x))-1)),xytext=(result(x)-0.5, (result(x)*np.exp(result(x))-1)+6),color='k',fontsize=10)
else:
Y(result(x))
Y(3)
# 画图
x = 0
plt.title("Newton tangent method")#图形标题
x = np.linspace(0,3)
plt.xlim(0,3)# 固定坐标
plt.ylim(-10,60)
plt.plot(x,(x*np.exp
MA==/dissolve/70/gravity/SouthEast)t([0,3],[0,0],"--")
plt.show()