```python
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "last_expr"
import numpy as np
from matplotlib import pyplot as plt
```

# 变化率


对于函数 ${y = f(x)}$ 定义变化率为：

$$ roc = \frac{\Delta{y}}{\Delta{x}}  $$

代入函数曲线上的两点，可以计算 ${m}$ ：

\begin{equation}roc = \frac{f(x_{2}) - f(x_{1})}{x_{2} - x_{1}} \end{equation}

例如 ${f(x) = x^{2} + x}$


```python
def f(x):
    return (x)**2 + x

x = np.array(range(0, 11))
y = np.array([0,10])

plt.xlabel('x')
plt.ylabel('y')
plt.grid()

plt.plot(x, f(x), color='g')
plt.plot(y, f(y), color='m')

plt.show()
```

# 极限

例：
\begin{equation}\lim_{x \to 5} f(x)\end{equation}




```python
%matplotlib widget
x = [*range(0,5), *np.arange(4.25, 6, 0.25), *range(6, 11)]
y = [f(i) for i in x] 

plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()

plt.plot(x,y, color='lightgrey', marker='o', markeredgecolor='green', markerfacecolor='green')
plt.plot(5, f(5), color='red', marker='o', markersize=10)
plt.plot(5.25, f(5.25), color='blue', marker='<', markersize=10)
plt.plot(4.75, f(4.75), color='orange', marker='>', markersize=10)
plt.show()
```

##  连续性

对函数 ${g(x) = -(\frac{12}{2x})^{2},\;\; x \ne 0}$ 画出图形



```python
%matplotlib widget
def g(x):
    if x != 0:
        return -(12/(2*x))**2
x = range(-20, 21)
y = [g(a) for a in x]
plt.xlabel('x')
plt.ylabel('g(x)')
plt.grid()
plt.plot(x,y, color='g')
xy = (0,g(1))
plt.annotate('O',xy, xytext=(-0.7, -37),fontsize=14,color='b')
plt.show()
```

函数 ${h(x) = 2\sqrt{x},\;\; x \ge 0}$ 是不连续的。


```python
%matplotlib inline
def h(x):
    if x >= 0:
        import numpy as np
        return 2 * np.sqrt(x)

x = range(-20, 21)
y = [h(a) for a in x]

plt.xlabel('x')
plt.ylabel('h(x)')
plt.grid()

plt.plot(x,y, color='g')
plt.plot(0, h(0), color='g', marker='o', markerfacecolor='g', markersize=10)

plt.show()
```

函数 ${
k(x) = \begin{cases}
  x + 20, & \text{if } x \le 0, \\
  x - 100, & \text{otherwise }\end{cases}}$ 是不连续的。


```python
%matplotlib inline

def k(x):
    import numpy as np
    if x <= 0:
        return x + 20
    else:
        return x - 100

x1 = range(-20, 1)
x2 = range(1, 20)
y1 = [k(i) for i in x1]
y2 = [k(i) for i in x2]

plt.xlabel('x')
plt.ylabel('k(x)')
plt.grid()
plt.plot(x1,y1, color='g')
plt.plot(x2,y2, color='g')
plt.plot(0, k(0), color='g', marker='o', markerfacecolor='g', markersize=10)
plt.plot(0, k(0.0001), color='g', marker='o', markerfacecolor='w', markersize=10)

plt.show()
```

### 无穷

对 
$$d(x) = \frac{4}{x - 25},\;\; x \ne 25$$

当 ${x \to 25}$ 时:


```python
%matplotlib inline

def d(x):
    if x != 25:
        return 4 / (x - 25)

x = [*range(-100, 24), *np.arange(24.9, 25.2, 0.1), *range(26, 101)]
y = [d(i) for i in x]

plt.xlabel('x')
plt.ylabel('d(x)')
plt.grid()

plt.plot(x,y, color='purple')
plt.show()
```

\begin{equation}\lim_{x \to 25^{+}} d(x) = \infty \end{equation}

\begin{equation}\lim_{x \to 25^{-}} d(x) = -\infty \end{equation}

对函数 ${e(x) = \begin{cases}
  5, & \text{if } x = 0, \\
  1 + x^{2}, & \text{otherwise }
\end{cases}}$ 求 ${{x\to0}}$ 时的极限：

$$\lim_{x \to 0} a(x) \ne a(0) $$

$$\lim_{x \to 0} a(x) = 1 $$


