modified_newton.m

来自「压缩包里包含了无约束优化问题常用的几种求解方法的源程序:变量轮换法(variab」· M 代码 · 共 33 行

M
33
字号
function [min,solvec,stepnum]=modified_newton(str,ini,err)
%  File name:modified_newton.m      
%  Author:Yan Anxin
%  ID number:081810
%  Date:finished on 2008.11.16
%  Description:  
%       Here defines a function,to solve the bivariate unconstrained optimization problems by modified-newton-method
%       In the syntax [min,solvec,stepnum]=modified_newton(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=modified_newton(str,ini,err) attempts to find min, i.e. the minimum of objective function   
%       [min,solvec]=modified_newton(str,ini,err) returns solvec, i.e. the minimum point  
%       [min,solvec,stepnum]=modified_newton(str,ini,err) returns stepnum,i.e. the times of iterative


str='x1-x2+2*x1^2+2*x1*x2+x2^2';
ini=[0 0]';
err=0.1^6;             
k=1;stepnum=0;
[A,b]=coefficient_matrix(str);           %calls the function coefficient_matrix(str) to obtain the coefficient
x(:,1)=ini;
syms lambda x1 x2
while norm(gradient(str,x(:,k)))>=err
    p(:,k)=-(2*A)^(-1)*gradient(str,x(:,k));
    f=subs(str,[x1 x2],x(:,k)+lambda*p(:,k));
    f1=diff(f,lambda);
    lam=solve(f1,lambda);               %gets the best step
    x(:,k+1)=x(:,k)+lam*p(:,k);
    k=k+1;stepnum=stepnum+1;
end
solvec=x(:,k);
min=minval(str,solvec);                 %calls the function minval(str,solvec) to obtain the minimum value

⌨️ 快捷键说明

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