在 MATLAB 中使用矢量值输入最小化函数

Minimizing Function with vector valued input in MATLAB

我想最小化如下函数:

这里,n可以是5,10,50等。我想用Matlab,想用梯度下降和拟牛顿法加上BFGS更新来解决这个问题,同时回溯线搜索。我是 Matlab 的新手。有人可以帮忙吗?我可以找到类似问题的解决方案 link: https://www.mathworks.com/help/optim/ug/unconstrained-nonlinear-optimization-algorithms.html .

但是,我真的不知道如何在 Matlab 中创建向量值函数(在我的例子中,输入 x 可以是一个 n 维向量)。

你将不得不做出很大的飞跃才能到达你想去的地方——我可以建议你经历一些basic tutorial first in order to digest basic MATLAB syntax and concepts? Another useful read is the very basic example to unconstrained optimization in the documentation。但是,您的问题的答案仅涉及基本语法,因此我们可以快速完成。

调用优化工具箱的无约束非线性优化算法的绝对最小值是 objective 函数 的公式。该函数应该 return 您的函数在任何给定点 x 的函数值 f,在您的情况下它显示为

function f = objfun(x)
    f = sum(100 * (x(2:end) - x(1:end-1).^2).^2 + (1 - x(1:end-1)).^2);
end

注意

为简单起见,将此函数保存到当前工作目录中的文件 objfun.m,以便您可以通过命令 window.

使用它

现在您所要做的就是从命令 window:

中调用适当的优化算法,例如准牛顿法
n = 10; % Use n variables
options = optimoptions(@fminunc,'Algorithm','quasi-newton'); % Use QM method
x0 = rand(n,1); % Random starting guess
[x,fval,exitflag] = fminunc(@objfun, x0, options); % Solve!
fprintf('Final objval=%.2e, exitflag=%d\n', fval, exitflag);

在我的机器上,我看到算法收敛了:

Local minimum found.

Optimization completed because the size of the gradient is less than the default value of the optimality tolerance.

Final objval=5.57e-11, exitflag=1