Not your typical vertcat error. Weird behaviour with it.
조회 수: 2 (최근 30일)
이전 댓글 표시
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)
(-((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
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];
댓글 수: 0
채택된 답변
VBBV
2023년 5월 11일
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
x(2)
(-((k1+k2)*x(1))/m1) + ((k2*x(3))/m1) - (((d1+d2)*x(2))/m1) + ((d2*x(4))/m1) +u(1)
x(4)
x(3)
x(4)
(d2*x(2)/m2) - (d2*x(4)/m2) + (k2*x(1)/m2) - (k2*x(3)/m2) + 0
(-((k1+k2)*x(1))/m1) + ((k2*x(3))/m1) - (((d1+d2)*x(2))/m1) + ((d2*x(4))/m1) +u(1)
(d2*x(2)/m2) - (d2*x(4)/m2) + (k2*x(1)/m2) - (k2*x(3)/m2) + 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]
댓글 수: 5
VBBV
2023년 5월 11일
편집: VBBV
2023년 5월 11일
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!