Week 3 - Solving second order ODEs
%write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping
clear
close
clc
%state all input variables
b=0.5; %damping coefficient
g=10; %gravity in m/s2
l=0.75; %length of pendulum
m=1; %mass of ball in kg
%initial conditions angular displacement and velocity
theta_0=[0;5];
%time input
t_span=linspace(90,10,400);
%%solving the differential equation
[t,results]=ode45(@(t,theta) ode_func(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 harmonic motion of a pendulum');
legend('angular displacement','angular velocity');
xlabel('plot')
ylabel('time in sec')
% LOOPING
ct=1;
for i =1:length(results(:,1))
%initial coordinates
x0=0;
y0=0;
%final coordinates
x1= l*sind(results(i,1)); %by using trignometric calculations
y1=-l*cosd(results(i,1));
%plotting for animation
figure(2)
%base of pendulum
plot([-1,1],[0,0],'linewidth',4,'color','black');
hold on
plot([x0 x1],[y0 y1],'linewidth',3);
hold on
plot(x1,y1,'o','markersize',20,'markerfacecolor','y');
title('animation of pendulum');
axis([-2 2 -2 2]);
pause(0.03);
hold off;
M(ct)=getframe(gcf);
ct=ct+1;
end
%creating movie from captured frames
movie(M)
videofile=VideoWriter('simple_pendulum.avi','Uncompressed AVI');
open(videofile);
WriteVideo(videofile,M);
close(videofile)
%REPORT
EXPLANATION OF CODE
clear
close
clc
(1)Initially we have to assign a inputs
1.damping coefficient(b)
2.mass of ball(m)
3.gravity(g)
4.length of pendulum(l)
(2)Next we have to give initial conditions for angular displacement and velocity
theta_0=[0;5];
(3)t_span=linspace(90,10,400)
(4)%solving the differential equation
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
and also we have descript the function in new script.The main thing is function name and script file name must be same
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
we have to create loop for ode function
ct=1;

for i =1:length(results(:,1))
%initial coordinates
x0=0;
y0=0;
%final coordinates
x1= l*sind(results(i,1)); %by using trignometric calculations
y1=-l*cosd(results(i,1));
%%then plot the initial and final coordinates
figure(2)
%base of pendulum
plot([-1,1],[0,0],'linewidth',4,'color','black');
hold on
plot([x0 x1],[y0 y1],'linewidth',3);
hold on
plot(x1,y1,'o','markersize',20,'markerfacecolor','y');
title('animation of pendulum');
axis([-2 2 -2 2]);
pause(0.03);
hold off;
M(ct)=getframe(gcf);
ct=ct+1;
end
%for creating movie file we know the coding
movie(M)
videofile=VideoWriter('simple_pendulum.avi','Uncompressed AVI');
open(videofile);
WriteVideo(videofile,M);
close(videofile)
(2)error facing while coding
(1) M(ct)=get frame(gcf) . In that command I use lower case letter for 'm',that shows error and forget to use end in looping
(3)OVERCOME FROM ERRORS
(1)By using uppercase letters 'M' and using end option in looping I came out from errors
(4)errors displayed in command window
Error in pendulam (line 52)
M(ct)=getframe(gcf);
M(ct)=getframe(gcf);

