马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
我是初学者,在借助代码修改程序中出现问题,苦思多日不见出路,特向高手请教.程序如下:
global x;
x1(1,:)=[0.671796 0.919430 0.941397 0.438988 0.866638 0.201446 0.523668 0.408518 0.373441 0.491071 1.114654 1.155400 0.961947 1.094813 1.123866 0.350057 0.391865 0.246599 0.538903 0.502409 0.406430 0.456700 0.460840 0.397420 0.436510 0.483510 0.374620 0.377710 0.590160 0.523430 0.669290 0.665860 0.749950 0.605870 0.710390 0.625950 0.711690 0.499100 0.638230 0.511580 0.663890 0.636220 0.758930 0.528160 3.038550 0.798380 0.786560 0.771090 0.913760 0.606690];
x1(2,:)=[0.767520 0.676370 0.597390 0.562620 0.701980 0.727960 0.835730 0.571260 0.854420 0.842830 0.597410 0.649750 0.509080 0.708950 0.795860 0.955310 0.871440 0.914960 0.815090 0.906860 0.491800 0.541700 0.545900 0.470700 0.499200 0.552200 0.486200 0.458700 0.619100 0.575000 0.658900 0.660500 0.694800 0.641200 0.678700 0.622300 0.672600 0.540700 0.650700 0.541100 0.665800 0.660700 0.766800 0.580900 0.662400 0.757700 0.775700 0.742300 0.819000 0.628400];
x1(3,:)=[0.425500 1.400000 0.259300 0.255860 0.071340 0.041920 0.196130 0.119700 0.188000 0.194500 0.583500 0.230900 1.080000 0.068720 0.176560 0.043970 0.140240 0.160500 0.250200 0.174000 0.008670 0.713800 0.625100 0.172800 0.026740 0.318590 0.024710 0.170000 0.657100 0.303000 0.762700 1.320000 0.346400 0.276750 0.194680 0.281690 0.116280 0.159600 0.112300 0.119600 0.486700 1.150000 0.368300 0.351650 0.080900 0.208280 0.182530 0.105700 0.079850 0.002050];
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 of initial particle
fbestpos=fpopul;% func values of initial particle
%ppopul(:,:,iter)=ppopul;
% finding best particle in initial
[fbestpart(iter),g]=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
[fbestpart(iter),g]=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([2.5 7 2 5 0 4])
hold on
end
iter
fevals
function sum=gold(x1,x2,x3)
global x;
sum=0;
x1(1,:)=[0.671796 0.919430 0.941397 0.438988 0.866638 0.201446 0.523668 0.408518 0.373441 0.491071 1.114654 1.155400 0.961947 1.094813 1.123866 0.350057 0.391865 0.246599 0.538903 0.502409];
x1(2,:)=[0.767520 0.676370 0.597390 0.562620 0.701980 0.727960 0.835730 0.571260 0.854420 0.842830 0.597410 0.649750 0.509080 0.708950 0.795860 0.955310 0.871440 0.914960 0.815090 0.906860];
x1(3,:)=[0.425500 1.400000 0.259300 0.255860 0.071340 0.041920 0.196130 0.119700 0.188000 0.194500 0.583500 0.230900 1.080000 0.068720 0.176560 0.043970 0.140240 0.160500 0.250200 0.174000];
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生成过程中出错,可是不清楚怎么错的, |