image thumbnail

Comparing different methods to solve an Inverted Pendulum problem

by

 

A simple reporting tool that shows how you can model an inverted pendulum in a few methods.

Dynamics of a Single Link

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.

Contents

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

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');

Contact us