MATLAB function dynamic model

4 views (last 30 days)
Christi Louw
Christi Louw on 9 Apr 2020
Answered: Ameer Hamza on 9 Apr 2020
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

Accepted Answer

Ameer Hamza
Ameer Hamza on 9 Apr 2020
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

More Answers (0)

Categories

Find more on General Applications in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!