3,071 views (last 30 days)

Guillaume
on 26 Jan 2017

Edited: Guillaume
on 26 Jan 2017

Replace the two / by ./, and ^2 by .^2

And if you want to avoid potential nasty surprises in future code, never write a subtraction as

a -b

either put a space on both sides of the operator

a - b

or on neither:

a-b

Compare the result of

[2 - 1]

[2-1]

[2 -1]

to see why your notation is problematic.

Diah Junaidi
on 27 Nov 2017

How about this sir?? Would you please check mine too. I dont know what going to do sir :(

Ivan Dwi Putra
on 10 Mar 2020

function dx = trainfinalmodel(t,x)

%Parameter Massa

m1 = 65; % massa train set 1 dalam kg

m2 = 65; % massa train set 2 dalam kg

g = 10;

%Parameter Gaya

f1 = 117; % dalam N

f2 = 117; % dalam N

c_0_1 = 0.0016*m1*g;

c_1_1 = 0.00008*m1*g;

c_2_1 = 0.0000006*m1*g*(x(2)^2) ;

c_0_2 = 0.0016*m2*g;

c_1_2 = 0.00008*m2*g;

c_2_2 = 0.0000006*m2*g*(x(4)^2) ;

v_0 = 60;

hstar = 120;

a_1 = -1/m1*(c_1_1+2*c_2_1*v_0);

a_2 = -1/m2*(c_1_2+2*c_2_2*v_0);

a_1_head = 1-(a_1*hstar);

a_2_head = 1-(a_2*hstar);

b = 1;

p_1 = -1/m1*(c_0_1 - c_2_1*(v_0)^2);

p_2 = -1/m2*(c_0_2 - c_2_2*(v_0)^2);

x = [x(1);x(2);x(3);x(4)];

x_transpost_t = transpose(x);

A = [0 a_1_head 0 0;

0 0 0 0;

0 (a_2_head-1) 0 a_2_head;

0 0 0 0;

];

B = [-b*hstar 0;

b 0;

0 -b*hstar;

-b -b;

];

U_t = [f1;f2];

W = [((a_1 - 1)*v_0) - p_1*hstar;

((a_2 - 1)*v_0) - p_2*hstar;

];

dx = (A.*x_transpost_t) + (B.*U_t) + W;

end

please check my mine too, i have same problem the matrix dimension must agree

the cyclist
on 26 Jan 2017

Edited: the cyclist
on 26 Jan 2017

You need to use element-wise division, not matrix division.

Use "./" in place of "/"

You are similarly going to want ".^" rather than just "^" for raising to a power.

Diah Junaidi
on 27 Nov 2017

How about this sir?? I dont know what should i do :( would you please check mine sir ? :(

the cyclist
on 27 Nov 2017

Ivan Dwi Putra
on 27 Apr 2020

I need help for my code, i have 3 question

- if my B matrix 2x4 dimension, the K matrix in LQR is only 1 K or there is two K1 and K2 ?
- if two K matrix, the K matrix = [K1;K2] or [K1 K2] ?
- i have error dimension matrix must agree, when i want multiply matrix in state space function

this is my code

function dx = lqrtrain(t,x)

%Parameter Massa

m1 = 80*10^3; % massa train set 1 dalam kg

m2 = 80*10^3; % massa train set 2 dalam kg

g = 10;

%Parameter Gaya

f1 = 117*10^10; % dalam N

f2 = 117*10^10; % dalam N

c_0_2 = 0.01176 ;

c_1_2 = 0.00077616;

c_2_2 = 4.48;

v_0 = 60;

hstar = 120;

a_1 = (-1./m1.*(c_1_1 + 2*c_2_1*v_0));

a_2 = (-1./m2.*(c_1_2 + 2*c_2_2*v_0));

a_1_head = 1 - (a_1*hstar);

a_2_head = 1 - (a_2*hstar);

b = 1;

p_1 = -1./m1.*(c_0_1 - c_2_1*(v_0)^2);

p_2 = -1./m2.*(c_0_2 - c_2_2*(v_0)^2);

% Initial Conditions

x0 = [0;

0;

500;

0;];

A = [0 a_1_head 0 0;

0 0 0 0;

0 (a_2_head-1) 0 a_2_head;

0 0 0 0;

];

B = [-b.*hstar 0;

b 0;

0 -b.*hstar;

-b b ;

];

C = [1 0 0 0;

0 0 1 0;

];

D = 0;

Q = [10 0 0 0;

0 10 0 0;

0 0 10 0;

0 0 0 10;

];

R = [1 1;

1 2

];

K1 = lqr(A,B,Q,R);

K2 = lqr(A,B,Q,R);

K = [-1000.00; -946.79; 4353.27; 803.53;];

K_1 = [10.2

9.9007

211.2757

37.501];

W = [((a_1-1)*v_0)-(p_1*hstar);

0;

((a_2-1)*v_0)-(p_2*hstar);

((a_1-1)*v_0)-(p_1*hstar);

];

sys = ss((A - (B.*K_1)), B, C, D);

% Run response to initial condition

t = 0:0.005:30;

[t,x] = initial(sys, x0, t);

plot(t,x)

end

the cyclist
on 27 Apr 2020

You posted this as an "answer" on a 2-year-old question. Almost no one will see it.

Instead, you should post a new question.

krishnasundeep basavaraju
on 18 Jul 2020

can any one help me to resolve the error : matrix dimensions must agree

Error in eemd (line 14)

modes = modes + emd(y1,'MaxNumIMF',aim);

the code is given below:

function [modes] = eemd(y, aim, NR, Nstd)

stdy = std(y);

if stdy < 0.01

stdy = 1;

end

y = y ./ stdy;

siz = length(y);

modes = zeros(siz,aim);

for k = 1:NR

disp(['Ensemble number #' num2str(k)]);

wn = (randn(1,siz).*Nstd)';

y1 = y + wn;

y2 = y - wn;

modes = modes + emd(y1,'MaxNumIMF',aim);

if Nstd > 0 && NR > 1

modes = modes + emd(y2,'MaxNumIMF',aim);

end

end

modes = modes .* stdy ./ (NR);

if Nstd > 0 && NR > 1

modes = modes ./ 2;

end

end

the cyclist
on 18 Jul 2020

You posted this as an "answer" on a 3-year-old question. Almost no one will see it.

Instead, you should post a new question.

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/322080-how-can-i-fix-this-error-error-using-matrix-dimensions-must-agree#comment_423609

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/322080-how-can-i-fix-this-error-error-using-matrix-dimensions-must-agree#comment_423609

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/322080-how-can-i-fix-this-error-error-using-matrix-dimensions-must-agree#comment_506628

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/322080-how-can-i-fix-this-error-error-using-matrix-dimensions-must-agree#comment_506628

Sign in to comment.