Simulation Of a Simple Pendulum using MATLAB's ODE45
The objective of this challenge is to solve a second-order ODE which brings out the behaviour of a simple pendulum's movement. The second-order ODE is simplified to two first-order ODE to perform the motion.
The given equation is as follows:
DATA REQUIRED FOR SOLVING:
The data required for solving the ODE is
Length of the Pendulum, L = 1m
mass of the Pendulum, m = 1kg
Damping Coefficient, b = 0.05
Acceleration due to gravity, g = 9.81m/s.
BREAKING THE EQUATION:
θ=θ1θ=θ1
dθdt=dθ1dt=θ2dθdt=dθ1dt=θ2
dθ1dt=θ2dθ1dt=θ2
d2θ1dt2=dθ2dtd2θ1dt2=dθ2dtt = theta 2`
dθ2dt=−bmθ2−gLsinθ1dθ2dt=-bmθ2-gLsinθ1
Here, θ1=θ2θ1=θ2
In the code, theta gives us two rows and 100 columns. These are the values of Displacement and Velocity respectively.
CREATING THE MOVIE:
- In the video, you could see that in axis (0,0) is fixed from which the pendulum simulates.
- Then the values of x1 and y1 are stored in the same plot for each value of displacement
- Hence by capturing all the values for displacement, we get a movie.
MAIN CODE FOR SIMULATING SIMPLE PENDULUM:
clear all
close all
clc
L = 1;
m = 1;
b = 0.05;
g = 9.81;
%TIME LIMIT
t_span = linspace(1,20,100);
%INITIAL CONDITIONS
theta_1 = [0 3];
[t, theta] = ode45(@(t, theta) ode_pend(t, theta, b,g,L,m), t_span, theta_1);
figure(1)
plot(t, theta(:,1), 'color' , 'r');
hold on
plot(t,theta(:,2), 'color', 'k');
hold on
grid on
xlabel('Time');
ylabel ('angular velocity and Displacement');
legend ('angular Displacement','Angular Velocity');
%STICHING IMAGES FOR MOVIE GENERATION
ct = 1;
for i = 1:length(theta(:,1))
x0 = 0;
y0 = 0;
x1 = L*sin(theta(i,1));
y1 = -L*cos(theta(i,1));
figure(2)
plot([x0 x1], [y0 y1] , 'linewidth', 2, 'color' ,'g')
hold on
plot(x1,y1)
hold off
grid on
pause(0.01)
axis ([-2 2 -2 0])
M(ct) = getframe(gcf);
ct = ct+1;
end
%CREATING A MOVIE
movie(M)
videofile = VideoWriter('forward_kinematics.avi' , 'uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
FUNCTION CODE:
function [dthetadt] = ode_pend(t, theta, b,g,L,m)
theta1 = theta(1)
theta2 = theta(2)
dtheta1dt = theta2;
dtheta2dt = -(b/m)*theta2 - (g/L)*sin(theta1);
dthetadt = [dtheta1dt;dtheta2dt];
end
RESULTS:
Graph comparing Angular velocity and Angular acceleration:
CONCLUSIONS:
From the graph of Angular velocity and displacement, we could see the performance of the pendulum with respect to time from 0-20s.
The angular velocity and displacement decrease with respect to time.
The images of each value of x1 and y1 are stitched and the movie is created which gives a harmonic motion.
VIDEO OUTPUT: