3

【数值积分】scipy.integrate

 2 years ago
source link: https://www.guofei.site/2017/06/06/scipyintegrate.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【数值积分】scipy.integrate

2017年06月06日

Author: Guofei

文章归类: 5-5-数值计算 ,文章编号: 7512


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2017/06/06/scipyintegrate.html

Edit

定积分

step1:生成数据

import numpy as np
def func(x):
    return np.sin(x**2)

import numpy as np
x=np.linspace(-1,1,1000)
y=func(x)

step2:计算定积分

  1. 矩形法计算定积分,最不准确但最方便的一种
    S1=(x[1]-x[0])*sum(y)
    
  2. 梯形法。比矩形法准一些
    S2=np.trapz(y,x)
  3. 貌似是simpson算法?
    from scipy import integrate
    S3,err=integrate.quad(func,-1,1)

二重积分dblquad()

例如,要算这个定积分:
∫0.5−0.5∫1−x2√−1−x2√sin((xy)2)dydx∫−0.50.5∫−1−x21−x2sin((xy)2)dydx

import numpy as np
from scipy import integrate

def fun(x,y):#被积函数
    return np.sin((x*y)**2)

def bound(x):#用来生成积分上界和下界
    return (1-x**2)**0.5

S,err=integrate.dblquad(fun,-0.5,0.5,lambda x:-bound(x),lambda x:bound(x))

或者另一种形式:

import numpy as np
from scipy import integrate


def fun(x,y):#被积函数
    return np.sin((x*y)**2)

def bound1(x):#积分上界
    return -(1-x**2)**0.5
def bound2(x):#积分下界
    return (1-x**2)**0.5

S,err=integrate.dblquad(fun,-0.5,0.5,bound1,bound2)

三重积分tplquad()

三重积分tplquad,非分方程odeint() 略


您的支持将鼓励我继续创作!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK