Week 3 - Solving second order ODEs
Aim: To solve an ordinary differential equation (ODE) and use the calculated information to animate the motion of the pendulum.
Code:
%program to simulate the transient behaviour of a simple pendulum
clear all
close all
clc
%inputs
b = 0.05; % damping coefficient
g = 9.81; % gravity (m/s^2)
l = 1; % length of the spring (m)% length of the spring (m)
m = 1; % mass of the bob (kg)
% initial condition: angular displacement is 0 and angular velocity is 5
theta_0 = [0;3];
% time points: Pendulum oscillates between 0-20 seconds
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);
%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')
ylabel('Plot')
xlabel('Time(seconds)')
% For loop
% Before starting the for loop, a counter value was defined
ct =1;
for i = 1:length(results(:,1))
x_start = 0
y_start = 0
x_end = l*sin(results(i,1))
y_end = -l*cos(results(i,1))
% plotting the animation
figure(2)
plot([-1 1], [0 0], 'linewidth', 4)
hold on
plot([x_start x_end],[y_start y_end],'linewidth',3)
hold on
plot(x_end,y_end,'o','markersize',20,'markerFacecolor','y')
axis([-1.5 1.5 -1.5 1])
pause(0.05)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
% Animation of the pendulum
movie(M)
Videofile=VideoWriter('Simple_Pendulum.avi','Uncompressed AVI');
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)
% Code for Function 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
Code Explanation:
- First of all define the inputs and variables. The damping coefficient, gravity, length of the spring and mass of the bob have all been defined.
- Next step is to define the initial conditions, the angular displacement and angular velocity have been defined. Timespan has also been defined.
- Next step is to solve the ODE, Ode45 was used, it is a function in Matlab that solves ordinary or partial differential equations using a numerical method. The numerical method in question here is the 4th order Runge Kutta method. The function ode45 takes 3 inputs. The 3 inputs are the function to be integrated, the time span and the initial conditions. [t, results] means that the results that have been calculated with respect to time.
- After calculating the results, a graph has to be plotted, the graph contained two plots. One for angular displacement with respect to time and one for angular velocity with respect for time. The legend for the graph and the axis' labels have been defined.
- The second part of the code is to establish a for loop, A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. a counter loop was added before the For loop. the start and endpoints for x and y-axis have been defined. The loop is used where all the values of timespan are assigned to a variable 'i' one at a time. The coordinates of [ xo x1 y0 y1] and their corresponding relations are entered to form a 'string' of the pendulum. Markersize command was used to form the bob of the pendulum.
- A pause command was used for pausing the command for a certain time, for this project a pause of 0.03 s was used. The M command was used to capture the current frame every time the for loop runs. The final step of the loop is to add a +1 to the ct variable; this will increment the frame every time the for loop run. End command was used for the plot and for the for loop to stop both from running.
- The final step of the code is to create a movie of the animation of the robotic arm, so the movie command was used. The VideoWriter command was used to assign the video format, for this project .avi was used as the format. The open command was used to open the video. writevideo command was used to write the video with plot frames. Finally, the close command was used to close the video file.
Results:


Errors:
No erorrs has been faced during this challenge.
Conclusion:
The simulation of the transient behavior of a simple pendulum is successfully created by using a program in MATLAB.
Animation Vedio: