Week 3 - Solving second order ODEs
CREATING A SIMPLE PENDULUM USING MATLAB
AIM : To create an animation of a simple pendulum using matlab as a solution for second order ODE.
OBJECTIVE :
- To write the program for the second order ODE which represents the equation of motion of a simple pendulum with damping.
- To create animation for the same
GIVEN DETAILS:
Simulate the motion between 0-20 sec
for angular displacement=0
angular velocity=3 rad/sec
at time t=0.
In the above equation,
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
INPUT DIMENSIONS :
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2
INTRODUCTION :
Simple Pendulum :
A pendulum is a weight suspended from a pivot so that it can swing freely.When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position. When released, the restoring force acting on the pendulum's mass causes it to oscillate about the equilibrium position, swinging back and forth. The time for one complete cycle, a left swing and a right swing, is called the period. The period depends on the length of the pendulum and also to a slight degree on the amplitude, the width of the pendulum's swing.
In mathematics, an ordinary differential equation (ODE) is a differential equation containing one or more functions of one independent variable and the derivatives of those functions.
The term ordinary is used in contrast with the term partial differential equation which may be with respect to more than one independent variable.
MATLAB PROGRAM FOR THE SAME :
Program for defining the function
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
Program for calling the function :
clear all
close all
clc
b = .05; %damping coefficient
g = 9.81; %accleration due to gravity
l = 1; %length of the string
m = 1; % mass of the pendulum
% 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);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Velocity')
legend('displacement','Angular Velocity')
% Fixed Coordinates
ct = 1;
theta_1 = results(:,1);
for i = 1:length(results(:,1))
x1 = 0;
y1 = 0;
theta_2 = theta_1(i);
%Coordinates
x2 = l*sin(theta_2);
y2 = -l*cos(theta_2);
x3 = -1;
x4 = 1;
y3 = 0;
y4 = 0;
figure(2)
%Base of the pendulum
plot([x3 x4],[y3 y4],'linewidth',4,'color','r')
hold on
%string of the pendulum
plot([x1 x2],[y1 y2],'g','linewidth',6,'color','b')
hold on
%Plotting the bob of the pendulum
plot(x2,y2,'marker','o','markersize',28,'markerfacecolor','y')
%For axis
axis([-2 2 -2 0])
pause(.3);
hold off
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M);
Videofile = VideoWriter('Pendulum.avi','Uncompressed AVI');
open(Videofile);
writeVideo(Videofile,M);
close(Videofile);
EXPLAINATION OF THE CODE :
First we provided various input parameters such as length,damping coefficient ,mass ,gravitational constant,etc .
ode is an solver used in matlab to solve ODE's.
Plot command is used to plot the graph for velocity and angular displacement .Legend is used to identify different plots.
Axis command is used to fix the axis . The pendulum has fix coordinates as mentioned in the program.Pause command pauses the code which is not very efficent.
Marker command is used for making the bob of the pendulum .
Now for making the movie based on the animation we wrote the following in the form of code :
- Getting individual frames and saving it in an array called M. ct represents each and every frame.
- Movie command is going to create to generate a movie with all these frames.
- Videowriter command creates AVI files where we can dump all the frames.
Now run the plot ,and we can see the required animation.
RESULTS AND PLOTS :
PLOT OF THE PENDULUM PLOT BETWEEN VELOCITY AND ANGULAR DISPLACEMENT
WORKSPACE
CONCLUSION :
Using matlab we have successfully the animation for the simple pendulum corresponding to the second order ODE equation .
PLEASE NOTE THAT THE ANIMATED VIDEO IS ATTACHED BELOW
https://drive.google.com/file/d/1IoaQ1pgMJIBm0GmP5DMfrEFRo7XAuSey/view?usp=drivesdk