Write a program to simulate the transient behavior of a simple pendulum and to create an animation of its motion
OBJECTIVE: Write a program to simulate the transient behavior of a simple pendulum and to create an animation of its motion.
What is ODE?
The number of the highest derivative in a differential equation. A differential equation of order 1 is called first order, order 2-second order, etc. Example: The differential equation y\" + xy\' – x3y = sin x is second-order since the highest derivative is y\" or the second derivative
ODE is important in engineering they provide lots of information and solving those ODE can solve many examples
An example is a Simple Pendulum
PROCEDURE :
In above fig, there is formula given which is in second-order we need to convert it into first order ode, first convert that into first order
after solving we get
dtheta1_dt=theta2
dtheta2_dt= -(b/m)*theta2 - (g/l)*sin(theta1)
where,
b= Damping coefficient
g= gravity
L = Length of pendulum
m= mass
if we provide the initial condition for displacement and initial velocity
initial condition [ 0;3] ....given
and need to define the time for how many time we need the ode to work
Time is given 0 to 20 second
FUNCTION CODE:
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
we have a function for this program so that we can change the value of damping coefficient, theta, gravity, etc to solve
MAIN CODE:
clear all
close all
clc
%input
l=1 ;
m=1 ;
b=0.05;
g=9.81;
%intial condition
theta_0 = [0;3];
%time points
t_span = [0 20];
% Solve ODE. the time interval set from 0 to 20 seconds
[t,results]=ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1),\'linewidth\',3);
hold on
plot(t,results(:,2),\'linewidth\',3);
xlabel(\'time\')
ylabel(\'Amplitude\');
grid on
legend({\'displacement\',\'velocity\'})
% % using for loop
% ct=1;
for i=1:length(results(:,1))
x0=0;
y0=0;
x1= l*sin(results(i,1));
y1=-l*cos(results(i,1));
figure(2)
plot([-0.5,0.5],[0,0],\'linewidth\',3,\'color\',\'r\');
line([x0 x1],[y0 y1],\'linewidth\',2,\'color\',\'g\');
hold on;
plot(x1,y1,\'o\',\'markers\',20,\'markerfacecolor\',\'b\');
grid on;
axis([-1.5 1.5 -1.5 0.5]);
pause(0.01);
hold off;
M(i) = getframe(gcf);
end
% creating movie
movie(M)
videofile = VideoWriter(\'pendulum_animation.avi\',\'uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
As per the code, we are using a syntax called ode45 to solve the ode problems
[t,y] = ode45(odefun,tspan,y0)....eg.
with a time span of 0 to 20 sec in this syntax there are two conditions used by ode45 ie. one dependent and another independent
t= time is independent and theta changes with respect to time so it is dependent
in ode45 (@ (t,theta) ) wt it did is it gives the output by considering dependent and independent values of theta with respect to time and create an array of results vs time
[t,results]=ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span,theta_0);
FIG1: plot of time vs amplitude to show the effects of damping
we get the results for displacement and velocity from initial to final
we get the values for displacement in the first column and velocity column
as we need to plot the ode for pendulum we need to plot first the plot for displacement and velocity with respect to time
using for loop consider the first column as it is for the displacement result
the initial and final condition of displacement of the pendulum and fix the axis for creating a pendulum we need to use marker command and give the size of the marker and set the speed
end the for loop
and create a movie
and save the file name to pendulum_animation.avi
RESULTS: ANIMATION OF SIMPLE PENDULUM