Week 3 - Solving second order ODEs
Project report
Aim :
writing a program in matlab that will simulate the pendulum motion.
objectives :
To write a program that solves the following ODE.This ODI represents the equation of motion of a simple pendulum with damping

In the above equation,
g=gravity in m/s2
L=length of the pendulum in m
m=mass of the ball in kg
b=damping coefficient
Theory
A pendulum is a weight suspended from a pivot so that it can swing freely. When a pendulum is displaced sideways with from its resting, equilibrium position, it is subject to restoring force due to gravity that will accelerate it back toward the equilibrium position.When released, the restoring force acting on the pendulum's mass causes it to oscillate about the equilibrium position, swinging back and forth.The time for one complete cycle, a left wing and right wing, is called the period. The period depends on the length of the pendulum and also to a slight degree on the amplitude, the width of the pendulum's swing.

A simple pendulum consists of mass m hanging from a string of length L and fixed at pivot point P. When displaced to an initial angle and released, the pendulum will bring back and forth with periodic motion.
Program:
Matlab program that will simulate the pendulum motion
clear all
close all
clc
%inputs
b=0.05;
g=9.81;
l=1;
m=1;
% initial condition
theta_0=[0;2];
% time point
t_span=linspace(0,20,500);
% slove ODE
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%ploting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('plot')
grid on
title('Pendulum Motion')
% iteration using for loop to create
ct=1;
theta_1 = results(:,1);
for i = 1:length(results(:,1))
% initial coordinates
x_start = 0;
y_start = 0;
theta_2 = theta_1(i);
% final coordinates
x_end = 1*sin(theta_2);
y_end = -1*cos(theta_2);
% ploting for the animation
figure(2)
% base of pendulum
plot([-1,1],[0,0],'linewidth',4,'color','k')
hold on
% string the pendulum
line([x_start x_end],[y_start y_end],'linewidth',3,'color','b');
hold on
%bob of pendulum
plot(x_end,y_end,'marker','o','markersize',28,'markerfacecolor','r');
axis ([-2 2 -2 2 ])
grid on
% pause(0.03)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
% creating movie
movie(M)
videofile = VideoWriter('simple_pendulum.avi');
open(videofile);
writeVideo(videofile,M);
close(videofile);
Function call:
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
Output:

YouTube Link for an animation in Report:
https://drive.google.com/file/d/1ZKaq3bNfS-hV80cGI9_Tlr6eCfzPrDch/view?usp=sharing
Errors occured while coding:
1.Unrecognized function or variable 'ct'
Error in pendulum(line 54)
Solution - I was not added ct value in code,due to this pendulum is not moving.After occuring error I added ct value in code.

Conclusion :
1.Different equation of simple pendulum is studied
2.The different commands used to create tghe pendulum animation is also understood