Matrix dimensions do not match

7 views (last 30 days)
Srikanth
Srikanth on 26 Mar 2012
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)));}

Accepted Answer

Geoff
Geoff on 27 Mar 2012
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'.
  1 Comment
Srikanth
Srikanth on 27 Mar 2012
thanks for the reply, i am a little confused with the syntax. I want to pass each element of the array for alpha into the sin(), so i get the array for u updating for each increment of i. What is it that i am doing wrong here. I thought to assign it as a vector we just write u(i) in matlab script.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!