Error using * Incorrect dimensions for matrix multiplication. Error in hbg_implict (line 23) out = A*y + B*u - E*yp;
1 view (last 30 days)
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];
0 Comments
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
Walter Roberson
on 10 Aug 2021
That is the Unicode "small tilde" symbol, U+02DC . You need the normal tilde symbol, ~
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!