main.m

来自「最优化计算的牛顿法+共轭梯度法的MATLAB程序」· M 代码 · 共 49 行

M
49
字号
function y=main(x)
%This program uses the steepest_down + conjugate_grad method
%to calculate the minimum of the function f(x)=x(1)^2+2*x(2)^2

format long
eps=input('please input your accuracy:');
%eps is the demmanded accuracy on the norm of 
%the gradient of the objective function

m=1;
%m is the count of the iteration step of the algorithm

iterstep(1,:)=x;
%iterstep contains the intermediate points of iteration

while m<=3
   while norm(gradobject1(x))>eps
     grad=gradobject1(x);
     alpha=goldsplictobj(x);
     x=x-alpha*grad;
     iterstep(m+1,:)=x;
     m=m+1;
  end
end

while norm(gradobject1(x))>eps
    x1=iterstep(m-1,:);
    x2=iterstep(m,:);
    grad1=gradobject1(x1);
    grad2=gradobject1(x2);
    beta=(norm(gradobject1(x2))/norm(gradobject1(x1)))^2;
    S=-grad2-beta*grad1;
    x2=x2-alpha*S;
    x=x2;
    iterstep(m+1,:)=x;
    m=m+1;
end


step=max(size(iterstep))-1
x
iterstep
plot(iterstep(:,1),iterstep(:,2));
%Draw the search trajectory 




⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?