Error using * Incorrect dimensions for matrix multiplication. Error in hbg_implict (line 23) out = A*y + B*u - E*yp;
Show older comments
Hey guys, Whenever i try to run cal my function I keep getting this error, and i am not able to find the error, because i think my matrix dimensions are right. would really appreciate if someone can fix it. Thank you in advance.
function out = hbg_implict(t, y, yp)
index = t*100000;
ts = round(index);
n=0;
even = n(rem(ts,2)==0);
if even == 0 %Change to 100kHz
il1=0;
il2=1;
il3=1;
hsm = hss(il1, il2, il3);
else
il1=1;
il2=0;
il3=1;
hsm = hss(il1, il2, il3);
end
u = [28;0]; % Input V
d = length(hsm(:,1));
w = length(u(:,1));
A = hsm(:,1:d);
B = hsm(:,d+1:d+w);
E = hsm(:,d+w+1:d+w+d);
out = A*y + B*u - E*yp;
end
function hsm = hss(il1, il2, il3)
L1 = 10e-3;
L2 = 2.25e-7;
L3 = 1e-5;
R1 = 1.73;
R2 = 2e-5;
a = 0.00902;
A = [-1*xor(il1,il2)/(L1*R1) -a*xor(il1,il2)/L2 0 0 0
a*xor(il1,il2)/L1 -il3/(L2*R2) 0 0 0
0 0 ~il3/(L3*R2) 0 0
0 0 0 ~xor(il1,il2)/L1 0
0 il3/L2 0 0 -il3/L3];
B = [il1 il2
0 0
0 0
0 0
0 0];
E = [xor(il1,il2) 0 0 0 0
0 1 0 0 il3
0 0 ~il3 0 0
0 0 0 0 0
0 0 0 0 0];
% Reduce size of model
order = length(A(1,:));
z = zeros(1,order);
i = 1;
for n = (1:order)
% Delete rows of zeros
if sum(A(n,:)) == 0
else
Ared1(i,:) = A(n,:);
Bred1(i,:) = B(n,:);
Ered1(i,:) = E(n,:);
i = i+1;
end;
end;
i = 1;
for n = (1:order) % Delete Columns of zeros
if sum(Ared1(:,n)) == 0
else
Ared2(:,i) = Ared1(:,n);
Bred2 = Bred1;
Ered2(:,i) = Ered1(:,n);
i = i+1;
end;
end;
hsm = [Ared2 Bred2 Ered2];
end
The solver that i am using it, is shown below:
tspan = [0:(1/1000000):9.5];
y0 = zeros(1,3)';
yp0 = zeros(1,3);
[t_imp, y_imp] = ode15i(@hbg_implict, tspan, y0, yp0);
% Run simulation until event
tspan1 = [0:1e-3:(5-(1e-3))];
y01=[0 0 0]';
yp01 = zeros(1,3)';
[t1,y1] = ode15i(@hbg_implict, tspan1, y01, yp01);
% Concatenate results vectors
t = [t1; t2];
y = [y1; y2];
Answers (1)
Walter Roberson
on 10 Aug 2021
I know what is happening with your code, but at the moment I do not know why.
The second time your function is called, yp is a row vector instead of a column vector.
ode15i() documents that you should be receiving a column vector for that parameter, so I suspect a MATLAB bug.
The workaround is not difficult:
out = A*y + B*u - E*yp(:);
14 Comments
rohit singh
on 10 Aug 2021
Walter Roberson
on 10 Aug 2021
yp0 = zeros(1,3);
That line there: you need to change it to make yp0 a column vector. That is not what the documentation says, but it is necessary.
% Concatenate results vectors
t = [t1; t2];
y = [y1; y2];
Problem: you do not have a t2 or y2. You created t_imp and y_imp and t1 and y1.
It is not obvious to me what would be wanted there. Your second run is a subset of the first run, just every 1000 samples out of it for a while.
Walter Roberson
on 10 Aug 2021
I submitted a bug report about ode15i not treating the column orientation properly.
rohit singh
on 10 Aug 2021
Walter Roberson
on 10 Aug 2021
Edited: Walter Roberson
on 10 Aug 2021
I made the change to yp0 and ran your code, and it worked until it went to try to do the [t1; t2]
If you post the repaired code for
% Concatenate results vectors
then I will try again.
Walter Roberson
on 10 Aug 2021
Side note: please check the line
Bred2 = Bred1;
That is copying a fixed size of information to Bred2, and that size will not necessarily be compatible with the Ared2 and Ered2 that are being created, as those are like
Ared2(:,i) = Ared1(:,n);
rohit singh
on 10 Aug 2021
rohit singh
on 10 Aug 2021
rohit singh
on 10 Aug 2021
Walter Roberson
on 10 Aug 2021
Your new A matrix is rank 4, but your boundary conditions are still only length 3
out = A*y + B*u - E*yp;
A, B, E are all 4 x something, but your y and yp are only 3.
rohit singh
on 10 Aug 2021
Walter Roberson
on 10 Aug 2021
yp = zeros(1,4)';
yp0 = zeros(1,4)';
rohit singh
on 10 Aug 2021
Walter Roberson
on 10 Aug 2021
That is the Unicode "small tilde" symbol, U+02DC . You need the normal tilde symbol, ~
Categories
Find more on Function Creation 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!