Challenge

Solving second order Ordinary Differential equations using MATLAB

Objective: Write a program to solve an ODE representing the motion of a Simple Pendulum

Equation for the motion of a simple pendulum:

`(d^2(theta))/dt^2+b/m*(d(theta)/dt)+(g/l)*sintheta=0`

Pendulum characteristics:

Length = 1 meter

mass = 0.1 kg

b = 0.1

gravity = 9.81 m/s^2

The motion is analysed for a totle time of 20 seconds

Initial angular displacement of pendulum = 0 radian

Initial angular velocity of pendulum = 3 radian/sec

Procedure:

  1. The angular velocity, length and mass are specified for the simple pendulum
  2. The lsode function is called to solve for the position vs time function of the simple Pendulum
  3. The parameters obtained from lsode function are then used as outputs and plots are made

Main plotting function for position of Simple Pendulum:

clear all
close all
clc

theta_0=[0;3];
l=1;
t_span= linspace(0,20,50)
[results]=lsode('pendulum_ode',theta_0,t_span);

subplot(5,4,[13,14,17,18]);
plot(t_span,results(:,1),'color','b');
hold on;
grid on;
plot(t_span,results(:,2),'color','r');
hold off;

subplot(5,4,[15,16,19,20]);
plot(results(:,1),results(:,2));
xlabel('position-radians');
ylabel('angular velocity');


ct=1;
for i=1:length(results(:,1));
x0=0;
y0=0;
xl=l*sin(results(i,1));
yl=-l*cos(results(i,1));
subplot(5,4,[1,2,3,4,5,6,7,8,9,10,11,12]);
plot([-1 1],[0 0],'linewidth',4,'color','k');
hold on
line([x0 xl],[y0 yl],'linewidth',2,'color','b');
hold on

plot(xl,yl,'-o','markers',20,'markerfacecolor','r');
grid on;
axis([-1.5 1.5 -1.5 0.5])
pause(0.1);
hold off
text=sprintf("pendulum%d.jpg",ct);
saveas(1,text);
ct=ct+1;
end

The lsode function code :

function [theta_dot]=pendulum_ode(theta,t);

l=1;
m=1;
b=0.05;
g=9.81;

theta_dot=zeros(2,1);
theta_dot(1)=theta(2);
theta_dot(2)=((-b/m)*theta(2))-((g/l)*sin(theta(1)));
end

Results:

The motion of the Simple Pendulum is shown in the video below:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.