📄 conjugate_gradient.m
字号:
function [min,solvec,stepnum]=conjugate_gradient(str,ini,err)
% File name:conjugate_gradient.m
% Author:Yan Anxin
% ID number:081810
% Date:finished on 2008.11.15
% Description:
% Here defines a function,to solve the bivariate unconstrained optimization problems by conjugate-gradient-method
% In the syntax [min,solvec,stepnum]=conjugate_gradient(str,ini,err),
% str----input,means the expression of objective function
% ini----input,sets the starting point to ini
% err----input,sets admissible error
% min=conjugate_gradient(str,ini,err) attempts to find min, i.e. the minimum of objective function
% [min,solvec]=conjugate_gradient(str,ini,err) returns solvec, i.e. the minimum point
% [min,solvec,stepnum]=conjugate_gradient(str,ini,err) returns stepnum,i.e. the times of iterative
[A,b]=coefficient_matrix(str); %calls the function coefficient_matrix(str) to obtain the coefficient
A=A.*2;
x(:,1)=ini;
n=2;k=2;stepnum=0;
sol=[inf,inf]';
while k==n
L(:,1)=A*x(:,1)+b;
if norm(L(:,1))<=err,sol=x(:,1);break,end
p(:,1)=-(A*x(:,1)+b);k=1;
while k~=n
gamma(:,k)=-(A*x(:,k)+b)'*p(:,k)/((p(:,k))'*A*p(:,k));
x(:,k+1)=x(:,k)+gamma(:,k)*p(:,k);
if norm(A*x(:,k+1)+b)<=err,sol=x(:,k);break,end
beta(:,k)=-(A*x(:,k+1)+b)'*p(:,k)/((p(:,k))'*A*p(:,k));
p(:,k+1)=-(A*x(:,k)+b); %gets the direction of search
k=k+1;stepnum=stepnum+1;
end
if sol==x(:,k),break,end
x(:,1)=x(:,n);
end
solvec=sol;
min=minval(str,solvec); %calls the function minval(str,solvec) to obtain the minimum value
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -