ODE and pendulum animation
AIM: To write a program in Matlab that will simulate the pendulum motion.
ASSUMPTIONS:
1. The value of acceleration due to gravity is (g) 9.81 m/s^2
2. The length of the pendulum is 1 meter.
3. The damping coefficient is 0.05.
4. Tha mass of the pendulum bob is 1 kg.
THEORY:
The motion of the pendulum can be easilty represented by the following differential equation:
`(d^2 θ)/dt^2 +b/m dθ/dt + g/L sinθ = 0`
where:
`theta`= angular displacement of the bob
b = damping coefficient
g = accelration due to gravity
m = mass of the pendulum bob
L = length of the pendulum string
t = time period
`(dθ)/dt`=angular velocity
d2θdt2">`(d^2θ)/(dt^2)` = angular acceleration
MATLAB CODE
1. MAIN PROGRAM:
clc
clear all
close all
%Input Parameters
b = 0.05;
g = 9.81;
l = 1;
m = 1;
%Initial Conditions
alpha0 = [0;3];
time_span = linspace(0,20,500);
%Function Call Statement
[t,results] = ode45(@(t,alpha) pendulum_func(t,alpha,b,g,l,m),time_span, alpha0);
%Plot
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Plot')
grid on
figure(2)
%counter for frame
ct = 1;
for i = 1:length(time_span)
x0 = 0;
y0 = 0;
x1 = l*sin(results(i,1));
y1 = -l*cos(results(i,1));
plot([-0.6 0.6],[0 0],'linewidth',12)
hold on
plot([x0 x1],[y0 y1],'color','b','linewidth',3)
axis([-3 1 -3 1])
hold on
plot(x1,y1,'o','MarkerSize',35,'MarkerFaceColor','g')
hold off
axis([-1 1 -1.5 1])
pause(0.001)
M(ct) = getframe(gcf);
ct = ct+1;
grid on
end
%Animation
movie(M)
videofile = VideoWriter('ode_2nd_order.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Explanation:
1. Take inputs such as b,g,l,m
2. Assume proper initial states for the angular displacement and time.
3. Apply the in built sover function in MATLAB, i.e ode45, which basically acts as a solver of the ODE. It calls the function pendulum_func and accesses the values. It basically integrates the values of angular velocity at various time intervals and stores the results in matrix [t,results]
4.Plot the data obtained with a graph of Plot versus Time, by plotting the time and the results.
5. For the animation, we use the for loop by separating the frames and stitching them together. This can be achieved by setting initial value of counter frame equal to 1.
6. For aesthetic purposes, the color and the size of the bob can be adjusted.
7. Finally use the writeVideo command to stitch all the frames together.
8. Save the file and close it.
2.FUCTION PROGRAM
function [dalpha_dt] = pendulum_func(t,alpha,b,g,l,m)
alpha1 = alpha(1);
alpha2 = alpha(2);
dalpha1_dt = alpha2;
dalpha2_dt = -(b/m)*alpha2-(g/l)*sin(alpha1);
dalpha_dt = [dalpha1_dt; dalpha2_dt];
end
Explanation:
1. dalpha_dt is a function of the parameters (t,alpha,b,g,l,m)
aplha(1) is the value assigned to alpha1
alpha(2) is the value assigned to alpha2
2. dalpha1_dt is equal to alpha2 and dalpha2_dt is equal to -(b/m)*alpha2-(g/l)*sin(alpha1)
3. dalpha_dt is 2 row and 1 column matrix
CONCLUSION:
1. MATLAB can be effectively used to solve 2nd order ODE's
2. The graph of plot versus time can be easily obtained.
3. For the animation of the pendulum the YouTube link of the same is given below: