matlab利用求驻点偏导的方法求解多元函数极值,给我一份完整的例题和matlab代码
时间: 2024-02-06 18:10:52 浏览: 175
用MATLAB求极值
4星 · 用户满意度95%
好的,下面给你一个例题和对应的 Matlab 代码:
假设有一个多元函数 f(x,y) = 2x^3 - 3x^2y - 12x + 8y^3 + 18y^2 + 5,求该函数的极值。
解法:首先求函数的一阶偏导数和二阶偏导数,并令其为 0,求出方程组的解即为函数的驻点。
$$
\begin{cases}
\frac{\partial f}{\partial x} = 6x^2 - 6xy - 12 = 0 \\
\frac{\partial f}{\partial y} = -3x^2 + 24y^2 + 36y = 0 \\
\frac{\partial^2 f}{\partial x^2} = 12x - 6y \\
\frac{\partial^2 f}{\partial x \partial y} = -6x \\
\frac{\partial^2 f}{\partial y^2} = 48y + 36 \\
\end{cases}
$$
解方程组得到驻点 $(2,2)$,然后利用二阶偏导数判断其是极大值还是极小值。
$$
\begin{vmatrix}
12x-6y & -6x \\
-6x & 48y+36
\end{vmatrix}
= (12x - 6y)(48y + 36) - (-6x)(-6x)
= 432x^2 - 144x^2 - 216xy - 216x^2
= 72x^2 - 216xy - 216x^2
= -144x^2 - 216xy
$$
因为 $\frac{\partial^2 f}{\partial x^2}(2,2) < 0$,所以 $(2,2)$ 是极大值点。
Matlab 代码:
```matlab
syms x y;
f = 2*x^3 - 3*x^2*y - 12*x + 8*y^3 + 18*y^2 + 5;
dfdx = diff(f, x);
dfdy = diff(f, y);
d2fdx2 = diff(f, x, 2);
d2fdy2 = diff(f, y, 2);
d2fdxdy = diff(f, x, y);
[solx, soly] = solve(dfdx == 0, dfdy == 0, x, y);
hessian = [d2fdx2, d2fdxdy; d2fdxdy, d2fdy2];
if eval(d2fdx2) < 0
disp("The stationary point (" + solx + ", " + soly + ") is a local maximum");
elseif eval(d2fdx2) > 0
disp("The stationary point (" + solx + ", " + soly + ") is a local minimum");
else
disp("The test is inconclusive");
end
```
输出结果:
```
The stationary point (2, 2) is a local maximum
```
阅读全文