```python
%matplotlib inline

def e(x):
    if x == 0:
        return 5
    else:
        return 1 + x**2

x= [-1, -0.5, -0.2, -0.1, -0.01, 0.01, 0.1, 0.2, 0.5, 1]
y =[e(i) for i in x]

plt.xlabel('x')
plt.ylabel('e(x)')
plt.grid()

plt.plot(x, y, color='m')
plt.scatter(0, e(0), color='m')
plt.plot(0, 1, color='m', marker='o', markerfacecolor='w', markersize=10)
plt.show()
```

对函数 

$$g(x) = \frac{x^{2} - 1}{x - 1}, x \ne 1$$

求 ${x \to 1}$ 时的极限：

\begin{equation}\lim_{x \to a} g(x) = \frac{(x-1)(x+1)}{x - 1}\end{equation}

\begin{equation}\lim_{x \to a} g(x)= x+1\end{equation}

\begin{equation}\lim_{x \to 1} g(x) = 2\end{equation}

类似的，对函数

\begin{equation}h(x) = \frac{\sqrt{x} - 2}{x - 4}, x \ne 4 \text{ and } x \ge 0\end{equation}

求 ${x \to 4}$ 时的极限:

\begin{equation}\lim_{x \to a}h(x) = \frac{1}{{\sqrt{x} + 2}}\end{equation}


\begin{equation}\lim_{x \to 4}h(x) = \frac{1}{{\sqrt{4} + 2}} = \frac{1}{4}\end{equation}

## 极限的算术运算

\begin{equation}\lim_{x \to a} (j(x) + l(x)) = \lim_{x \to a} j(x) + \lim_{x \to a} l(x)\end{equation}

\begin{equation}\lim_{x \to a} (j(x) - l(x)) = \lim_{x \to a} j(x) - \lim_{x \to a} l(x)\end{equation}

\begin{equation}\lim_{x \to a} (j(x) \cdot l(x)) = \lim_{x \to a} j(x) \cdot \lim_{x \to a} l(x)\end{equation}

\begin{equation}\lim_{x \to a} \frac{j(x)}{l(x)} = \frac{\lim_{x \to a} j(x)}{\lim_{x \to a} l(x)}\end{equation}

\begin{equation}\lim_{x \to a} (j(x))^{n} = \Big(\lim_{x \to a} j(x)\Big)^{n}\end{equation}

## 微分和求导

- 导函数的定义：

\begin{equation}\lim_{h \to 0} \frac{f(x + h) - f(x)}{h} \end{equation}

\begin{equation}f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h} \end{equation}

\begin{equation}\frac{d}{dx}f(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h} \end{equation}


```python
%matplotlib inline

def f(x):
    return x**2 + x

x = list(range(0, 11))
y = [f(i) for i in x]
x1 = 3
y1 = f(x1)
h = 3
x2 = x1+h
y2 = f(x2)

plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.plot(x,y, color='green')
plt.scatter(x1,y1, c='red')
plt.annotate('(x,f(x))',(x1,y1), xytext=(x1-0.5, y1+3))

plt.scatter(x2,y2, c='red')
plt.annotate('(x+h, f(x+h))',(x2,y2), xytext=(x2+0.5, y2))
plt.show()
```

#### 导函数也可记为

\begin{equation}f'(\textbf{a}) = \lim_{h \to 0} \frac{f(\textbf{a} + h) - f(\textbf{a})}{h} \Leftrightarrow f'(a) = \lim_{x \to a} \frac{f(x) - f(a)}{x - a} \end{equation}

### 求导数

\begin{equation}f(x) = x^{2} + x\end{equation}

\begin{equation}f'(a) = \lim_{h \to 0} \frac{f(a + h) - f(a)}{h} \end{equation}

\begin{equation}f'(\textbf{2}) = \lim_{h \to 0} \frac{f(\textbf{2} + h) - f(\textbf{2})}{h} \end{equation}

\begin{equation}f'(2) = \lim_{h \to 0} \frac{((2+h)^{2} + 2 + h) - (2^{2} + 2)}{h} \end{equation}

\begin{equation}f'(2) = \lim_{h \to 0} \frac{(h^{2} + 5h + 6) - 6}{h} = \lim_{h \to 0} \frac{h^{2} + 5h}{h}  = \lim_{h \to 0} h + 5 = 5\end{equation}


