Python實(shí)現(xiàn)數(shù)值積分方式
原理:
利用復(fù)化梯形公式,復(fù)化Simpson公式,計(jì)算積分。
步驟:

import math
"""測(cè)試函數(shù)"""
def f(x,i):
if i == 1:
return (4 - (math.sin(x)) ** 2) ** 0.5
if i == 2:
if x == 0:
return 1
else:
return math.sin(x) / x
if i == 3:
return (math.exp(x)) / (4 + x ** 2)
if i == 4:
return math.log(1+x,math.e) / (1 + x ** 2)
"""打印顯示函數(shù)"""
def p(i,n):
return "第" + str(i) + "題,n=" + str(n) + "時(shí)的積分值為:"
"""復(fù)化Simpson函數(shù)"""
def Simpson(a, b, n, i):
h = (b - a) / (2 * n)
F0 = f(a,i) + f(b,i)
F1 = 0
F2 = 0
for j in range(1,2 * n):
x = a + (j * h)
if j % 2 == 0:
F2 = F2 + f(x,i)
else:
F1 = F1 + f(x,i)
SN = (h * (F0 + 2 * F2 + 4 * F1)) / 3
print("復(fù)化Simpson函數(shù)" + p(i,n) + str("%-10.7f"%(SN)))
return SN
def T(a, b, n, i):
h = (b - a) / n
F0 = f(a,i) + f(b,i)
F = 0
for j in range(1,n):
x = a + (j * h)
F = F + f(x,i)
SN = (h * (F0 + 2 * F)) / 2
print("復(fù)化梯形函數(shù)" + p(i,n) + str("%-10.7f"%(SN)))
return SN
def SimpsonTimes(x):
n = 1
y = Simpson(0, math.pi/4, n, 1)
while(abs(y - 1.5343916) > x):
n = n + 1
y = Simpson(0, math.pi/4, n, 1)
else:
return n
def Times(x):
n = 1
y = T(0, math.pi/4, n, 1)
while(abs(y - 1.5343916) > x):
n = n + 1
y = T(0, math.pi/4, n, 1)
else:
return n
"""
測(cè)試部分
"""
Simpson(0, math.pi/4, 10, 1)
Simpson(0, 1, 10, 2)
Simpson(0, 1, 10, 3)
Simpson(0, 1, 10, 4)
Simpson(0, math.pi/4, 20, 1)
Simpson(0, 1, 20, 2)
Simpson(0, 1, 20, 3)
Simpson(0, 1, 20, 4)
T(0, math.pi/4, 10, 1)
T(0, 1, 10, 2)
T(0, 1, 10, 3)
T(0, 1, 10, 4)
T(0, math.pi/4, 20, 1)
T(0, 1, 20, 2)
T(0, 1, 20, 3)
T(0, 1, 20, 4)
print("復(fù)化梯形函數(shù)求解第一問(wèn),精度為0.00001時(shí)需要" + str(Times(0.00001)) + "個(gè)步數(shù)")
print("復(fù)化Simpson函數(shù)求解第一問(wèn),精度為0.00001時(shí)需要" + str(SimpsonTimes(0.00001)) + "個(gè)步數(shù)")
print("復(fù)化梯形函數(shù)求解第一問(wèn),精度為0.000001時(shí)需要" + str(Times(0.000001)) + "個(gè)步數(shù)")
print("復(fù)化Simpson函數(shù)求解第一問(wèn),精度為0.000001時(shí)需要" + str(SimpsonTimes(0.000001)) + "個(gè)步數(shù)")
以上這篇Python實(shí)現(xiàn)數(shù)值積分方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python2.7安裝opencv-python很慢且總是失敗問(wèn)題
這篇文章主要介紹了python2.7安裝opencv-python很慢且總是失敗問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
python中cv2.imdecode()與cv2.imencode()的使用小結(jié)
本文介紹了cv2.imencode()和cv2.imdecode()函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
python結(jié)合shell自動(dòng)創(chuàng)建kafka的連接器實(shí)戰(zhàn)教程
這篇文章主要介紹了python結(jié)合shell自動(dòng)創(chuàng)建kafka的連接器,需要安裝連接oracle的python包,獲取oracle表信息,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
python matplotlib畫(huà)盒圖、子圖解決坐標(biāo)軸標(biāo)簽重疊的問(wèn)題
今天小編就為大家分享一篇python matplotlib畫(huà)盒圖、子圖解決坐標(biāo)軸標(biāo)簽重疊的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-03-03
詳解使用python的logging模塊在stdout輸出的兩種方法
這篇文章主要介紹了詳解使用python的logging模塊在stdout輸出的相關(guān)資料,需要的朋友可以參考下2017-05-05
pyinstaller打包可執(zhí)行文件出現(xiàn)KeyError的問(wèn)題
這篇文章主要介紹了pyinstaller打包可執(zhí)行文件出現(xiàn)KeyError的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
10個(gè)python爬蟲(chóng)入門(mén)基礎(chǔ)代碼實(shí)例 + 1個(gè)簡(jiǎn)單的python爬蟲(chóng)完整實(shí)例
這篇文章主要介紹了10個(gè)python爬蟲(chóng)入門(mén)基礎(chǔ)代碼實(shí)例和1個(gè)簡(jiǎn)單的python爬蟲(chóng)爬蟲(chóng)貼吧圖片的實(shí)例,需要的朋友可以參考下2020-12-12

