# Matrix dimension must agree error

1 view (last 30 days)
Abdul Basit on 16 Jun 2020
Commented: Walter Roberson on 16 Jun 2020
clc;
clear
close all;
%% Phase 1 - Objective Function
%fun
%% Phase 2 - PSO Parameters
LB=[0 0 0];
UB=[10 10 10];
m=3;
n=500;
wmax=0.9;
wmin=0.4;
c1=2.05;
c2=2.05;
Maxiter=100;
% Loop for maximum run start
for run=1:20
%% Phase 3 - Initialization of Position & Velocity
for i=1:n
for j=1:m
pos(i,j)=(LB(j)+rand()).*(UB(j)-LB(j));
end
end
vel=(0.1).*pos;
%% Phase 4 - Function Evaluation
for i=1:n
out(i,1)=fun(pos(i,:));
end
pbestval=out;
pbest=pos;
[fminval, index]=min(out);
gbest=pbest(index,:);
%PSO algorithm start here
iter=1;
while iter<=Maxiter
w=wmax-(iter/Maxiter).*(wmax-wmin);
%% Phase 5 - Compute PBest and GBest
X=pos;
Out=fun(X);
Har=find(out<=pbestval);
pbest(Har,:)=X(Har,:);
pbestval=out(Har);
[fbestval, ind1]=min(pbestval);
if fbestval<=fminval
fminvalue=fbestval;
gbest=pbest(ind1,:);
end
%% Phase 6 - Update Velocity & Position (Handling Boundary Constrains)
for i=1:n
for j=1:m
vel(i,j)=w.*vel(i,j)+c1.*rand().*(pbest(i,j)-pos(i,j))...
+c2.*rand().*(gbest(1,j)-pos(i,j));
pos(i,j)=vel(i,j)+pos(i,j);
if pos(i,j)<LB(j)
pos(i,j)=LB(j);
elseif pos(i,j)>UB(j)
pos(i,j)=UB(j);
end
end
end
iter=iter+1;
end
%% Phase 7 - Store Best Value
F_ans(run)=fun(gbest);
F_gbest(run,:)=gbest;
% Loop for maximum run end
end
[bestFUN, bestRUN]=min(F_ans);
Best_X=F_gbest(bestRUN,:);
plot(F_ans)
Function
function output = fun(X)
x=X(:,1);
R=X(:,2);
H=X(:,3);
vc=3; %Cut in Speed(m/s)
vf=25; %Cut Out Speed(m/s)
vr=12; %Rated Wind Speed(m/s)
p=1.2754; %value of air density i.e.:1.2754kg/m^3
% R=36; %Value of Rotor Radius (m)
% H=65; %Value of Hub Height (m)
Pr=1194.7; %Value of Rated Power(kW)
k=2; %Shape Factor:
c=7.5; %Scale Factor
fun1 = @(v)(v.^3).*(k/c).*((v/c).^(k-1)).*exp(-(v/c).^k);
fun2 = @(v)(vr.^3).*(k/c).*((v/c).^(k-1)).*exp(-(v/c).^k);
f1 = integral(fun1,vc,vr);
f2 = integral(fun2,vr,vf);
AEP=((1744.992.*p.*(pi).*(R.^2))).*(f1+f2);
a=(0.00622.*(R.^3.5))+(0.07756.*(R.^3))+(0.0612.*(R.^2.946))+(0.009426.*(R.^2.887))+(0.3508.*(R.^2.6578));
b=(1.3377.*(R.^2.53))+(0.5297.*(R.^2.5025))-(0.00771.*(R.^2.5))+(0.06901.*(R.^2).*H.*pi)+(5.343.*(R.^1.953));
c=(23.9347.*(R))+(55.7435.*(H.^0.4037).*(R.^0.8074))+((4.041.*(10.^-7)).*(Pr.^3))-(0.00244.*(Pr.^2))+(2.096.*(Pr.^1.249));
d=(15.0656.*(Pr))+(6.8004.*(Pr))+(((0.00108).*(AEP)).*(1-x/15000))+(8265.6416);
e=((0.2082.*(Pr.^3))-(0.006.*(Pr.^3))+(49.4518.*(Pr))+(0.5133.*(H.^1.1736).*(R.^1.1736))).*(1+x/15000);
f=(0.007.*(1+(x/15000)));
output=((a/AEP)+(b/AEP)+(c/AEP)+(d/AEP)+(e/AEP)+f); <== Error Here
end
Error using +
Matrix dimensions must agree.
Error in fun (line 58)
output=((a/AEP)+(b/AEP)+(c/AEP)+(d/AEP)+(e/AEP)+f);
Error in PSOnew (line 56)
Out=fun(X);
Walter Roberson on 16 Jun 2020
I do not encounter any problem when I comment out
<== Error Here
For greater certainty, I have attached what I ran (which did not encounter the problem)

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!