欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python用quad、dblquad實(shí)現(xiàn)一維二維積分的實(shí)例詳解

 更新時(shí)間:2019年11月20日 09:54:54   作者:潛水的飛魚(yú)baby  
今天小編大家分享一篇python用quad、dblquad實(shí)現(xiàn)一維二維積分的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

背景:

python函數(shù)庫(kù)scipy的quad、dblquad實(shí)現(xiàn)一維二維積分的范例。需要注意dblquad的積分順序問(wèn)題。

代碼:

import numpy as np
from scipy import integrate
 
 
def half_circle(x):
  """
  原心:(1,0),半徑為1
  半圓函數(shù):(x-1)^2+y^2 = 1
  """
  return (1-(x-1)**2)**0.5
 
"""
梯形法求積分:半圓線和x軸包圍的面積
"""
N = 10000
x = np.linspace(0,2,num=N)#,endpoint=True)
dh = (2-0)/N
y = half_circle(x)
"""
梯形法求積分:(上底+ 下底)*高/2
"""
S = sum((y[1:]+y[:-1])*dh/2)
 
print("=========%s=========="%"梯形法")
print("面積:%f"%S)
 
"""
直接調(diào)用intergrate的積分函數(shù)quad
"""
S2,err = integrate.quad(half_circle,0,2)
 
print("=========%s=========="%"quad")
print("面積:%f"%S2)
 
 
"""
多重定積分:注意積分順序
"""
def half_sphere(y,x):
  """
  球心:(1,0,0)
  半徑:1
  半球:(x-1)^2+y^2+z^2=1
  """
  return (1-(x-1)**2-y**2)**0.5
 
"""
積分順序:
v = V x in [0,2] :V y in [-g(x),h(x)]
"""
V3,err = integrate.dblquad(half_sphere,0,2,lambda x:-half_circle(x),lambda x:half_circle(x))
print("========%s==========="%"dblquad")
print("體積:%f"%V3)

結(jié)果:

========
=========梯形法==========
面積:1.570638
=========quad==========
面積:1.570796
========dblquad===========
體積:2.094395

以上這篇python用quad、dblquad實(shí)現(xiàn)一維二維積分的實(shí)例詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論