Error using * Incorrect dimensions for matrix multiplication. Error in hbg_implict (line 23) out = A*y + B*u - E*yp;

1 Ansicht (letzte 30 Tage)
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];

Antworten (1)

Walter Roberson
Walter Roberson am 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 Kommentare
rohit singh
rohit singh am 10 Aug. 2021
It worked!! thanks a lot.
Walter, do you know why matlab doesnt accept this symbol " ˜ " ?
It matlab accept this symbol then I dont have to put the values for the vairables in the matrix, it automatically takes it.
you can see in the code shown below where i am trying to use the symbol:
function out = hbg(t, y, yp)
index = t*100000;
ts = round(index);
n=0; even = n(rem(ts,2)==0);
if even == 0; %Change to 100kHz
l1=0; l2=1; l3=1;
hsm = hss(l1, l2, l3);
else l1=1; l2=0; l3=1;
hsm = hss(l1, l2, l3);
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
A = [-1*xor(l1,l2)/(L1*R1) -a*xor(l1,l2)/L2 0 0 0
a*xor(l1,l2)/L1 -l3/(L2*R2) 0 0 0
0 0 ˜l3/(L3*R2) 0 0
0 0 0 ˜xor(l1,l2)/L1 0
0 l3/L2 0 0 -l3/L3];
B = [l1 l2
0 0
0 0
0 0
0 0];
E = [xor(l1,l2) 0 0 0 0
0 1 0 0 l3
0 0 ˜l3 0 0
0 0 0 0 0
0 0 0 0 0];
Walter Roberson
Walter Roberson am 10 Aug. 2021
That is the Unicode "small tilde" symbol, U+02DC . You need the normal tilde symbol, ~

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by