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:

where:

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

figure; plot(t,theta,'r',t,thetaprime,'g',... 'DisplayName',{'\theta';'\theta'''},'LineWidth',2); title('Time histories for \theta and \theta'' - Single Link'); xlabel('time (sec)'); legend('show'); grid('on');