Matlab 源码,function前一行表文件名:
1 multphr.m 2 function [x, mu, lambda, output] = multphr(fun, hf, gf, dfun, dhf, dgf, x0) 3 maxk=500; sigma=2; eta=2; theta=0.8; epsilon=1e-5; k=0; ink=0; 4 x=x0; he=feval(hf, x); gi=feval(gf, x); l=length(he); m=length(gi); 5 6 mu=0.1*ones(l,1); lambda=0.1*ones(m,1); 7 btak=10; btakold=10; 8 9 while btak>epsilon && k<maxk 10 [x, ~, ik] = bfgs('mpsi', 'dmpsi', x0, fun, hf, gf, dfun, dhf, dgf, mu, lambda, sigma); 11 ink = ink+ik; 12 he=feval(hf, x); gi=feval(gf, x); 13 btak=0; 14 for i=1:l 15 btak=btak+he(i)^2; 16 end 17 for i=1:m 18 btak=btak+min(gi(i), lambda(i)/sigma)^2; 19 end 20 btak=sqrt(btak); 21 if btak>epsilon 22 if k>=2 && btak>theta*btakold 23 sigma = eta*sigma; 24 end 25 for i=1:l 26 mu(i)=mu(i)-sigma*he(i); 27 end 28 for i=1:m 29 lambda(i)=max(0, lambda(i)-sigma*gi(i)); % lambda(i)=max(0, lambda(i)-gi(i)); 30 end 31 end 32 k=k+1; 33 btakold=btak; 34 x0=x; 35 output.fval=feval(fun, x); 36 output.iter=k; 37 output.innter_iter=ink; 38 output.bta=btak; 39 end 40 41 42 bfgs.m 43 function [x, val, k] = bfgs(fun, gfun, x0, varargin) 44 maxk=5000; k=0; rho=0.55; sigma=0.4; epsilon=1e-5; 45 n=length(x0); Bk=eye(n); 46 while (k<maxk) 47 gk = feval(gfun, x0, varargin{:}); 48 if norm(gk)<epsilon 49 break; 50 end 51 dk = -Bk\gk; 52 m=0; mk=0; 53 while (m<20) 54 if(feval(fun, x0+rho^m*dk, varargin{:}) < feval(fun, x0, varargin{:})+sigma*rho^m*gk'*dk) 55 mk=m; break; 56 end 57 m=m+1; 58 end 59 x=x0+rho^mk*dk; 60 sk=x-x0; 61 yk = feval(gfun, x, varargin{:})-gk; 62 if yk'*sk>0 63 Bk = Bk-(Bk*sk*sk'*Bk)/(sk'*Bk*sk)+(yk*yk')/(yk'*sk); 64 end 65 k=k+1; x0=x; 66 end 67 val = feval(fun, x0, varargin{:}); 68 69 mpsi.m 70 function psi = mpsi(x, fun, hf, gf, dfun, dhf, dgf, mu, lambda, sigma) 71 psi=feval(fun, x); he=feval(hf, x); gi=feval(gf, x); l=length(he); m=length(gi); 72 for i=1:l 73 psi=psi-he(i)*mu(i)+0.5*sigma*he(i)^2; 74 end 75 for i=1:m 76 psi=psi +(min(0, sigma*gi(i)-lambda(i))^2 - lambda(i)^2)/(2*sigma); 77 end 78 79 dmpsi.m 80 function dpsi=dmpsi(x, fun, hf, gf, dfun, dhf, dgf, mu, lambda, sigma) 81 dpsi=feval(dfun, x); he=feval(hf, x); gi=feval(gf, x); dhe=feval(dhf, x); dgi=feval(dgf, x); 82 l=length(he); m=length(gi); 83 for i=1:l 84 dpsi = dpsi+(sigma*he(i)-mu(i))*dhe(:,i); 85 end 86 for i=1:m 87 dpsi = dpsi+(sigma*gi(i)-lambda(i))*dgi(:,i); 88 end 89 90 fun.m 91 function f=fun(x) 92 f=(x(1)-2)^2+(x(2)-1)^2; 93 94 dfun.m 95 function df = dfun(x) 96 df = [2*x(1)-4, 2*x(2)-2]'; 97 98 hf.m 99 function he=hf(x) 100 he=x(1)-2*x(2)+1; 101 102 dhf.m 103 function dfe=dhf(x) 104 dfe=[1, -2]'; 105 106 gff.m 107 function gi=gff(x) 108 gi= -0.25*x(1)^2-x(2)^2+1; 109 110 111 dgf.m 112 function dgi=dgf(x) 113 dgi=[-0.5*x(1), -2*x(2)]';
运行结果: >> x0=[3 3]'; >> [x, mu, lambda, output] = multphr('fun', 'hf', 'gff', 'dfun', 'dhf', 'dgf', x0) x = 0.8229 0.9114 mu = -1.5945 lambda = 1.8465 output = fval: 1.3934 iter: 23 innter_iter: 82 bta: 9.5419e-006