Week 3 - Solving second order ODEs
Aim:- To create an animation of simple pendulum by using second order ODE by using ODE solver function.
To create plot between Angular velocity and displacements of simple pendulum by using ODE 45

Objects :-
- Solve ODE which represent the equation of motion of simple pendulum damping with the help of MATLAB code
- Plotting the graph of variation of angular displacements VS time and angular velocity vs time.
- Animation of the plot for the motion of pendulum bet 0-20sec and Angular displacements =0
Angular velocity =3red/sec at time =0
b = Damping coefficient (N- m/s)
m =Mass of the ball (kg)
g = Gravitational acceleration (m/s)
l- length of pendulum I (m)
clear all
close all
clc
Given initial condition in question
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('Angular displacement vs Time and angular velocity vs Time ')
legend('angular displacement in red','Angular velosity in red/sec')
Plot of Angular displacements vs time and angular velocity vs time.

% 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 from capture frome
movie(M)
videofile = VideoWriter('simple_pendulum.avi');
open(videofile);
writeVideo(videofile,M);
close(videofile);
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

Missing variable in line 22 – dtheta_dt =theta2 against dtheta1_dt=theta2

You tube link
https://youtu.be/ytHx_6tkfSA
https://drive.google.com/file/d/1zosldVcFzl0EGSte5mQOig3KdaJCYB70/view?usp=sharing