数值分析牛顿切线法python3实现并绘图

开发法求根:牛顿切线,定点迭代,正割法
切线逼近真值求根。
公式含求导


这里写图片描述


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()
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值