正割法是近似的牛頓切線法,把求導(dǎo)用斜率代替,用切線不斷逼近函數(shù)的單根。
示意圖
迭代的起點(diǎn)
推廣的公式
核心code,直接放上去
眼熟不
你可以編寫一個(gè)簡(jiǎn)單的函數(shù)來(lái)測(cè)試這個(gè)功能
就是這么簡(jiǎn)單,當(dāng)然了字?jǐn)?shù)這么少,還成為不了一篇原創(chuàng)文章。再寫一個(gè)小程序。
我們可以使用Matploatlib的繪圖功能模擬
引入
寫好要計(jì)算的函數(shù)
def Y(x):
global i
i = i+1
plt.plot([x, x], [0, (x**3-x-1)])
plt.plot([x, result(x)], [(x**3-x-1), 0])
temp = round(x-result(x), 5)
if(temp == 0.0):
print('正割法第', i, '次')
print('解得:', round(x, 5))
x = result(x)
y = (result(x)**3 - result(x) - 1)
plt.plot(x, y, ".")
plt.plot(x, y, "g-")
plt.annotate("(1.32472,1.32472)", xy=(result(x), (result(x)**3 - result(x) - 1)),
xytext=(result(x) - 0.5, (result(x)**3 - result(x) - 1) + 2), color='k', fontsize=10)
else:
Y(result(x))
Y(2.7)
x = 0
plt.title("secant method")
x = np.linspace(0, 3)
plt.xlim(0, 3) # 固定坐標(biāo)
plt.ylim(-5, 20)
plt.plot(x, x**3-x-1, "b-")
plt.grid(True)
plt.plot([0, 3], [0, 0], "--")
plt.show()
審核編輯:劉清
-
算法
+關(guān)注
關(guān)注
23文章
4625瀏覽量
93124 -
python
+關(guān)注
關(guān)注
56文章
4802瀏覽量
84890
原文標(biāo)題:Python實(shí)現(xiàn)所有算法-正割法(Secant)
文章出處:【微信號(hào):TT1827652464,微信公眾號(hào):云深之無(wú)跡】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
TimSort:一個(gè)在標(biāo)準(zhǔn)函數(shù)庫(kù)中廣泛使用的排序算法
一個(gè)月速成python+OpenCV圖像處理
![一個(gè)月速成<b class='flag-5'>python</b>+OpenCV圖像處理](https://file1.elecfans.com/web3/M00/00/7F/wKgZPGdJlvGANv8EAAAl3WfWse8658.png)
【「從算法到電路—數(shù)字芯片算法的電路實(shí)現(xiàn)」閱讀體驗(yàn)】+內(nèi)容簡(jiǎn)介
【「從算法到電路—數(shù)字芯片算法的電路實(shí)現(xiàn)」閱讀體驗(yàn)】+一本介紹基礎(chǔ)硬件算法模塊實(shí)現(xiàn)的好書
如何使用Python實(shí)現(xiàn)PID控制
采用pcm9211的模擬輸入1K的正弦波信號(hào),發(fā)現(xiàn)正弦波信號(hào)輸出半波了,請(qǐng)問需要怎么設(shè)置呢?
想通過使用AIC3254降噪,在purepath中的算法模塊中哪個(gè)比較合適呢?
利用Python腳本登錄到交換機(jī)并創(chuàng)建VLAN
Python建模算法與應(yīng)用
如何實(shí)現(xiàn)Python復(fù)制文件操作
利用Matlab函數(shù)實(shí)現(xiàn)深度學(xué)習(xí)算法
神經(jīng)網(wǎng)絡(luò)的基本原理及Python編程實(shí)現(xiàn)
FPGA能實(shí)現(xiàn)什么樣的算法?
PMSM控制利用foc算法,靜止?fàn)顟B(tài)下是如何啟動(dòng)的?
通過Python腳本實(shí)現(xiàn)WIFI密碼的自動(dòng)猜解
![通過<b class='flag-5'>Python</b>腳本<b class='flag-5'>實(shí)現(xiàn)</b>WIFI密碼的自動(dòng)猜解](https://file1.elecfans.com/web2/M00/BF/45/wKgaomWxzO6AGd6HAAAkwwGQtG8589.png)
評(píng)論