Week 3 - Solving second order ODEs
AIM:
In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum.
Objective:
to write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping
SIMPLE PENDULUM:
The simple pendulum is another mechanical system that moves in an oscillatory motion. It consists of a point mass 'm' suspended by means of light inextensible string of length L from a fixed support as shown in Fig. 2.8. The motion occurs in a vertical plane and is driven by a gravitational force.

FUNCTION:
function [dtheta_dt]=ode_func(t,theta,b,g,l,m)
theta1 = theta(1)
theta2 = theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
CODE:
clear all
close all
clc
% inputs
b=0.5;
g=9.81;
l=1;
m=0.1;
%Intial condition
theta_0=[0;5];
%time poins
t_span=linspace(0,20,500);
%ODE is
[t,result]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%ploting
plot(t,result(:,1))
hold on
plot(t,result(:,2))
xlabel('plot');
ylabel('time');
%Animation
ct=1;
theta_1=result(:,1);
for i=1:length(result(:,1))
% initial coordinates
xstart=0;
ystart=0;
theta_2=theta_1(i);
%final coordinates
xend=1*sin(theta_2);
yend=-1*cos(theta_2);
%plotting
figure(2)
%pendulam base line
plot([-1,1],[0,0],'linewidth',3,'color','black')
hold on
%start point of pendulam
line([xstart xend],[ystart yend],'linewidth',6,'color','b')
hold on
%string of pendulam
plot(xend,yend,'marker','o','MarkerSize',20,'MarkerFaceColor','r')
axis([-2 2 -2 2])
pause(0.3)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
% Animation of pendulam
movie(M);
videofile=videowriter('simplependulam.avi','uncompressed avi');
open(videofile);
writevideo(videofile,M);
close(videofile);
RESULTS:
ploting

Animation:

links: