【Python讲优化】S06E03 详解多元函数和偏导数的概念

### 1.函数：从一元到多元

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

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

### 2.二元函数的可视化

#### 2.1.函数图像的绘制

from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

x = np.arange(-4, 4, 0.01)
y = np.arange(-4, 4, 0.01)
x, y = np.meshgrid(x, y)
z = x-(1./9)*x**3-(1./2)*y**2

ax.plot_surface(x, y, z, cmap=plt.cm.Blues_r)
plt.show()

#### 2.2.等高线图的绘制

import numpy as np
import matplotlib.pyplot as plt

def f(x, y):
return x-(1./9)*x**3-(1./2)*y**2

x = np.arange(-4, 4, 0.01)
y = np.arange(-4, 4, 0.01)

#把x,y数据生成mesh网格状的数据
X, Y = np.meshgrid(x, y)

#填充等高线间的颜色
plt.contourf(X, Y, f(X, Y), 24, cmap=plt.cm.hot)
#添加等高线
C = plt.contour(X, Y, f(X, Y), 24)
#增加各等高线的高度值
plt.clabel(C, inline=True, fontsize=12)

plt.show()

### 3.多元函数的偏导数

#### 3.1.概念的引入

$f_y(x_0,y_0)=lim_{\Delta y\rightarrow 0}\frac{f(x_0,y_0+\Delta y)-f(x_0,y_0)}{\Delta y}$

#### 3.2.偏导数的几何意义

`
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

# 多元函数z=f(x,y)

def f(x, y):
return x-(1./9)x3-(1./2)y2