Week 3 - Solving second order ODEs
AIM:
To write a program that solves the following ODE
THE GIVEN VALUES ARE:

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.
Write a program in Matlab\Octave that will simulate the pendulum motion, just like the one shown at the start of this challenge.
use,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
OBJECTIVE: The time period of the simple pendulum is directly proportional to the square root of its length. ... This property is known as the law of length. The time period of a simple pendulum is inversely proportional to the square root of the acceleration due to gravity at that place.
The goal of this experiment was to determine the effect of mass and length on the period of oscillation of a simple pendulum. Using a photogate to measure the period, we varied the pendulum mass for a fixed length and varied the pendulum length for a fixed mass.
There are four laws of a simple pendulum. Each law gives information on how the physical quantity of the simple pendulum varies with each other.
The surprising conclusion - the pendulum traverses a longer distance in a shorter time than in a shorter distance, and its period is shorter. ... This friction slowed down the pendulum's faster movements and led to a decrease in the length of the arc through which it passed.
CARRY OUT A SIMPLE PENDULUM EXPERIMENT:
Cut a piece of a string or dental floss so that it is about 1 m long. Attach a small object of high density to the end of the string (for example, a metal nut or a car key). Starting at an angle of less than 10º, allow the pendulum to swing and measure the pendulum's period for 10 oscillations using a stopwatch.

SECOND-ORDER ODE(ORDINARY DIFFERENTIAL EQUATIONS):
The order of a differential equation is the highest-order derivative that it involves. Thus, a second order differential equation is one in which there is a second derivative but not a third or higher derivative.

HERE WE GO FOR THE CODE:
CODE FOR THE PENDULUM MOTION:
%%program to solve 2nd order ODE of a pendulum
clear all
close all
clc
%constant inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;
%initial condition
theta_0 =[0,3]; %initial displacement is 0m and intial velocity is 5m/s
%time points
t_span =linspace(0,20,500);
%solving the ode
[t,results] =ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting the results(displacement and velocity)
figure(1)
plot(t,results(:,1));
hold on
plot(t,results(:,2));
xlabel('time(s)')
ylabel('displacement and velocity vs time');
hold off
%animation of pendulum motion
pos = results(:,1);
figure(2)
count = 1;
for i = 1:length(pos)
x0 = 0;
y0 = 0;
xpos = l*sin(pos(i));
ypos = -1*cos(pos(1));
plot([x0 xpos], [y0 ypos])
hold on
plot(x0, y0, 's', 'Markersize', 20)
plot(xpos, ypos, '.', 'Markersize', 50)
axis([-2 2 -2 2]);
hold off
M(count) = getframe(gcf);
count = count+1;
end
movie(M);
videofile=VideoWriter('pendulum.avi','uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
CODE FOR THE PENDULUM:
function[dtheta_dt]=ode_func(t, theta, b, g, l, m)
theta1=theta(1);
theta2=theta(2);
dtheta1=theta2;
dtheta2=-b/m*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1;dtheta2];
end
THE GRAPH SHOWS THE SIMPLE PENDULUM MOTION:

ERRORS I'VE FACED:

I have entered "time" instead of using "t_span"
YOUTUBE VIDEO LINK:
https://youtu.be/EyE4yBfu65g