```python
%matplotlib widget

def f(x):
    return x**2 + x

x = list(range(0, 11))
y = [f(i) for i in x]

x1 = 2
y1 = f(x1)

m = 5

plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()

plt.plot(x,y, color='green')
plt.scatter(x1,y1, c='red')
plt.annotate('(x,f(x))',(x1,y1), xytext=(x1-0.5, y1+3))

xMin = x1 - 5
yMin = y1 - (5*m)
xMax = x1 + 5
yMax = y1 + (5*m)
plt.plot([xMin,xMax],[yMin,yMax], color='magenta')

plt.show()
```

### 求导函数

\begin{equation}f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h} \end{equation}

\begin{equation}f'(x) = \lim_{h \to 0} \frac{((x+h)^{2} + x + h) - (x^{2} + x)}{h} \end{equation}

\begin{equation}f'(x) = \lim_{h \to 0} \frac{x^{2} + h^{2} + 2xh + x + h - x^{2} - x}{h} \end{equation}

\begin{equation}f'(x) = \lim_{h \to 0} \frac{h^{2} + 2xh + h}{h} \end{equation}

\begin{equation}f'(x) = \lim_{h \to 0} 2x + h + 1 \end{equation}

\begin{equation}f'(x) = 2x + 1 \end{equation}

## 可微

并不是所有的函数都是可以求导函数的。

可微的函数有下面的几何特征
- 连续
- 切线不竖直
- 光滑


\begin{equation}
q(x) = \begin{cases}
  \frac{40,000}{x^{2}}, & \text{if } x < -4, \\
  (x^{2} -2) \cdot (x - 1), & \text{if } x \ne 0 \text{ and } x \ge -4 \text{ and } x < 8, \\
  (x^{2} -2), & \text{if } x \ne 0 \text{ and } x \ge 8
\end{cases}
\end{equation}


```python
%matplotlib inline

def q(x):
    if x != 0:
        if x < -4:
            return 40000 / (x**2)
        elif x < 8:
            return (x**2 - 2) * x - 1
        else:
            return (x**2 - 2)

x = [*range(-10, -5), -4.01]
x2 = [*range(-4, 8), 7.9999, *range(8, 11)]

y = [q(i) for i in x]
y2 = [q(i) for i in x2]

plt.xlabel('x')
plt.ylabel('q(x)')
plt.grid()

plt.plot(x,y, color='purple')
plt.plot(x2,y2, color='purple')
plt.scatter(-4,q(-4), c='red')
plt.annotate('A (x= -4)',(-5,q(-3.9)), xytext=(-7, q(-3.9)))
plt.scatter(0,0, c='red')
plt.annotate('B (x= 0)',(0,0), xytext=(-1, 40))
plt.scatter(8,q(8), c='red')
plt.annotate('C (x= 8)',(8,q(8)), xytext=(8, 100))

plt.show()
```

- A不连续
- B不连续
- C不光滑

## 求导规则

### 基本规则
\begin{equation}f(x) = \pi \;\; \therefore \;\; f'(x) = 0 \end{equation}
    
\begin{equation}f(x) = 2g(x) \;\; \therefore \;\; f'(x) = 2g'(x) \end{equation}

\begin{equation}f(x) = g(x) + h(x) \;\; \therefore \;\; f'(x) = g'(x) + h'(x) \end{equation}

\begin{equation}f(x) = k(x) - l(x) \;\; \therefore \;\; f'(x) = k'(x) - l'(x) \end{equation}

\begin{equation}\frac{d}{dx}(2x + 6) = \frac{d}{dx} 2x +  \frac{d}{dx} 6 = 2\end{equation}

### 幂规则
> 极其常用的规则
\begin{equation}f(x) = x^{n} \;\; \therefore \;\; f'(x) = nx^{n-1}\end{equation}

例如：

\begin{equation}f(x) = x^{3} \;\; \therefore \;\; f'(x) = 3x^{2}\end{equation}

\begin{equation}f(x) = x^{-2} \;\; \therefore \;\; f'(x) = -2x^{-3}\end{equation}

\begin{equation}f(x) = x^{2} \;\; \therefore \;\; f'(x) = 2x\end{equation}

### 幂规则的推导

\begin{equation}f(x) = x^{2}\end{equation}

\begin{equation}f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h} \end{equation}

\begin{equation}f'(x) = \lim_{h \to 0} \frac{(x + h)^{2} - x^{2}}{h} \end{equation}

\begin{equation}f'(x) = \lim_{h \to 0} \frac{x^{2} + h^{2} + 2xh - x^{2}}{h} = \lim_{h \to 0} \frac{h^{2} + 2xh}{h} = \lim_{h \to 0} h + 2x = 2x\end{equation}

