在 MATLAB 上绘制等高线以及梯度下降的迭代

时间:2021-04-01 16:07:50

标签: optimization matlab gradient-descent

Iterates of the gradient method along with the contour lines of the objective function

上图来自 Amir Beck 的“非线性优化简介:MATLAB 的理论、算法和应用”。他还为二次形式添加了以下梯度下降代码。

function [x,fun_val]=gradient_method_quadratic(A,b,x0,epsilon)
% INPUT
% ======================
% A ....... the positive definite matrix associated with the
% objective function
% b ....... a column vector associated with the linear part of the
% objective function
% x0 ...... starting point of the method
% epsilon . tolerance parameter
% OUTPUT
% =======================
% x ....... an optimal solution (up to a tolerance) of
% min(x^T A x+2 b^T x)
% fun_val . the optimal function value up to a tolerance
x=x0;
iter=0;
grad=2*(A*x+b);
while (norm(grad)>epsilon)
iter=iter+1;
t=norm(grad)^2/(2*grad’*A*grad);
x=x-t*grad;
grad=2*(A*x+b);
fun_val=x’*A*x+2*b’*x;
fprintf(’iter_number = %3d norm_grad = %2.6f fun_val = %2.6f\n’,...
iter,norm(grad),fun_val);
end

我尝试绘制轮廓线,但无法绘制。我能做的最好的事情就是绘制每次迭代,所以我主要在绘制等高线时遇到问题。我是 MATLAB 的初学者,所以也许这并不太难,但任何帮助都将不胜感激。谢谢。

0 个答案:

没有答案