Week 3 - Solving second order ODEs
Aim:
Write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping
Write a program in MatlabOctave that will simulate the pendulum motion.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
use,
L=1 metre
m=1 kg
b=0.05
g=9.81 m/s^2
When you do this you will get the position of the pendulum as a function of time. Use this information to animate the motion of the pendulum.
Theory:
In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum

The way the pendulum moves depends on the Newtons second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "ball" w.r.t time.
The second order ode of simple pendulum is defind as

where,
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Now breaking down the 2nd order ode into two 1st order ode
Now we have defined θ1 and θ2.
## Code for solving the 2nd order ode of simple pendulum and simulating it ##
% First the code of the function
function[dtheta_dt] = ode_func(t,theta,b,g,l,m) % output of the function is dθ/dt
theta1 = theta(1); % θ1
theta2 = theta(2); % θ2
dtheta1_dt = theta2; % dθ1/dt = θ2
dtheta2_dt = -(b/m)*theta2 -(g/l)*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt]; % dθ/dt = [dθ1/dt; dθ2/dt]
end

% g=9.81 m/s^2
% l=1 m
% m=1 kg
clear all
close all
clc
b = 0.05;
g = 9.81;
l = 1;
m = 1;
theta_0 = [0;3]; % 0 is for displacment and 3 is for velocity
t_span = linspace(0,10,250);
[t,result] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
hold on
plot(t,result(:,1))
plot(t,result(:,2))
xlabel('Time')
ylabel('Displacement and Velocity')
legend('Angular displacement','Angular Velocity')

ct = 1;
for i = 1:length(result(:,1))
x0 = 0;
y0 = 0;
% co ordinates for the swinging point of the pendulum fixed to the massx1 = l*sin(result(i,1));
y1 = -l*cos(result(i,1));
figure(2)
plot([-5 5],[0 0],'color','k',LineWidth=5) % fixed support
axis([-2 2 -1.5 0.5]) % specifying the axis so it does not move for diffrent frames
hold on
plot([x0 x1],[y0 y1],'color','b')
plot(x1,y1,'marker','o','markerfacecolor','r','color','b','markersize',20)
pause (0.001)
hold off
M(ct) = getframe(gcf);
ct = ct +1;
end
movie(M)
videofile_pendulum_oscillation = VideoWriter('videofile_pendulum_oscillation.avi','Uncompressed AVI');
open(videofile_pendulum_oscillation)
writeVideo(videofile_pendulum_oscillation,M)
close(videofile_pendulum_oscillation)



