Week 3 - Solving second order ODEs
Simple pendulum:
A simple pendulum is one that can be considered to be a point mass suspended from a string or rod of negligible mass. It is a resonant system with a single resonant frequency. Pendulum, body suspended from a fixed point so that it can swing back and forth under the influence of gravity. These are used to regulate the movement of clocks because of the interval of time for each complete oscillation. The time taken by a simple pendulum to complete the one complete oscillation is known as the Time period of a simple pendulum.

Given equation
`(d^2theta)/(dt)^2 + (b/m)((d theta)/dt) + (gsintheta)/l = 0`
we assume that
1. `theta = theta_1`
2. `(d theta)/(dt) = (d theta_1)/(dt) = theta_2`
3. Now from the above equations we can written as `(d^2 theta)/(dt^2) = (d theta_2)/(dt)`
Substituting the above 3 equations in the given equation, we get
4. `(d theta_2)/(dt) = -(b/m)theta_2 - (g/l)sintheta_1`
we are mentioned these 4 equations in the functions code.
Given inputs:
Length of the pendulum(L) = 1m;
Damping coefficient(b) = 0.05;
Mass of the ball(m) = 1kg;
Gravity(g) = 9.81 `m/s^2`,
Time(t) = 0,
Angular displacement = 0;
Angular velocity = 3 rad/sec.
Code:
clear all
close all
clc
% inputs
b = 0.05;
l = 1;
g = 9.81;
m = 0.1;
% initial conditions
theta0 = [0;3];
% time points
t_spam = linspace(0,10,500);
% solve ODE
[t,results] = ode45(@(t,theta) ode_func(t, theta, b,g,m,l),t_spam,theta0);
figure(1)
plot(t,results(:,1),'linewidth',3)
hold on
plot(t,results(:,2),'linewidth',3)
grid on
xlabel('time')
ylabel('plot')
% Animation
ct = 1;
for i = 1:length(results(:,1))
x0 = 0;
y0 = 0;
x1 = l*sin(results(i,1));
y1 = -l*cos(results(i,1));
figure(2)
plot([-0.3 0.3],[0 0],'linewidth',4);
hold on
plot([x0,x1],[y0,y1],'color','r','linewidth',3);
hold on
plot(x1,y1,'marker','o','markersize',30,'markerfacecolor','g');
axis([-1 1 -1.5 0]);
hold off
M(ct) = getframe(gcf);
ct = ct+1;
pause(0.03);
end
movie(M);
videofile = VideoWriter('ode_week3.avi','uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
Function:
function [dtheta_dt] = ode_func(t, theta, b,g,m,l)
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
Result:


Animation link:
https://www.youtube.com/watch?v=LNn7iyc9bp0&ab_channel=Harsha