Week 3 - Solving second order ODEs
PROGRAM TO SOLVE THE GIVEN ODEs
clear all
close all
clc
L = 1
m = 1
b = 0.05
g = 9.81
theta_0 = [0;3]
t_span = linspace(0,20,200)
[t, x] = ode45(@(t,theta) ode_func(t,theta,L,m,b,g),t_span, theta_0);
plot(t,x(:,1))
hold on
plot(t,x(:,2))
ylabel('Plot')
xlabel('Plot')
X0 = 0
Y0 = 0
ct = 1
for i = 1:length(t)
X1 = L*sin(x(i,1))
Y1 = -L*cos(x(i,1))
figure(2)
plot([-1 1],[0 0],'linewidth',6,'color','g')
axis([-2 2 -2 2])
line([X0 X1],[Y0 Y1],'color','b')
hold on
plot(X0,Y0,'O','markersize',8,'markerfacecolor','y')
plot(X1,Y1,'O','markersize',20,'markerfacecolor','r')
title('Damping of simple pendulum')
legend('Support rod','length of pendulum','Fixed point','Bob of pendulum')
grid on
hold off
M(ct)=getframe(gcf)
ct=ct+1
end
movie(M)
videofile=VideoWriter('Simple_Pendulum','AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
function [dtheta_dt] = ode_func(t,theta,l,m,b,g)
theta1 = theta(1)
theta2 = theta(2)
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2 - (g/1)*sin(theta1)
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end


Code to solve second-order ODEs and to animate the motion of the pendulum
- First, we will provide different parameters of the pendulum
- Then we will provide the ode function and we will provide useful information in function command in another script
- After that, we will use the plot command to plot it
- And now to make animation we will use the for and counter command
- After that to make a horizontal line we will use the plot command
- Then we will use line command to plot vertical line
- Then we will use the marker command to make bob of the sphere
- After defining the lines in the graph we will use the movie command to make a movie
The error faced in programming was while using line command to plot the vertical line of the pendulum in which due to wrong coordinates the line was not moving along with bob. The problem was overcome by reading the code again and to reduce further error I was paying attention. The error has been shared below.

Youtube link of animation
https://youtu.be/vBtg5weG2qM