Week 3 - Solving second order ODEs
Problem statement:
To write a Matlab program that solves the provided ODE of a motion of a simple pendulum with damping and animated the motion with the calculated values.

Given Parameters:
Damping Coefficient,b=0.05
Gravity,g=9.81 m/s^2
Length,L= 1 m
Mass,m= 1 kg
Solving ODE equation:
The second-order differential equation was solved by changing it to two first-order differential equations and a solution vector with two-column is determined.

Matlab code:
clear all
close all
clc
%Inputs
l=1;
m=1;
b=0.05;
g=9.81;
%Initial Condition
theta_0=[0;3];
%Time conditions
t_span=linspace(0,20,500);
%Solving ODE
[t,results]= ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%Plotting
%figure1
plot(t,results(:,1))
hold on
plot (t,results(:,2))
title('Motion of a simple pedulum with Damping')
legend('Displacement','Velocity')
ylabel('Plot')
xlabel('Time(s)')
figure(2)
ct=1;
for i = 1:length(results(:,1));
x0=0;
y0=0;
x1=1*sin(results(i,1));
y1=-l*cos(results(i,1));
%Plotting
plot([-1,1],[0,0],'linewidth',10,'color','k')
hold on
plot([x0 x1],[y0 y1],'linewidth',4,'color','r')
hold on
plot(x1,y1,'o','markersize',40,'markerfacecolor','y')
title('Animated Motion of Pendulum')
axis([-1 1 -1.5 0.5])
pause(0.03)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
%Movie(m)
movie(M)
videofile= VideoWriter('animated pendulum.avi','uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Code Explanation:
- The initial conditions values,theta_0 = [0;3] are provided, where 0 is the displacement and 3 is the velocity.
- The ODE has to be integrated to obtain a required solution, hence the time span 't_span' command is included from 0 to 20 seconds.
- Then, ODE45 syntax is introduced which defines ODE as the function of t and theta and corresponding values and initial conditions and time span.
- Ode_func mentioned in the syntax is the call function that solves the ODE in Matlab and defines it in the actual program.
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 - After calculating the values, a plot has been plotted between displacement and velocity for the defined theta.
Plot:

- Then, to animate the motion of the pendulum, a 'for loop' command has been used with the index variable 'i' for the range of values of time 't'.
- The structure of the pendulum with damping has been plotted with the plot commands.
- Then, getframe command and set of movie(M) have been used and the motion of the pendulum is animated.
Animation:
(Youtube link has been provided)
Errors faced:
- Didn't encounter any errors until the programming and until the animation. Only struggled to set the axis as the structure of the pendulum was not in the center frame with my initial values. Then, tried with multiples axis values and finally fixed a frame to pendulum base.
Conclusion:
The Second-order equation of motion of the simple pendulum was solved and animation has been created which defines the position of the pendulum with respect to time.