[求助]函数出错求助
<P>function simple = simple(N,a,x)<br>Fun = @fun;<br>eps = 1/(10.^12);<br>Sig = 10000;<br>alpha = 1;<br>beta = 0.5;<br>gama = 2;<br>count =0;<br>p = a*(sqrt(N+1)+N-1)/(sqrt(2)*N);<br>q = a*(sqrt(N+1)-1)/(sqrt(2)*N);<br>simp = zeros(N,N+1);%-------定义-----%<br>reflect = ones(0,N);<br>f_worst = 0;<br>i_worst = 0;<br>f_best = 0;<br>i_best = 0;</P><P>%--------------------建立单纯行矩阵------------------%<br>for i = 1:N<br> for j = 1:N+1;<br> simp(i,j) = x(i);<br> end<br>end</P>
<P>for i =1:N<br> for j =2:N+1<br> if( ( j - i ) ~= 1)<br> simp(i,j) = x(i) + q;<br> end<br> end<br>end</P>
<P>for i = 2:N+1<br> simp(i-1,i) = x(i-1) +p;<br>end</P>
<P>%---------------获取x()时函数f()的值------------------%</P>
<P>for i = 1:N+1<br> f(i)= feval(Fun,(simp(:,i)));<br>end</P>
<P>%----------------开始迭代----------------%<br>while( Sig > eps)<br> count = count + 1;<br> f_worst = f(1);<br> i_worst = 1;<br> f_best = f(1);<br> i_best = 1;<br>%----------------求得最大值---------------% <br> for i = 2:N+1<br> if(f(i) > f_worst)<br> i_worst = i;<br> f_worst = f(i);<br> end<br> if(f(i)< f_best)<br> i_best = i;<br> f_best = f(i);<br> end<br> end<br><br>%------------------求得次最大值----------------%<br> i_subworst = 1;<br> f_subworst = f(1);<br> if(i_worst == 1)<br> i_subworst = 2;<br> f_subworst = f(2);<br> end<br> <br> for i = (i_subworst+1):N+1<br> if((i~=i_worst) & (f(i)>f_subworst))<br> i_subworst = i;<br> f_subworst = f(i);<br> end<br> end<br> <br> %---------------------求得形心-----------------------%<br> for j = 1:N<br> x_center(j) = 0;<br> for i = 1:N+1<br> if (i~=i_worst)<br> x_center(j) = x_center(j) + simp(j,i);<br> end<br> end<br> x_center(j) = x_center(j)/N;<br> end</P>
<P> %--------------------得到投影点-----------------------%<br> for(i = 1:N)<br> x_reflect(i) = (1+alpha)*x_center(i)-alpha*simp(i,i_worst);<br> end<br> <br> f_reflect = fun(x_reflect);<br> %---------------------放大-----------------------%<br> if(f_reflect < f(i_best))<br> for (i = 1:N)<br> x_expand(i) = gama*x_reflect(i)+(1-gama)*x_center(i);<br> end<br> f_expand = fun(x_expand);<br> if(f_expand <f(i_best))<br> for (i = 1:N)<br> simp(i,i_worst) = x_expand(i);<br> end<br> f(i_worst) = f_expand;<br> else<br> for(i = 1:N)<br> simp(i,i_worst)= x_reflect(i);<br> end<br> fun(i_worst) = f_reflect;<br> end<br> <br> else<br> if(f_reflect <= f(i_subworst))<br> for (i = 1:N)<br> simp(i,i_worst) = x_reflect(i);<br> end<br> f(i_worst) = f_reflect;<br> continue;<br> else<br> if(f_reflect <= f(i_worst))<br> for j = 1:N<br> simp(j,i_worst) = x_reflect(j)<br> end<br> f(i_worst) = f_reflect;<br> <br> end<br> end<br> %------------------收缩-------------------%<br> for i = 1:N;<br> x_compact(i) = beta*simp(i,i_worst)+(1-beta)*x_center(i);<br> end<br> f_compact = feval(Fun,x_compact);<br> <br> %----------------收缩不行就重置------------%<br> if(f_compact > f(i_worst)) <br> for(i = 1:N)<br> for( j = 1:N+1)<br> simp(i,j) = (simp(i,j) + simp(i,i_best))/2;<br> end<br> end<br> else<br> for (i = 1:N)<br> simp(i,i_worst) = x_compact(i);<br> end<br> f(i_worst) = f_compact;<br> end<br> <br> %-------------------判断结束条件------------------%<br> Sig = 0;<br> f_center = feval(Fun,x_center);</P>
<P> for (j = 1:N+1)<br> Sig = Sig + (feval(Fun,(simp(:,j))) -f_center).^2;<br> end<br> Sig = sqrt(Sig/(N+1));<br> end<br> end <br>end</P>
<P>%-------------------显示结果-------------------%<br>(simp(:,1))<br>fmin = feval(Fun,(simp(:,1)))<br></P>
[此贴子已经被suffer于2006-5-18 10:13:54编辑过]
页:
[1]