Week 3 - Solving second order ODEs
AIM : To simulate the transient behaviour of a simple pendulum and to create an animation of it's motion on MATLAB.
OBJECTIVE :
- To write a program that solves the second order ODE corresponding to motion of a simple pendulum and to plot its angular displacement and angular velocity wrt time.
- To create the animation of the motion of pendulum.
THEORY : A simple pendulum is defined to have an object that has a small mass, also known as the pendulum bob, which is suspended from a light wire or string, such as shown in Figure 1.
Figure 1
When the bob of pendulum is displaced from it's mean position to either exterme position and is released, the pendulum will swing back and forth with periodic motion until the motion damps. By applying Newton's second law for rotational systems, the equation of motion for the pendulum may be obtained as
`tau = I*alpha` `implies` `-mg*sintheta*L` = `mL^2*(d^2theta)/(dt^2)`
and rearranged as `(d^2theta)/(dt^2) + (g/L)*sintheta =0`
and if a damper is introduced in the system the above equations changes slightly as
`(d^2theta)/dt+(b/m)*(d(theta))/dt+(g/L)*sintheta =0`
GOVERNING EQUATIONS :
To solve the above equation in MATLAB, we need to transform the given second order ODE into first order ODE as,
`implies` `theta = theta_1` ,
`implies``(d(theta))/dt = (d(theta_1))/dt = theta_2` ,
`implies``(d^2theta)/dt = (d^2theta_1)/dt = (d(theta_2))/dt` ,
`implies (d^2theta)/(dt) +(b/m)*(d(theta))/dt +(g/L)*sintheta = 0` ,
`implies (d^2theta_1)/(dt) +(b/m)*(d(theta_1))/dt+(g/L)*sintheta_1 =0` ,
`implies (d(theta_2))/dt+(b/m)*theta_2+(g/L)*sintheta_1` ,
`therefore, (d(theta_1))/dt = theta_2` and `(d(theta_2))/dt =-(b/m)*theta_1 -(g/L)*sintheta_1`
Represent them in matrix form which is used in MATLAB,
`implies d/dx[[theta_1],[theta_2]] = [[theta_2],[-(b/m)*theta_2-(g/L)*sintheta_1]]` ,
BODY OF THE CONTENT :
- Initial inputs such as b,g,l,m are provided according to data in the question.
- An array of initial theta was created to define initial angular displacement and initial angular velocity.
- An array of time span was created to store 500 equally spaced intervals between time 0 to 20 seconds.
- To solve the second order ODE, a function named ode_function which stores the above matrix data in it.
- The given second order ODE was solved using ode45 command followed by proper syntax of the calling function ode_function and the result obtained was used to plot the graph of variation of angular displacement and angular velocity wrt time.
- Now for creating the animation of obtained harmonic motion of pendulum, for loop was used and a loop counter variables was initiated. In this execution the one end of the string of pendulum was fixed and the other end moves according to the solution of the ODE solved and bob was made by using marker command and was plotted for each time interval of the time span.
- All the frame were compiled using getframe command and movie command was used to create the animation.
MATLAB CODE FOR SOLVING AND SIMULATION :
% A program for solving second order ODEs and simulate its motion
clear all
close all
clc
% Inputs
b = 0.05; % damping coefficient
g = 9.81; % acceleration due to gravity(m/s^2)
l = 1; % length of string (m)
m = 1; % mass of bob (kg)
% Initial conditions
theta_0 = [0 ; 3];
% time points
t_span = linspace(0,20,500);
% Solve ODE
[t , results] = ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0);
% Plotting the graph
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
title('Damped Harmonic motion of a Simple Pendulum')
legend('Angular displacement','Angular velocity')
xlabel('Time (sec)')
ylabel('Plot')
grid on
hold off
% Iteration using for loop to create animation of motion of pendulum
loop_counter =1;
for i = 1:length(results(:,1))
x_start = 0; % initial coordinates
y_start = 0;
x_end = l*sin(results(i,1)); % final coordinates
y_end = -l*cos(results(i,1));
% Plotting for animation
figure(2)
plot([-1 1],[0 0],'linewidth',2,'color','black') % base of pendulum
hold on
line([x_start x_end], [y_start y_end], 'linewidth',2,'color','blue') % string of pendulum
hold on
plot(x_end,y_end,'-O','markers',15,'markerface','red') % bob of pendulum
grid on
title('Animation of transient behaviour of a Simple Pendulum')
axis ([-2 2 -2 2])
pause(0.003)
hold off
M(loop_counter) = getframe(gcf);
loop_counter = loop_counter+1;
end
% Creating animation by compiling different frames
movie(M)
videofile = VideoWriter('Pendulum_damped_animation.avi','uncompressed avi');
open (videofile)
writeVideo(videofile,M)
close(videofile)
MATLAB CODE FOR FUNCTION USED :
% A function program which will be called in main program for solving ODEs
function [dtheta_dt] = ode_function(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
ERRORS AND REMEDIES :
- On plotting the animation of pendulum, the shape was inverted, i.e., the bob and string were above the fixed line.
- It was due to missing -ve sign in the following line of code and was rectified by putting the required sign at required place.
RESULTS :
- The following plot was generated -
The above plot of angular displacement and angular velocity shows the pendulum's motion to be gradually decreasing. This is due to damping effect which exponentially decreases the amplitude and the velocity of motion.
- The link of the animation of simulation-
https://www.youtube.com/watch?v=dwP2pQRijfE
CONCLUSION : In this way we can understand that any second order or any higher order ODE can easily be solved using MATLAB, and simulation of transient behaviour of simple pendulum was made using it to explain the effect of damping on it's motion.
REFERENCES :
- `b` = damping coefficient (`(N*s)/m`),
- `g` = acceleration due to gravity (`m/s^2`),
- `l` = length of string (`m`),
- `m`= mass of bob (`kg`),
- `theta` = angular displacement (position),
- `tau` = torque (`N*m`),
- `I` = moment of inertia (`(kg)/m^2`),
- `alpha` = angualr acceleration (`(rad)/s^2`)