### 乘法规则

\begin{equation}\frac{d}{dx}[f(x)g(x)] = f'(x)g(x) + f(x)g'(x) \end{equation}

例如

\begin{equation}f(x) = 2x^{2} \end{equation}

\begin{equation}g(x) = x + 1 \end{equation}

\begin{equation}f'(x) = 4x \end{equation}

\begin{equation}g'(x) = 1 \end{equation}

\begin{equation}\frac{d}{dx}[f(x)g(x)] = (4x \cdot (x + 1)) + (2x^{2} \cdot 1) \end{equation}

\begin{equation}\frac{d}{dx}[f(x)g(x)] = 6x^{2} + 4x \end{equation}

### 商数规则
\begin{equation}r(x) = \frac{s(x)}{t(x)} \end{equation}

\begin{equation}r'(x) = \frac{s'(x)t(x) - s(x)t'(x)}{（t(x)）^{2}} \end{equation}

例如

\begin{equation}s(x) = 3x^{2} \end{equation}

\begin{equation}t(x) = 2x\end{equation}

\begin{equation}r'(x) = \frac{(6x \cdot 2x) - (3x^{2} \cdot 2)}{（2x）^{2}} = \frac{6x^{2}}{4x^{2}} = \frac{3}{2}\end{equation}

### 链式规则 

\begin{equation}\frac{d}{dx}[o(i(x))] = o'(i(x)) \cdot i'(x)\end{equation}

例如

\begin{equation}i(x) = x^{2} \end{equation}

\begin{equation}o(x) = 2x \end{equation}

\begin{equation}o'(x) = 2,  i'(x) = 2x\end{equation}

\begin{equation}\frac{d}{dx}[o(i(x))] = 4x\end{equation}

# 极值和优化

对函数${k(x) = -10x^{2} + 100x + 3}$ 有导函数：

\begin{equation}k'(x) = -20x + 100 \end{equation}


```python
%matplotlib inline

def k(x):
    return -10*(x**2) + (100*x)  + 3

def kd(x):
    return -20*x + 100

x = list(range(0, 11))
y = [k(i) for i in x]

yd = [kd(i) for i in x]

plt.axhline()
plt.axvline()
plt.xlabel('x (time in seconds)')
plt.ylabel('k(x) (height in feet)')
plt.xticks(range(0,15, 1))
plt.yticks(range(-200, 500, 20))
plt.grid()

plt.plot(x,y, color='green')

plt.plot(x,yd, color='purple')

x1 = 2
x2 = 5
x3 = 8
plt.plot([x1-1,x1+1],[k(x1)-(kd(x1)),k(x1)+(kd(x1))], color='r')
plt.plot([x2-1,x2+1],[k(x2)-(kd(x2)),k(x2)+(kd(x2))], color='r')
plt.plot([x3-1,x3+1],[k(x3)-(kd(x3)),k(x3)+(kd(x3))], color='r')

plt.show()
```

## 找最大值和最小值

