网络编程
位置:首页>> 网络编程>> Python编程>> python用quad、dblquad实现一维二维积分的实例详解

python用quad、dblquad实现一维二维积分的实例详解

作者:潜水的飞鱼baby  发布时间:2022-02-17 05:32:51 

标签:python,一维,二维,积分

背景:

python函数库scipy的quad、dblquad实现一维二维积分的范例。需要注意dblquad的积分顺序问题。

代码:


import numpy as np
from scipy import integrate

def half_circle(x):
 """
 原心:(1,0),半径为1
 半圆函数:(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)

"""
直接调用intergrate的积分函数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)

结果:

========
=========梯形法==========
面积:1.570638
=========quad==========
面积:1.570796
========dblquad===========
体积:2.094395

来源:https://blog.csdn.net/u011702002/article/details/78084245

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com