Not your typical vertcat error. Weird behaviour with it.

Hey,
I am not sure what's going on with editor, but I am having issues in understanding why does dxdt does not work properly. The error is about vertcat.
% Error using vertcat
% Dimensions of arrays being concatenated are not consistent.
Here is the code
load("nlworkspace.mat");
m1 = parameters(1);
m2 = parameters(2);
k1 = parameters(3);
k2 = parameters(4);
d1 = parameters(5);
d2 = parameters(6);
% Output equation.
y = [x(1)]; % Displacement of the smaller mass
Now we execute each row of dxdt (further below) and we see the result
x(2)
ans = 50
(-((k1+k2)*x(1))/m1) + ((k2*x(3))/m1) - (((d1+d2)*x(2))/m1) + ((d2*x(4))/m1) +u(1)
ans = -416.6667
x(4)
ans = 50
(d2*x(2)/m2) - (d2*x(4)/m2) + (k2*x(1)/m2) - (k2*x(3)/m2) + 0
ans = 0
But if I want to do it this way, it doesn't work. Issue is that I have to add extra parentheses on the second element, but there should be none!
% State equations.
dxdt = [x(2); ...
(-((k1+k2)*x(1))/m1) + ((k2*x(3))/m1) - (((d1+d2)*x(2))/m1) + ((d2*x(4))/m1) +u(1); ...
x(4); ...
(d2*x(2)/m2) - (d2*x(4)/m2) + (k2*x(1)/m2) - (k2*x(3)/m2) + 0];
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

 Respuesta aceptada

load("nlworkspace.mat")
m1 = parameters(1)
m1 = 12
m2 = parameters(2)
m2 = 13
k1 = parameters(3)
k1 = 100
k2 = parameters(4)
k2 = 100
d1 = parameters(5)
d1 = 0
d2 = parameters(6)
d2 = 0
% Output equation.
y = [x(1)] % Displacement of the smaller mass
y = 50
x(2)
ans = 50
(-((k1+k2)*x(1))/m1) + ((k2*x(3))/m1) - (((d1+d2)*x(2))/m1) + ((d2*x(4))/m1) +u(1)
ans = -416.6667
x(4)
ans = 50
x(3)
ans = 50
x(4)
ans = 50
(d2*x(2)/m2) - (d2*x(4)/m2) + (k2*x(1)/m2) - (k2*x(3)/m2) + 0
ans = 0
(-((k1+k2)*x(1))/m1) + ((k2*x(3))/m1) - (((d1+d2)*x(2))/m1) + ((d2*x(4))/m1) +u(1)
ans = -416.6667
(d2*x(2)/m2) - (d2*x(4)/m2) + (k2*x(1)/m2) - (k2*x(3)/m2) + 0
ans = 0
% State equations.
dxdt = [x(2);(-((k1+k2)*x(1))/m1) + ((k2*x(3))/m1) - (((d1+d2)*x(2))/m1) + ((d2*x(4))/m1) + u(1);
x(4); (d2*x(2)/m2) - (d2*x(4)/m2) + (k2*x(1)/m2) - (k2*x(3)/m2) + 0]
dxdt = 4×1
50.0000 -416.6667 50.0000 0

5 comentarios

Okay,
so the issue is in the element commented below.
dxdt = [x(2); ...
(-((k1+k2)*x(1))/m1) + ((k2*x(3))/m1) - (((d1+d2)*x(2))/m1) + ((d2*x(4))/m1) +u(1); ...
% ^^^^^
x(4); ...
(d2*x(2)/m2) - (d2*x(4)/m2) + (k2*x(1)/m2) - (k2*x(3)/m2) + 0];
What is this behaviour, why does it work when there is space between '+' and 'u(1)'?
"why does it work when there is space between '+' and 'u(1)'?"
You can see this for yourself:
[1 -2]
ans = 1×2
1 -2
[1 - 2]
ans = -1
VBBV
VBBV el 11 de Mayo de 2023
Editada: VBBV el 11 de Mayo de 2023
Mathematical operators have precedence when used in equations or expressions.
When a space is separating between 2 variables is not provided , even though prefixed with operator they are treated as 2 different elements in a matrix. That's how matlab evaluates expressions and interprets the operators
Mario Malic
Mario Malic el 11 de Mayo de 2023
Editada: Mario Malic el 11 de Mayo de 2023
Oh my... I thought I was going crazy. I should take some time off. 😂
Thank you.
VBBV
VBBV el 11 de Mayo de 2023
Editada: VBBV el 11 de Mayo de 2023
As you said, it works when parenthesis is added, it's again because of operator precedence. Parenthesis () operator has the higher precedence in equation than others, so when you add a () it then delineates everything within the outermost () as ONE expression or element in matrix and evaluates it, otherwise it's treated as 2 different elements

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Fuzzy Logic Toolbox en Centro de ayuda y File Exchange.

Productos

Versión

R2023a

Etiquetas

Preguntada:

el 11 de Mayo de 2023

Editada:

el 11 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by