```python
import numpy as np
import scipy
import sympy as sym
import matplotlib
sym.init_printing()
```

## Integrate


```python
x = sym.symbols('x')
a = sym.Integral(sym.cos(x)*sym.exp(x), x)
sym.Eq(a, a.doit())
```

## Derivative


```python
y = x**2+4*x
y
```


```python
sym.plot(y, y.diff(x,1), y.diff(x, 2))
```


```python
a = sym.symbols('a')
x = sym.symbols('x')
y = sym.Function('y')(x)
z = sym.Function('z')(y)
sym.Eq(z.diff(y)*y.diff(x)*a, z.diff(x))
```


```python
x, y = sym.symbols('x y')
z = x**2+2*x+y**2+4*y+4
dzx = z.diff(x)
dzy = z.diff(y)
sym.plotting.plot3d(z, (x, -10, 10), (y, -10, 10))
sym.plotting.plot3d(sym.sqrt(dzx**2 +  dzy**2), (x, -10, 10), (y, -10, 10))
```


```python
x, y = sym.symbols('x y')
z = x**2+2*x+y**2+4*y+4
z
```


```python
(z.diff(x),z.diff(y))
```

$$lim_{x\rightarrow 0}{\frac{sin(x)}{x}}$$


```python
x, y = sym.symbols('x y')
sym.limit(sym.sin(x)/x,x,0)
```

## Series


```python
x, y = sym.symbols('x y')
z=sym.series((x-3)*(x+1)*(x+4)*(x-4)*x,x,x0=0,n=4)
z
```


```python
sym.plot(48*x,32*x**2,-19*x**3,(x,-4,4))
sym.plot(48*x+32*x**2-19*x**3,(x-3)*(x+1)*(x+4)*(x-4)*x,(x,-4,4))
```

## 三角函数与傅里叶变换


```python
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.style.use('seaborn-darkgrid')
x = np.linspace(-np.pi,np.pi,2000)
y = np.ones_like(x)
dx = np.mean(x[1:]-x[:-1])
y[:1000]=-1
plt.figure(1)
plt.plot(x, y, lw=2, alpha=.6)
plt.xlim([-1.2,1.2])

def par(n):
    an=1/np.pi*np.trapz(y*np.cos(x*n),dx=dx)
    bn=1/np.pi*np.trapz(y*np.sin(x*n),dx=dx)
    return an,bn
plt.figure(2)
for itr in range(20):
    an, bn = par(itr)
    plt.plot(x,an*np.cos(itr*x), lw=3, alpha=.5)
    plt.plot(x,bn*np.sin(itr*x), lw=3, alpha=.5)
plt.figure(3)
plt.plot(x, y, lw=2, alpha=.6)
sums = np.zeros_like(x)
for itr in range(20):
    an, bn = par(itr)
    sums[:] += an*np.cos(itr*x)+bn*np.sin(itr*x)
    plt.plot(x,sums, lw=3, alpha=.3)
plt.show()

    
```

## 曲线拟合


```python
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
import matplotlib as mpl
mpl.style.use('seaborn-darkgrid')
```


```python
#待拟合的函数，x是变量，p是参数
def func(x, p):
    a, b, c, d, e, f = p
    return a+b*x+c*x**2+d*x**3+e*x**4+f*x**5

#计算真实数据和拟合数据之间的误差，p是待拟合的参数，x和y分别是对应的真实数据
def error(p, x, y):
    return func(x, p) - y

#一组真实数据，在a=2, b=1的情况下得出
x1 = np.array([1, 2, 3, 4, 5, 6], dtype=float)
y1 = np.array([3, 2, 7, 9, 6, 13], dtype=float)

#调用拟合函数，第一个参数是需要拟合的差值函数，第二个是拟合初始值，第三个是传入函数的其他参数
r = leastsq(error, [1, 1, 1, 1, 1, 1], args=(x1, y1))

#打印结果，r[0]存储的是拟合的结果，r[1]、r[2]代表其他信息
x = np.linspace(1,6,1000)
y = func(x, r[0])
plt.plot(x,y, lw=2, alpha=0.4)
plt.scatter(x1, y1)
plt.show()
```


```python

```

ca0ff4091b25cdff9d32d5d4658626af2d070411
ipynb
Jupyter Notebook
math_slides/Chapter 3 - Function and derivative.ipynb
ningyunziying/tensorflow
106f7d3ffdebe2f168fb811b39a98ed48b4fe984
Apache-2.0
math_slides/Chapter 3 - Function and derivative.ipynb
ningyunziying/tensorflow
106f7d3ffdebe2f168fb811b39a98ed48b4fe984
Apache-2.0
math_slides/Chapter 3 - Function and derivative.ipynb
ningyunziying/tensorflow
106f7d3ffdebe2f168fb811b39a98ed48b4fe984
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES