clear all
close all
clc
b = 0.05 %damping
g = 9.81 %gravity (m/s^2)
l = 1 %length of the spring (m)
m = 1 %mass of the bob(kg)
%initial condition
%angular displacement is 0 and angular velocity is 3
theta_0 =[0 3];
%time span
%pendulam oscillates between 0-20 seconds
t_span = linspace(0,20,100)
% solving ODE
[t, results] = ode45(@(t,theta) ode_function(t,theta, b,g,l,m), t_span, theta_0);
%plotting
figure(1)
hold on
plot(t, results(:,1))
plot(t, results(:,2))
title('damped motion of a pendulam')
legend('angular displacement','angular velocity')
xlabel('time(second)')
ylabel('plot')
% for loop
%contor_command
ct =1;
for i=1:length(results(:,1))
x_start = 0
y_start = 0
x_end = 1*sin(results(i,1))
y_end = -1*cos(results(i,1))
% plotting the animation
figure(2)
plot([-1 1],[0 0],'LineWidth',5)
hold on
plot([x_start x_end],[y_start y_end],'LineWidth',3)
hold on
plot(x_end,y_end,'o','Markers',10,'MarkerFaceColor','y')
title('animation of moving a pendulam')
axis([ -1.5 0.5 -1.5 1])
pause(0.02)
hold off
M(ct) = getframe(gcf);
ct = ct+1;
end
%animation of the pendulam
movie(M)
videofile=VideoWriter('pendulam animation.avi', 'uncompressed avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
function [dtheta_dt] =ode_function(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
https://drive.google.com/file/d/1wfow59Y6qSqLby9OL_Nv94I6JJLReGPf/view?usp=share_link
Aim: To solves the ODE which represents the equation of motion of a simple pendulum with damping.
Objective:
To write a program that solves the following ODE which represents the equation of motion of a simple pendulum with damping and create an animated video of output obtains by solving this ODE.

Introduction:
A simple pendulam consists of a point mass suspended on a string or wire t6has mass.If the pendulam bob is pulled to some angle from vertical and let it go, it will swing back.
Movement of the pendulum depends on Newton's second law. When this law is written down, a second-order Ordinary Differential Equation is formed that describes the position of the end of the pendulum.
Derivation for code simplification –


Code Explanation:-
The function code is shown