📄 nelder.m
字号:
function [V0,y0,dV,dy]=nelder(F,V,min1,max1,epsilon,show)%Input - F is the object function input as a string 'F'% - V is a 3xn matrix containing starting simplex% - min1 & max1 are minimum and maximum number of iterations% - epsilon is the tolerance% - show == 1 displays iterations (P and Q)%Output - V0 is the vertex forthe minimum% - y0 is the function value F(V0)% - dV is the size of the final simplex% - dy is the error bound for the minimum% - P is a matrix containing the vertex iterations% - Q is an array containing the iterations for F(P)% NUMERICAL METHODS: Matlab Programs% (c) 2004 by John H. Mathews and Kurtis D. Fink% Complementary Software to accompany the textbook:% NUMERICAL METHODS: Using Matlab, Fourth Edition% ISBN: 0-13-065248-2% Prentice-Hall Pub. Inc.% One Lake Street% Upper Saddle River, NJ 07458if nargin==5, show=0;end[mm n]=size(V);% Order the verticesfor j=1:n+1 Z=V(j,1:n); Y(j)=feval(F,Z);end[mm lo]=min(Y);[mm hi]=max(Y);li=hi;ho=lo;for j=1:n+1 if(j~=lo&j~=hi&Y(j)<=Y(li)) li=j; end if (j~=hi&j~=lo&Y(j)>=Y(ho)) ho=j; endendcnt=0;% Start of Nelder-Mead algorithmwhile (Y(hi)>Y(lo)+epsilon&cnt<max1)|cnt<min1 S=zeros(1,1:n); for j=1:n+1 S=S+V(j,1:n); end M=(S-V(hi,1:n))/n; R=2*M-V(hi,1:n); yR=feval(F,R); if (yR<Y(ho)) if (Y(li)<yR) V(hi,1:n)=R; Y(hi)=yR; else E=2*R-M; yE=feval(F,E); if (yE<Y(li)) V(hi,1:n)=E; Y(hi)=yE; else V(hi,1:n)=R; Y(hi)=yR; end end else if (yR<Y(hi)) V(hi,1:n)=R; Y(hi)=yR; end C=(V(hi,1:n)+M)/2; yC=feval(F,C); C2=(M+R)/2; yC2=feval(F,C2); if (yC2<yC) C=C2; yC=yC2; end if (yC<Y(hi)) V(hi,1:n)=C; Y(hi)=yC; else for j=1:n+1 if (j~=lo) V(j,1:n)=(V(j,1:n)+V(lo,1:n))/2; Z=V(j,1:n); Y(j)=feval(F,Z); end end end end [mm lo]=min(Y); [mm hi]=max(Y); li=hi; ho=lo; for j=1:n+1 if (j~=lo&j~=hi&Y(j)<=Y(li)) li=j; end if (j~=hi&j~=lo&Y(j)>=Y(ho)) ho=j; end end cnt=cnt+1; P(cnt,:)=V(lo,:); Q(cnt)=Y(lo);end% End of Nelder-Mead algorithm%Determine size of simplexsnorm=0;for j=1:n+1 s=norm(V(j)-V(lo)); if(s>=snorm) snorm=s; endendQ=Q';V0=V(lo,1:n);y0=Y(lo);dV=snorm;dy=abs(Y(hi)-Y(lo));if (show==1) disp(P); disp(Q);end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -