### 미분


```python
#수치미분
def f(x):
    return x**3-3*x**2+x

x=np.linspace(-1,3,400)
y=f(x)

from scipy.misc import derivative

print(derivative(f,0,dx=0.005)) #x=0인 지점에서 수치미분(간격:0.005)
```

    1.000025


### 심파이(Sympy)를 이용한 함수미분


```python
import sympy

# Juypter 노트북에서 수학식의 LaTeX 표현을 위해 필요함
sympy.init_printing(use_latex='mathjax')
```


```python
#심볼 변수 선언
x = sympy.symbols('x')
x
```




$\displaystyle x$




```python

f= x *sympy.exp(x)
f
```




$\displaystyle x e^{x}$




```python
#미분
sympy.diff(f)
```




$\displaystyle x e^{x} + e^{x}$




```python
#소인수분해 등 수식정리
sympy.simplify(sympy.diff(f))
```




$\displaystyle \left(x + 1\right) e^{x}$




```python
#함수선언
x, y = sympy.symbols('x y')
f = x ** 2 + 4 * x * y + 4 * y ** 2
f
```




$\displaystyle x^{2} + 4 x y + 4 y^{2}$




```python
#x에 대한 편미분
sympy.diff(f,x)
```




$\displaystyle 2 x + 4 y$




```python
#y에 대한 편미분
sympy.diff(f,y)
```




$\displaystyle 4 x + 8 y$



#### 정규분포의 확률밀도함수 미분하기


```python
x,mu,sigma = sympy.symbols('x mu sigma')
f = sympy.exp((x - mu) ** 2 / sigma ** 2)
f 
```




$\displaystyle e^{\frac{\left(- \mu + x\right)^{2}}{\sigma^{2}}}$




```python
sympy.diff(f,x)
```




$\displaystyle \frac{\left(- 2 \mu + 2 x\right) e^{\frac{\left(- \mu + x\right)^{2}}{\sigma^{2}}}}{\sigma^{2}}$




```python
sympy.simplify(sympy.diff(f, x))
```




$\displaystyle \frac{2 \left(- \mu + x\right) e^{\frac{\left(\mu - x\right)^{2}}{\sigma^{2}}}}{\sigma^{2}}$




```python
#이차도함수
sympy.diff(f,x,x)
```




$\displaystyle \frac{2 \left(1 + \frac{2 \left(\mu - x\right)^{2}}{\sigma^{2}}\right) e^{\frac{\left(\mu - x\right)^{2}}{\sigma^{2}}}}{\sigma^{2}}$




```python
#4.2.5
x= sympy.symbols('x')
f= x**3 -1
```


```python
sympy.diff(f)
```




$\displaystyle 3 x^{2}$




```python
x, k= sympy.symbols('x k')
f2=sympy.log(x**2-3*k)
sympy.diff(f2,x)
```




$\displaystyle \frac{2 x}{- 3 k + x^{2}}$




```python
x,a,b= sympy.symbols('x a b')
f3=sympy.exp(a*x**b)
sympy.diff(f3,x)
```




$\displaystyle \frac{a b x^{b} e^{a x^{b}}}{x}$



#### 4.2.6 다음함수에대한 1차/2차 편미분 구하기


```python
x,y= sympy. symbols('x y')
f=sympy.exp(x**2+2*y**2)
f
```




$\displaystyle e^{x^{2} + 2 y^{2}}$




```python
sympy.diff(f,x)
```




$\displaystyle 2 x e^{x^{2} + 2 y^{2}}$




```python
sympy.diff(f,y)
```




$\displaystyle 4 y e^{x^{2} + 2 y^{2}}$




```python
sympy.diff(f,x,x)
```




$\displaystyle 2 \left(2 x^{2} + 1\right) e^{x^{2} + 2 y^{2}}$




```python
sympy.diff(f,x,y)
```




$\displaystyle 8 x y e^{x^{2} + 2 y^{2}}$




```python
sympy.diff(f,y,x)  #슈와르츠 정리에 의해 diff(f,x,y)와 동일
```




$\displaystyle 8 x y e^{x^{2} + 2 y^{2}}$




```python
sympy.diff(f,y,y)
```




$\displaystyle 4 \left(4 y^{2} + 1\right) e^{x^{2} + 2 y^{2}}$



### 적분


```python
import sympy

sympy.init_printing(use_latex='mathjax')

x = sympy.symbols('x')
f = x * sympy.exp(x) + sympy.exp(x)
f

```




$\displaystyle x e^{x} + e^{x}$




```python
sympy.integrate(f)
```




$\displaystyle x e^{x}$




```python
x, y = sympy.symbols('x y')
f = 2 * x + y
f
```




$\displaystyle 2 x + y$




```python
sympy.integrate(f, x)
```




$\displaystyle x^{2} + x y$




```python
x, y = sympy.symbols('x y')
f= 1+ x*y
sympy.integrate(f,x)
```




$\displaystyle \frac{x^{2} y}{2} + x$




```python
f1= x*y*sympy.exp(x**2+y**2)
sympy.integrate(f1,x)
```




$\displaystyle \frac{y e^{x^{2} + y^{2}}}{2}$



#### 다차도함수와 다중적분


```python
#다음 부정적분을 구하라
x, y = sympy.symbols('x y')
f=x*y*sympy.exp(x**2+y**2)
sympy.integrate(f,x,y)
```




$\displaystyle \frac{e^{x^{2} + y^{2}}}{4}$



#### 정적분
- a,b구간 사이의 면적
-$$
\begin{align}
\int_{a}^{b} f(x) dx 
\tag{4.3.13}
\end{align}
$$


```python
x, y = sympy.symbols('x y')
f = x ** 3 - 3 * x ** 2 + x + 6
f
```




$\displaystyle x^{3} - 3 x^{2} + x + 6$




```python
# 부정 적분
F = sympy.integrate(f)
F
```




$\displaystyle \frac{x^{4}}{4} - x^{3} + \frac{x^{2}}{2} + 6 x$




```python
(F.subs(x, 2) - F.subs(x, 0)).evalf()
```




$\displaystyle 10.0$



#### 수치적분



```python
import scipy.integrate as integrate
def f(x):
    return x ** 3 - 3 * x ** 2 + x + 6


integrate.quad(f, 0, 2)  # 정적분 (수치적분)
```




$\displaystyle \left( 10.0, \  1.1102230246251565e-13\right)$



- 수치적분 결과값의 두번째 숫자는 오차의 상한값을 나타낸다

다음의 함수 수치이중적분하기
$$
\begin{align}
\int_0^{\infty} \int_1^{\infty} \dfrac{\exp(-xy)}{y^2} dx dy
\tag{4.3.20}
\end{align}
$$


```python
def f(x,y):
    return np.exp(-x*y)/y**2

integrate.dblquad(f,1,np.inf, lambda x:0, lambda x: np.inf)
```




$\displaystyle \left( 0.4999999999999961, \  1.068453874338024e-08\right)$




```python

```

c53613aeb5bf4ab0a1b158613d4604a0a472b4f1
ipynb
Jupyter Notebook
math/04_differentiative_sympy_integral.ipynb
dayoungMM/TIL
b844ef5621657908d4c256cdfe233462dd075e8b
MIT
math/04_differentiative_sympy_integral.ipynb
dayoungMM/TIL
b844ef5621657908d4c256cdfe233462dd075e8b
MIT
math/04_differentiative_sympy_integral.ipynb
dayoungMM/TIL
b844ef5621657908d4c256cdfe233462dd075e8b
MIT
Qwen/Qwen-72B
1. YES
2. YES