# integrate functions


```python
from scipy import integrate
import numpy as np
import matplotlib.pyplot as plt
import sympy as sy
sy.init_printing()
```


```python
a,b=-1,1
x=np.linspace(a,b,17)
y=np.exp(-x)
```


```python
val_trapz=integrate.trapz(y,x)
val_trapz
```


```python
val_simps=integrate.simps(y,x)
val_simps
```


```python
val_true=-np.exp(-b)+np.exp(-a)
```


```python
print(val_true-val_trapz)
```

    -0.00305962308718



```python
print(val_true-val_simps)
```

    -3.18201703609e-06



```python
x=np.linspace(a,b,1+2**4)
y=np.exp(-x)
dx=x[1]-x[0]
val_romb=integrate.romb(y,dx=dx)
val_romb
```


```python
print(val_true-val_romb)
```

    -4.20898871312e-11



```python
f=lambda x:np.exp(-x**2) * (x**12-x**5)
val,err=integrate.quad(f,0,np.inf)
val
```


```python
err
```

# 多重積分
$$\int_{x=a}^b \int_{y=g_1(x)}^{g_2(x)} f(x,y) dxdy$$
where $g_1=-1$ and $g_2=0$


```python
def f(x,y):
    return 4-x**2-y**2
a,b=0,1
g1,g2=lambda x:-1,lambda x:0
integrate.dblquad(f,a,b,g1,g2)
```


```python
val,err=integrate.dblquad(f,0,1,lambda x:x-1,lambda x:1-x)
val,err
```

$$\int_{a}^{b} \int_{y=g_1(x)}^{g_2(x)} \int_{z=h_1(x,y)}^{h_2(x,y)} f(x,y,z)dxdydz$$


```python
ff=lambda x,y,z:(x+y+z)**2
a,b=-1,1
g1,g2=lambda x:-1,lambda x:1
h1,h2=lambda x,y:-1,lambda x,y:1
var,err=integrate.tplquad(ff,a,b,g1,g2,h1,h2)
val
```


```python
val,err=integrate.nquad(f,[(-1,1),(-1,1),(-1,1)])
val
```


```python

```

97665a627d0e3554795621db2bf0f3685b28f4d3
ipynb
Jupyter Notebook
scipyExercise/integrate functions.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
scipyExercise/integrate functions.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
scipyExercise/integrate functions.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
Qwen/Qwen-72B
1. YES
2. YES