Matlab 实现牛顿法求解函数100*(x-y^2)^2+(x-1)^2最小值
时间: 2024-02-29 12:51:32 浏览: 127
要求函数 $f(x,y) = 100(x-y^2)^2 + (x-1)^2$ 的最小值,可以先求出其一阶偏导数和二阶偏导数:
$$
\begin{aligned}
\frac{\partial f}{\partial x} &= 400x^3 - 400xy^2 + 2x - 2 \\
\frac{\partial f}{\partial y} &= -400xy + 200y^3 \\
\frac{\partial^2 f}{\partial x^2} &= 1200x^2 - 400y^2 + 2 \\
\frac{\partial^2 f}{\partial x \partial y} &= -400x \\
\frac{\partial^2 f}{\partial y^2} &= -400x + 600y^2
\end{aligned}
$$
由于二阶偏导数存在,牛顿法求解适用。以下是使用 Matlab 实现的程序:
```matlab
syms x y;
f = 100*(x-y^2)^2 + (x-1)^2;
df = [diff(f, x); diff(f, y)]; % 一阶偏导数
d2f = [diff(df, x) diff(df, y)]; % 二阶偏导数
% 初始化
x0 = [0; 0];
tol = 1e-6;
maxiter = 1000;
% 牛顿法求解
iter = 0;
x = x0;
fval = double(subs(f, [x; y], x));
dfval = double(subs(df, [x; y], x));
d2fval = double(subs(d2f, [x; y], x));
while norm(dfval) > tol && iter < maxiter
x = x - d2fval \ dfval;
fval = double(subs(f, [x; y], x));
dfval = double(subs(df, [x; y], x));
d2fval = double(subs(d2f, [x; y], x));
iter = iter + 1;
end
if norm(dfval) <= tol
fprintf('最小值点为 (%f, %f),最小值为 %f\n', x(1), x(2), fval);
else
fprintf('未成功收敛\n');
end
```
输出结果为:
```
最小值点为 (1.000000, 1.000000),最小值为 0.000000
```
说明函数的最小值为 $0$,最小值点为 $(1,1)$。
阅读全文