Week 3 - Solving second order ODEs
Aim :
To write a program that solves the given ODE and create animation video.
Explanation :
1. The given parameters b, g, m, l values are entered.
2. Initial condition of pendulum angular displacement and angular velocity values also entered
3.Time interval of 20 sec with 500 value are given
4.To solve ODE, odefunction is used to create the result of time and ode colume 1 and colume 2 is plotted in line 18 & 20.
5.To Create animation, The origin of pendulum coordinates ( 0,0 ) is entered.
6. For loop command is used AND x1 y1 value are calculated
7. Counter is used to create the counts of every loop.
8. Plotting, The support beam coordinates value
9. Plotting, The coordinates of the length of the rod.
10. Using Marker size command ball size shape is plotted.
11. Frame will be played in sequence and created as a movie.
12. The movie is named as Using videoWriter command and saved in default folder.
Error :
1. Used - minus for both x1 y1 coordinates.
2. Video played inpendulum moves in upwards
3.writevideo v is in small case
Rectify :
after discussing with support engineer, I have been rectified all the errors
Video link :
https://drive.google.com/file/d/1wt3vC71hI4P65Z7lWSJYhXBIQmzVBZv1/view?usp=sharing
Error screen shot:

Program:
clear all
close all
clc
b = 0.05;
g = 9.81;
l = 1;
m = 1;
%initial condition
theta_0 = [0;3];
%time points
t_span = linspace(0,20,500);
%solve ODE
[t,results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
plot(t,results(:,1),'linewidth',2);
hold on
plot(t,results(:,2),'linewidth',2);
ylabel('Angular displacement')
xlabel('Time')
legend({'angle (rad)','angular speed (rad/s)'})
title('simple pendulum motion');
%%Animation
X0 = 0;
Y0 = 0;
ct = 1;
for i = 1:length(t_span);
X1 = l*sin(results(i,1));
Y1 = - l*cos(results(i,1));
figure(2)
plot([-1 1],[0 0],'linewidth',6,'color','g');
axis([-2 2 -2 2]);
line([X0 X1],[Y0 Y1],'linewidth',2,'color','b');
hold on;
plot(X0,Y0,'O','markersize',3,'markerfacecolor','y');
plot(X1,Y1,'O','markersize',20,'markerfacecolor','r');
title('damping of simple pendulum');
legend('support beam','length rod','Hinge joint','Ball of pendulum');
grid on;
hold off;
M(ct)=getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter('simple_pendulum','uncompressed 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