Direct Dynamics of Two-Link Manipulator

Hi,
I am unsuccesful in using a MATLAB function block in Simulink to model the direct dynamics of a two-link planar manipulator. The function takes the gear ratio (η), 2x1 motor torque (τ), angular position ($v$) and angular velocity ($\dot v$) and outputs the angular acceleration ($\ddot v$). See below for my Simulink model.The torque step function has a step time of 0.1, initial value of [0;0]. final value of [15;10] and also a sample time of 0.1. η = 10 is a constant. The first integrator has initial conditions [-pi/2,0] and the second has initial conditions of [0,0]. I am outputting the torque, joint velocity and the joint position to the workspace.
I am getting the correct vector for torque: $[\tao_1, \tao_2] = [0,0]$ at time 0 and $[\tao_1, \tao_2] = [15,10]$ after that. However the joint 1 position is decreasing from 0 to -15 and the joint 2 position is staying at 0. The joint 1 and 2 velocities are staying constant at the initial velocity of -pi/2,0. I have pasted my code that I have gone over numerous times so I doubt there is any issue there. I think the issue is more in the setup for the Simulink.
Was wondering if people can help pinpoint the problem and help lead me in the right direction.
function v_ddot = dynamics(n, t_m, v_dot, v)
%given constants
ml1 = 49; ml2 = 34;
a1 = 1; a2 = 1;
l1 = 0.5; l2 = 0.5;
Iyy_1 = 4.08; Iyy_2 = 2.83;
Im1 = 0.25; Im2 = 0.25;
b1 = 12; b2 = 12;
bm1 = 0.08; bm2 = 0.08;
g = 9.8;
%calculations for lumped parameters I1 and I2
I1 = Iyy_1 + ml1 * l1^2 + ml2 * a1^2;
I2 = Iyy_2 + ml2 * l2^2;
h = ml2*a1*l2;
%mass, centrifugal/coriolis, gravitational, friction vectors
M = [n^2*Im1 + I1 + I2 + 2*h*cos(v(2)), I2 + h*cos(v(2)); I2 + h*cos(v(2)), n^2*Im2 + I2];
V = h*sin(v(2))*[-2*v_dot(1)*v_dot(2) - v_dot(2)^2; v_dot(1)^2];
G = [(ml1*l1 + ml2*l2)*cos(v(1))*g + ml2*l2*cos(v(1)+v(2))*g; ml2*l2*cos(v(1)+v(2))*g];
F = [b1*v_dot(1) + n^2*bm1; b2*v_dot(2) + n^2*bm2*v_dot(2)];
%solving for the angualar accelerations
v_ddot = M \ (-V - G - F + t_m * n);
end

Answers (1)

Paul
Paul on 22 Mar 2026 at 13:03
Edited: Paul on 22 Mar 2026 at 13:12
It looks like the Matlab Function block is commented. Right click on it and then click Uncomment on the pop-up menu.

3 Comments

Thank you, that seemed to work but now the JointPos and JointVel arrays are structured such that both the elements are within the same array indices (see below)? How do I get it that they are in seperate rows? I have the 'To Workspace' parameters structured so that their save format is a 2-D array.
Paul
Paul on 22 Mar 2026 at 15:15
Edited: Paul on 22 Mar 2026 at 16:33
In the To Workspace blocks you have "Save format" set to Array and "Save 2D signals" set to 2-D Array (concatenate along first dimension) ? That doesn't work for me either. Maybe that's a bug (I just submitted it). Though if it worked the way it should, it still wouldn't yield what you want because the input signals are, I think, 2 x 1 (not 1 x 2).
Verify the signal dimensions by going to Debug -> Diagnostics -> Information Overlays -> Signal Dimensions. Then update the diagram. I suspect that we'll see the the dimensions on the inputs to those blocks as 2 x 1, instead of 2.
If we can't get the To Workspace blocks to operate as they should, I guess we can use a Signal Specification block at the To Workspace inputs to force the Dimensions to be 2 (rather than 1x2 or 2x1).
Or, in the Step block, is the "Interpret vector parameters as 1D" checked or not? If it's not, make sure it is, unless you really want 2D signals.
Or leave the model as is and use reshape (and any other operations as needed) the results into the dimensions you want after the simulation finishes.
Response from tech support is that the combination of "Save format" = Array and "Save 2D signals" = 2-D Array (concatenate along first dimension) does not work as advertised if the solver is variable step and the signal input to the To Workspace block has a continuous or FiM sample time.

Sign in to comment.

Categories

Find more on General Applications in Help Center and File Exchange

Tags

Asked:

on 22 Mar 2026 at 4:55

Commented:

on 24 Mar 2026 at 21:12

Community Treasure Hunt

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

Start Hunting!