MATLAB Examples

Dynamics of a Single Link

This example illustrates how to solve a second order differential equation using one of the numeric ODE solvers in MATLAB.


Solve the second order differential equation

Notice that a second order differential equation has to be presented to the ODE solver as a system of two first order ordinary differential equations:

$dy_1/dt = y_2$

$dy_2/dt = -3g cos(y_1)/2\rho$

where: $y_1 = \theta, y_2 = \theta'$

Define the equation parameters

rho = 1;        % link length [m]
g = 9.81;       % gravity [m/s2]

Call one of the MATLAB ODE solvers

[t,y] = ode23(@(t,y) [y(2);-3*g*cos(y(1))/2/rho],[0 5],[pi/4,0]);

Replace the name of the output vector with the original state names

theta = y(:,1);
thetaprime = y(:,2);

Visualize the results

title('Time histories for \theta and \theta'' - Single Link');
xlabel('time (sec)');