Subscripted assignment dimension mismatch 出错请教
我是初学者,在借助代码修改程序中出现问题,苦思多日不见出路,特向高手请教.程序如下:global x;
x1(1,:)=;
x1(2,:)=;
x1(3,:)=;
success=0;
PopSize=20;
MaxIt=40;
iter=1;
fevals=0;%function evaluation'counter
maxw=1.2;%maximum interia weight's value
minw=0.1;%minimum interia weight's value
c1=0.5;
c2=0.5;
inertdec=(maxw-minw)/MaxIt;% weight decrease by same value, search change function of weight
w=maxw;
f='gold';
dim=3;
ErrGoal=1e-4;
%initializing swarm and velocity
vel=rand(dim,PopSize);
for i=1:3
popul(i,:)=rand(1,PopSize)*(max(x1(i,:))-min(x1(i,:)))+min(x1(i,:))
end
vel=rand(dim,PopSize);
%evaluation initial population
for i=1:PopSize
x1=popul(1,i); %popul is 2D array,2D particle population
x2=popul(2,i);
x3=popul(3,i);
fpopul(i)=feval(f,x1,x2,x3); %fpopul is 1D array,function values of 2D particle population
fevals=fevals+1;
ppopul(:,i,iter)=popul(:,i);
end
bestpos=popul; % positions ofinitial particle
fbestpos=fpopul;% func values of initial particle
%ppopul(:,:,iter)=ppopul;
% finding best particle in initial
=min(fpopul);% min value of population and index.
lastbpf=fbestpart(iter);% current values of optimal particle
%swarm evolution loop
while(iter<MaxIt)
iter=iter+1;
% update the value of inertia weight w
if(iter<=MaxIt)
w=maxw-(iter-1)*inertdec;
end
%velocity update
for i=1:PopSize
A(:,i)=bestpos(:,g);% optical position of each particle is g.
end
R1=rand(dim,PopSize);
R2=rand(dim,PopSize);
vel=w*vel+c1*R1.*(bestpos-popul)+c2*R2.*(A-popul);%local optical is bestpos,global optical is A.
%swarm update
popul=popul+vel;
for i=1:3
for j=1:PopSize
ppopul(i,j,iter)=popul(i,j);
end
end
for i=1:PopSize
x1=popul(1,i);
x2=popul(2,i);
x3=popul(3,i);
fpopul(iter,i)=feval(f,x1,x2,x3);
fevals=fevals+1;
end
for i=1:PopSize
if(fpopul(iter,i)<fbestpos(i))
fbestpos(i)=fpopul(iter,i);
bestpos(:,i)=popul(:,i);
end
end
=min(fbestpos);
if (abs(fbestpart(iter)-fbestpart(iter-1))<=ErrGoal)&(abs(fpopul(iter,1)-fpopul(iter-1,1))<=ErrGoal)
break;
end
end
xmin=popul(:,g)
fxmin=fbestpos(g)
bestpos
subplot(2,2,1)
a=iter/3;
for i=1:a
i=i*2;
% text(xx1(i,1),yy1(i,1),zz1(i,1),['\leftarrow(',num2str(i),')'],'FontSize',6.0)
plot3(ppopul(1,1,i),ppopul(2,1,i),ppopul(3,1,i),'linestyle','--','marker','.','markersize',3)
xlabel('萼片长度/cm','fontsize',8),ylabel('花瓣高度/cm','fontsize',8),zlabel('花瓣宽度/cm','fontsize',8)
title('粒子1优化过程轨迹','FontSize',8)
axis()
hold on
end
iter
fevals
function sum=gold(x1,x2,x3)
global x;
sum=0;
x1(1,:)=;
x1(2,:)=;
x1(3,:)=;
for i=1:20
sum=sum+0.126*(x(1,i)-x1)^2+0.505*(x(2,i)-x2)^2+0.368*(x(3,i)-x3)^2;
end
运行结果是:
??? Subscripted assignment dimension mismatch.
可能是gold生成过程中出错,可是不清楚怎么错的, 原帖由 pier 于 2007-8-3 16:06 发表 http://www.chinavib.com/forum/images/common/back.gif
我是初学者,在借助代码修改程序中出现问题,苦思多日不见出路,特向高手请教.程序如下:
global x;
x1(1,:)=
根据matlab的出错提示,定位到程序具体代码中,然后设置断点,调试一下。详情请参阅各个置顶贴
回复 #2 eight 的帖子
非常高兴能得到这么快的回复!!由衷地感到这个论坛的的负责人都特别认真!!谢谢eight!继续上面的问题:
运行结果是:Error in ==> pso>gold at 124
x1(1,:)=;
Error in ==> pso at 34
fpopul(i)=feval(f,x1,x2,x3); %fpopul is 1D array,function values of 2D particle population
因为采用的是gold优化序列(这个就是我用原来的标题的原因),结果运行的维度错误确实应该是关于gold,因此我主要想就gold这个优化序列指令的使用注意事项请教高手?
自己解答
我自己来解答吧运行过程中没有处理好下面一段:
for i=1:PopSize
x1=popul(1,i); %popul is 2D array,2D particle population
x2=popul(2,i);
x3=popul(3,i);
fpopul(i)=feval(f,x1,x2,x3); %fpopul is 1D array,function values of 2D particle population
fevals=fevals+1;
ppopul(:,i,iter)=popul(:,i);
end
因此,fpopul(i)=feval(f,x1,x2,x3)中的x2,x3等的脚标出现问题 请问楼主是怎么解决的这个问题呢?我现在也遇到相似的问题了,想不出解决办法 原帖由 苏静207 于 2010-6-25 10:56 发表 http://www.chinavib.com/forum/images/common/back.gif
请问楼主是怎么解决的这个问题呢?...
5F???
页:
[1]