对 ${k(x) = -10x^{2} + 100x + 3}$ 有 ${k'(x) = -20x + 100 }$

\begin{equation}-20x + 100 = 0 \end{equation}

\begin{equation}x = 5 \end{equation}

如果求二阶导数

\begin{equation}k'(x) = -20x + 100 \Rightarrow k''(x) = -20\end{equation}

根据二阶导数为常量，而且是负常量，得知一阶导函数是线性下降的。导函数为0的点就是极大值。

函数
\begin{equation}w(x) = x^{2} + 2x + 7 \end{equation}


```python
%matplotlib inline

def w(x):
    return (x**2) + (2*x) + 7

def wd(x):
    return 2*x + 2

x = list(range(-10, 11))
y = [w(i) for i in x]

yd = [wd(i) for i in x]

plt.axhline()
plt.axvline()
plt.xlabel('x')
plt.ylabel('w(x)')
plt.xticks(range(-10,15, 1))
plt.yticks(range(-200, 500, 20))
plt.grid()

plt.plot(x,y, color='g')
plt.plot(x,yd, color='m')
plt.show()
```

## 极值点

对函数
\begin{equation}v(x) = x^{3} - 2x + 100 \end{equation}


```python
%matplotlib widget

def v(x):
    return (x**3) - (2*x) + 100

def vd(x):
    return 3*(x**2) - 2

x = list(range(-10, 11))
y = [v(i) for i in x]

yd = [vd(i) for i in x]

plt.axhline()
plt.axvline()
plt.xlabel('x')
plt.ylabel('v(x)')
plt.xticks(range(-10,15, 1))
plt.yticks(range(-1000, 2000, 100))
plt.grid()
plt.plot(x,y, color='g')
plt.plot(x,yd, color='m')
plt.show()
```


```python
%matplotlib inline

def k(x):
    return -10*(x**2) + (100*x)  + 3

def kd(x):
    return -20*x + 100

def k2d(x):
    return -20

plt.axhline()
plt.axvline()
x = list(range(0, 11))
y = [k(i) for i in x]
yd = [kd(i) for i in x]
y2d = [k2d(i) for i in x]

plt.xlabel('x')
plt.ylabel('k(x)')
plt.xticks(range(0,15, 1))
plt.yticks(range(-200, 500, 20))
plt.grid()
plt.plot(x,y, color='g')
plt.plot(x,yd, color='r')
plt.plot(x,y2d, color='b')

plt.show()
```

练习：

\begin{equation}w(x) = x^{2} + 2x + 7 \end{equation}


```python
%matplotlib inline

def w(x):
    return (x**2) + (2*x) + 7

def wd(x):
    return 2*x + 2

def w2d(x):
    return 2

x = list(range(-10, 11))
y = [w(i) for i in x]
yd = [wd(i) for i in x]
y2d = [w2d(i) for i in x]

plt.axhline()
plt.axvline()
plt.xlabel('x (time in days)')
plt.ylabel('w(x) (flowers)')
plt.xticks(range(-10,15, 1))
plt.yticks(range(-200, 500, 20))
plt.grid()
plt.plot(x,y, color='green')
plt.plot(x,yd, color='purple')
plt.plot(x,y2d, color='magenta')

plt.show()
```

## 极值点不一定是最大值或者最小值
 
\begin{equation}v(x) = x^{3} - 6x^{2} + 12x + 2 \end{equation}
 
\begin{equation}v'(x) = 3x^{2} - 12x + 12 = 0\end{equation}
 
${x = 2}$ 是极值点


```python
%matplotlib inline

def v(x):
    return (x**3) - (6*(x**2)) + (12*x) + 2

def vd(x):
    return (3*(x**2)) - (12*x) + 12

def v2d(x):
    return (3*(2*x)) - 12

from matplotlib import pyplot as plt

x = list(range(-5, 11))
y = [v(i) for i in x]
yd = [vd(i) for i in x]
y2d = [v2d(i) for i in x]


plt.xlabel('x')
plt.ylabel('v(x)')
plt.xticks(range(-10,15, 1))
plt.yticks(range(-2000, 2000, 50))
plt.grid()

plt.plot(x,y, color='green')
plt.plot(x,yd, color='purple')
plt.plot(x,y2d, color='magenta')
plt.show()

print ("v(2) = " + str(v(2)))

print ("v'(2) = " + str(vd(2)))

print ("v''(2) = " + str(v2d(2)))

```

## 优化算法

- 求一阶导数
- 求极值
- 求二阶导数检查是否为最大值

# 偏微分

对多变量函数

$$f(x,y) = x^2 + y^2$$

对于变量x, 定义

$$\frac{\partial f(x,y)}{\partial x} = \frac{\partial (x^2 + y^2)}{\partial x}$$

$$\frac{\partial x^2}{\partial x} = 2x，\;\;\; \frac{\partial y^2}{\partial x} = 0$$

$$\frac{\partial f(x,y)}{\partial x} = 2x + 0 = 2x$$

对于变量y

$$\frac{\partial f(x,y)}{\partial y} = 0 + 2y = 2y$$

## 计算梯度

> 梯度是多唯曲面的切面

$$\frac{\partial f(x,y)}{\partial x} = 2x \\
\frac{\partial f(x,y)}{\partial y} = 2y$$

梯度向量：
$$grad(f(x,y)) =  \vec{g(x,y)} = \begin{bmatrix}\frac{\partial f(x,y)}{\partial x} \\ \frac{\partial f(x,y)}{\partial y} \end{bmatrix} = \begin{bmatrix}2x \\ 2y \end{bmatrix} $$


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

el = np.arange(-5,6)
nx, ny = np.meshgrid(el, el, sparse=False, indexing='ij')

x_coord = []
y_coord = []
z = []
for i in range(11):  
    for j in range(11):
        x_coord.append(float(-nx[i,j]))
        y_coord.append(float(-ny[i,j]))       
        z.append(nx[i,j]**2 + ny[i,j]**2)

x_grad = [-2 * x for x in x_coord]
y_grad = [-2 * y for y in y_coord] 

plt.xlim(-5.5,5.5)
plt.ylim(-5.5,5.5)
for x, y, xg, yg in zip(list(x_coord), list(y_coord), list(x_grad), list(y_grad)):
    if x != 0.0 or y != 0.0: ## Avoid the zero divide when scaling the arrow
        l = math.sqrt(xg**2 + yg**2)/2.0
        plt.quiver(x, y, xg, yg, width = l, units = 'dots')

z = np.array(z).reshape(11,11)    
plt.contour(el, el, z)    
```

图例
- 箭头是梯度方向
- 箭头宽度代表大小
- 梯度方向是垂直于曲面的


## 梯度下降算法

1. 从一个起点开始。
2. 计算这点的梯度。
3. 沿着梯度方向探索一步。
4. 判断梯度是否接近零，如果是，停止。
5. 否则，从第2步继续。

# 积分

对函数 ${f(x)=x}$ 有：

$$\int f(x)\;dx = \frac{1}{2} x^2$$

$$\int_0^2 f(x)\;dx = \frac{1}{2} x^2\ \big|_0^2 = \frac{4}{2} - \frac{0}{2} = 2$$


```python
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()

def f(x):
    return x

x = range(0, 11)
y = [f(a) for a in x]

plt.plot(x,y, color='purple')

section = np.arange(0, 2, 1/20)
plt.fill_between(section,f(section), color='orange')

plt.show()
```


```python
import scipy.integrate as integrate
i, e = integrate.quad(lambda x: f(x), 0, 2)
print (i)
```

    2.0


$$\int_0^3 3x^2 + 2x + 1\;dx = \frac{3}{3} x^3 + \frac{2}{2} x^2 + x\ \big|_0^3 
= 27 + 9 + 3 + 0 + 0 + 0
= 39$$ 


```python
from matplotlib.patches import Polygon


def g(x):
    return 3 * x**2 + 2 * x + 1

x = range(0, 11)
y = [g(a) for a in x]

fig, ax = plt.subplots()
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()

plt.plot(x,y, color='purple')
ix = np.linspace(0, 3)
iy = g(ix)
verts = [(0, 0)] + list(zip(ix, iy)) + [(3, 0)]
poly = Polygon(verts, facecolor='orange')
ax.add_patch(poly)

plt.show()
```


```python
i, e = integrate.quad(lambda x: 3 * x**2 + 2 * x + 1, 0, 3)
print(i)
print(e)
```

    38.99999999999999
    4.3298697960381095e-13


$$\int^{\infty}_0 e^{-5x} dx$$


```python
import numpy as np
i, e = integrate.quad(lambda x: np.exp(-x*5), 0, np.inf)

print('Integral: ' + str(i))
print('Absolute Error: ' + str(e))
```

    Integral: 0.20000000000000007
    Absolute Error: 1.560666811361375e-11


$$\int_{-\infty}^{\infty} \frac{1}{2 \pi} e^{\frac{-x^2}{\sqrt(2 \pi)}} dx$$


```python
import numpy as np
norms = lambda x: np.exp(-x**2/2.0)/np.sqrt(2.0 * 3.14159)
i, e = integrate.quad(norms, -np.inf, np.inf)

print('Integral: ' + str(i))
print('Absolute Error: ' + str(e))
```

    Integral: 1.0000004223321999
    Absolute Error: 1.0178195684846592e-08


b10d4b03f49bcc2621ce0d79573c474a71301b96
ipynb
Jupyter Notebook
基础教程/A1-Python与基础知识/数学基础/02_微积分.ipynb
microsoft/ai-edu
2f59fa4d3cf19f14e0b291e907d89664bcdc8df3
Apache-2.0
2019-05-07T02:48:50.000Z
2022-03-31T08:49:42.000Z
基础教程/A1-Python与基础知识/数学基础/02_微积分.ipynb
microsoft/ai-edu
2f59fa4d3cf19f14e0b291e907d89664bcdc8df3
Apache-2.0
2019-05-13T15:07:19.000Z
2022-03-23T08:52:32.000Z
基础教程/A1-Python与基础知识/数学基础/02_微积分.ipynb
microsoft/ai-edu
2f59fa4d3cf19f14e0b291e907d89664bcdc8df3
Apache-2.0
2019-05-07T02:55:15.000Z
2022-03-30T06:56:52.000Z
Qwen/Qwen-72B
1. YES
2. YES