Info

此问题已关闭。 请重新打开它进行编辑或回答。

When I am trying to run my code, I am getting these errors: Error using size Too many input arguments. Error in cell/strcat (line 26) siz{i} = size(varargin{i}); Error in uiopen (line 63) allML(1)=s​trcat(allM​L(1), ';*.mdl;*.slx');

1 次查看(过去 30 天)
clc;clear all;close all;
size = 10;
maxIter = 100;
inertia = 1;
correction_factor = 2;
% set the position of the initial swarm
a = 1:10;
[X,Y] = meshgrid(a,a);
C = cat(2,X',Y');
D = reshape(C,[],2);
position.x(1:size,1) = D(:,1);
position.y(1:size,1) = D(:,2);
position.v.x(1:size,1) = 0;
position.v.y(1:size,1) = 0;
position.funbest(1:size,1) = 10000;
position.pbest.x(1:size,1) = 0;
position.pbest.y(1:size,1) = 0;
plotObjFcn = 1;
objfcn = @(s)(s(:,1) - 30).^2 + (s(:,2) - 30).^2;
tic;
for iter = 1:maxIter
position.x(1:size,1) = position.x(1:size,1) + position.v.x(1:size,1)/1.3;
position.y(1:size,1) = position.y(1:size,1) + position.v.y(1:size,1)/1.3;
x = position.x(1:size,1);
y = position.y(1:size,1);
fval = objfcn([x y]);
for a = 1:size
if fval(a,1) < position.funbest(a,1)
position.pbest.x(a,1) = position.x(a, 1);
position.pbest.y(a,1) = position.y(a, 1);
position.funbest(a,1) = fval(a,1);
end
end
[~, gbest] = min(position.funbest(:,1));
position.v.x(:,1) = inertia*(rand(size,1).*position.v.x(:,1)) + correction_factor*(rand(size,1).*(position.pbest.x(:,1) ...
- position.x(:,1))) + correction_factor*(rand(size,1).*(position.pbest.x(gbest,1) - position.x(:,1)));
position.v.y(:,1) = inertia*(rand(size,1).*position.v.y(:,1)) + correction_factor*(rand(size,1).*(position.pbest.x(:,1) ...
- position.y(:,1))) + correction_factor*(rand(size,1).*(position.pbest.y(gbest,1) - position.y(:,1)));
clf;plot(position.x(:,1), position.y(:,1), 'kP');
axis([-2 100 -2 100]);
pause(.1);
disp(['iteration: ' num2str(iter)]);
end
toc;
hold on
figure(2);
if plotObjFcn
ub = 60;
lb = 0;
npoints = 1000;
x = (ub-lb) .* rand(npoints,2) + lb;
for a = 1:npoints
f = objfcn([x(a,1) x(a,2)]);
plot3(x(a,1),x(a,2),f,'.r');hold on
end
plot3(position.pbest.x(1,1),position.pbest.y(1,1),position.funbest(1,1),'xb','linewidth',5,'Markersize',5);grid
end
  1 个评论
Stephen23
Stephen23 2016-2-16
You don't use any of the functions shown in error message. Please add a comment here showing us the complete error message: this means all of the red text.

回答(1 个)

Arnab Sen
Arnab Sen 2016-2-23
Hi vineeth ,
You are getting this error due the variable 'size' that you define in the second line of the code by using the statement:
>>size=10
As a result the variable 'size' shadows the MATLAB inbuilt function 'size'. That is, as soon as the 'size' variable comes into the workspace any consecutive reference to 'size' would refer to the variable and not the MATLAB function. So, the following statement in cell/strcat (line 26)
>> siz{i} = size(varargin{i});
Here, the issue is the 'size' is treated as a variable and not as a function and MATLAB throws the error. To avoid it give the variable as different name other than 'size'.
You can verify using this by the following command :
>>which -all size

此问题已关闭。

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by