MATLAB function dynamic model

조회 수: 2 (최근 30일)
Christi Louw
Christi Louw 2020년 4월 9일
답변: Ameer Hamza 2020년 4월 9일
I am trying to simulate a double pendulum on a gantry using simulink. The MATLAB function allows you to define the model. i'm using matrices.
I'm trying to get the displacement (x, theta1, theta2) but simulink keeps on giving me the same error no matter how I arrange the blocks.
Error in default port dimensions function of S-function 'double_pendulum/MATLAB total'. This function does not fully set the dimensions of output port 2
MY FUNCTION DECLARATION:
function qdot2 = fcn(q,qdot,U)
m = 1.5; %kg
m1 = 0.5; %kg
m2 = 0.4;%kg
l1 = 0.5;%m
l2 = 0.4; %m
g = 9.81; %m/s^2
x = q(1,1);
theta1 = q(2,1);
theta2 = q(3,1);
xdot = qdot(1,1);
theta1dot = qdot(2,1);
theta2dot = qdot(3,1);
M = [(m + m1 + m2) ((m1 + m2)*l1*cos(theta1)) (m2*l2*cos(theta2)); ((m1+ m2)*l1*cos(theta1)) ((m1+ m2)*(l1^2)) (m2*l1*l2*cos(theta1 - theta2)); (m2*l2*cos(theta2)) (m2*l1*l2*cos(theta1 - theta2)) (m2*(l2^2))];
Vm = [0 (-(m1+m2)*l1*theta1dot*sin(theta1)) (-m2*l2*theta2dot*sin(theta2)); 0 0 (m2*l1*l2*theta2dot*sin(theta1-theta2)); 0 (-m2*l1*l2*theta2dot*sin(theta1-theta2)) 0];
Vmq = Vm*(qdot);
G = [0; ((m1 + m2)*g*l1*sin(theta1)); (m2*g*l2*sin(theta2))];
F = U(1,1);
qdot2 = -(inv(M))*(Vmq + G - F);
end

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 9일
Start the function like this
function qdot2 = fcn(q,qdot,U)
qdot2 = zeros(3,1); % <---- add this line. Initialization is important in simulink
m = 1.5; %kg
m1 = 0.5; %kg
m2 = 0.4;%kg

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Modeling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by