Matrix dimensions do not match

7 次查看(过去 30 天)
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)));}

采纳的回答

Geoff
Geoff 2012-3-27
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 个评论
Srikanth
Srikanth 2012-3-27
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by