%% Calculate kinetic energy of a single-link pendulum.
syms Iner M L theta thetad
  
%% Moment of inertia of first link about center of mass.
%  Thin rod formula.
Iner = (1/12) * M * L^2;

%% Position of center of mass of first link.
com1 = (L/2) * [ cos(theta); sin(theta) ];

%% Angle of first link.
angle1 = theta;

%% Velocity of center of mass of first link.
%  Notice this is done automatically using jacobian() and the chain rule.
%  Once we define the positions of the centers of mass of the
%  bodies, and their angles, we do not to calculate the velocities
%  by hand.
com1d = jacobian( com1, [theta] ) * [thetad];

%% Angular velocity of first link.
angle1d = jacobian(angle1, [theta]) * [thetad];

%% Kinetic energy
energy1 = 1/2 * M * sum(com1d.^2) + 1/2 * Iner * angle1d^2;
energy1s = simplify(energy1)

% That's how it's done.
