MATLAB: Matrix dimensions do not match

MATLABmatrix sizeode45

Hi, I am using the ode 45 function in matlab for my statespace model. For the last line of the code i keep getting the error In an assignment A(I) = B, the number of elements in B and I must be the same. This is for the line u(i)=(sin(f*2*pi.*t+(alpha(i)/2)))+(sin(f*2*pi.*t-(alpha(i)/2))); I thought i already assigned the matrix sizes in the lines above. what is the problem
u=zeros(1,8000);
alpha=zeros(1,8000);
[t,x]=ode45(@ssmodel,t,x0);
for i=2:tstop*fsamp
%u is the input,pout is the output power which is state variable x5
%squared over Resistance
u(i)=u(i-1);
pout=(x(:,5).^2)/Rac;
%poutmean is just the average of pout
poutmean(i)=((i-1)/i)*mean(pout(i-1,1))+ (pout(i,1))/i;
%the error is fed to a PI controller, Kp is the proportional gain and
%Ki is the integral gain
e(i)=pref-poutmean(i);
alpha(i)=((Kp+Ki)*e(i))-(Kp*e(i-1))+(alpha(i-1));
%a limiter which limits the range of alpha to +-pi.
if(alpha(i)>pi)
alpha(i)=pi;
else if (alpha(i)<-pi)
alpha(i)=-pi;
end
end
u(i)=(sin(f*2*pi.*t+(alpha(i)/2)))+(sin(f*2*pi.*t-(alpha(i)/2)));}

Best Answer

You are calling sin() on an array. It returns an array. You are assigning that array to u(i), which is a single value in an array. You will get an error if you do that.
Also, don't use 'else if', use 'elseif' with one 'end'.
Related Question