Week 3 - Solving second order ODEs
AIM: - To write a MATLAB program for motion of pendulum
A mechanical system in which oscillatory motion takes place is known as the simple pendulum. In this system, a point mass ‘m’ is present which is suspended by means of long thread that is inextensible with a string of length ‘L’ from a fixed rigid support. The gravitational force drives the motion in a simple pendulum and it occurs in a vertical plane. Here mg sin θ is the tangential component of the gravitational force, which is always acted towards the mean position where θ=0, that is, opposite to the displacement, and a restoring force is acted upon the arc.

Equation used:-

where..
b = damping coefficient
g = acceleration due to gravity
m = mass of a bob
L = length of string
Matlab Program
fuction code: -
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
To get plot and animation of motion of pendulum
close all
clear
clc
%inputs
L = 1; % length of a pendulum in m
m = 1; % mass of a ball in kg
b = 0.05; % damping coefficient
g = 9.81; % gravity in m/s^2
% initial condition
theta_0 = [0;3];
% time points
t_span = linspace(0,20,800);
% solving ODE
[t,results] = ode45(@(t,theta)ode_func(t,theta,b,g,L,m),t_span,theta_0);
figure(1)
plot(t,results(:,1),'linewidth',2);
hold on
plot(t,results(:,2),'linewidth',2)
axis([0 20 -4 4]);
xlabel('time(sec)');
ylabel('Angular displacement and angular velocity');
grid on
title('damping effect in simple pendulum');
legend('displacement','velocity')
ct = 1;
for i = 1:length(results)
y1 = -L*cos(results(i,1));
x1 = L*sin(results(i,1));
hold on;
figure(2)
fill([-2 2 2 -2],[-2 -2 2 2],'w');
plot([-0.5 0.5],[0 0],'linewidth',3,'color','r')
plot([0 0],[0 0],'.','color','k')
plot([0 L*sin(results(i,1))],[0 -L*cos(results(i,1))],'k','linewidth',3);
plot(L*sin(results(i,1)),-L*cos(results(i,1)),'Marker','o','Markersize',20,'Markerfacecolor','b','Markeredgecolor','b');
xlim([-1.2 1.2]);
ylim([-1.5 0.2]);
title('Motion of pendulum')
M(ct) = getframe(gcf);
ct = ct + 1;
end
movie(M)
videofile = VideoWriter('Pendulum_Motion.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
EXPLANATION
1. starting with close all,clear and clc command to end any program running.
2. enterd all parameters like length, mass,gravity,damping coefficient,angle(in theta) and time limit.
3. Then creating a function to required euation.
4.ODE45 command is added to our code to solve system of equation.
5.Figure 1. Plot is made between angular displacement, angular velocity and time.
6. Loop function is used to show the motion of pendulum and record a movie of it.
7.fill command is used here to cover the background color.
8. Then a line, string and bob is created through command. marker is used for making aball.
9.xlim amd ylim , command is used to get the x and y axis.
OUTPUT PLOT WE GET

Here above is the plot of time, velocity and displacement. red lines are velocity with initial condition, blue is the displacement.
Errors: -
getting a shadow of pendulum.
corrected by adding a cammand line before line number 38.
