Error using ==> mtimes Inner matrix dimensions must agree.
1 次查看(过去 30 天)
显示 更早的评论
when I run the main code that contain the execution of the function (StateEq.m) , and Xi is a matrix with (StateDim,nCpts) dimensions contains state space column vectors
StateDim = 8;
nCpts = 2*StateDim ;
Xp = StateEq(Xi);
I get
Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> StateEq at 24
X(:,k) = Xa(:,k) + dt*X1;
someone could help me. This is my StateEq function code:
function X=StateEq(Xa)
global StateDim nCpts dt ae ke ai ki c d gama e ks T0 T a ue ui
X=zeros(StateDim,nCpts);
for k=1:nCpts
Ti = 1/(T0+T);
x0 = 1/(1+exp(-c*(1-d)));
xt = 1/(1+exp(-c*(Xa(1,k)-d)));
me = ((2-xt)/(2-x0))*Xa(1,k);
mi = Xa(3,k);
m = ((gama*me)+mi)/(gama+1);
X1=[Xa(2,k);
((ae*ke*(ue-1))-(2*ke*Xa(2,k))-((ke^2)*(Xa(1,k)-1)));
Xa(4,k);
((ai*ki*(ui-1))-(2*ki*Xa(4,k))-((ki^2)*(Xa(3,k)-1)));
Xa(6,k);
((e*(ue-1))-(2*ks*Xa(6,k))-((ks^2)*(Xa(5,k)-1)));
Ti*(Xa(5,k)-(Xa(7,k)^(1/a)));
(1/T0)*(m-(Ti*((T0*(Xa(7,k)^(1/a)))+(T*Xa(5,k)))*(Xa(8,k)/Xa(7,k))))];
X(:,k) = Xa(:,k) + dt*X1;
end
Knowing that
dt ae ke ai ki c d gama e ks T0 T a ue ui
are simple parameters
another thing is that when i try to execute the content of the StateEq in the command window (i create an experimental matrix Xi and execute the content of the function for k=1 it work fine)
2 个评论
KSSV
2016-10-1
Check the size of Xa(:,k), X1. I think they are of different size that's why error popping out.
David Goodmanson
2016-10-1
Looks like it's telling you that dt is not a scalar and that the number of columns of dt ~= the number of rows of X1.
采纳的回答
Walter Roberson
2016-10-1
Your Xi is StateDim by nCpts and you have StateDim = 8; nCpts = 2*StateDim; so we deduce your Xi is 8 by 16. You pass that into your routine, where it becomes known as Xa .
In your code you create X1, which appears to be 8 x 1 .
You do a matrix multiplication dt * X1 and add that to Xa(:,k) . Since we know that Xa is 8 x 16, we deduce that the result of dt * X1 is required to be 8 x 1.
In order for dt * X1 to yield 8 x 1 when X1 is 8 x 1, dt would need to either be a scalar (in which case the * would silently become equivalent of .*) or else would need to be 8 x 8 . Any other size for dt and you would get the error about inner dimensions needing to agree.
Now what do we the readers know about the actual size of dt? The answer to that is: Nothing. It is a global variable for which you did not show the initialization.
One possibility is that dt is empty inside that routine. That would happen if you never initialized a global dt before you called the routine. Your could would need to have set up with
global dt
dt = some value
It is not enough for your code to have initialized with
dt = some value
as that would store only into the local workspace (or perhaps the base workspace) rather than the global workspace.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 General Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!