Week 3 - Solving second order ODEs
AIM: Write a program in Matlab that will simulate the pendulum motion.
- Calculate the angular displacement and angular velocity.
- Animation of the simple pendulum oscillation.
ODE for simple pendulum,
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.
SIMPLE PENDULUM DIAGRAM: -
ODE DERIVATION: -
use,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec
For,
Angular displacement=0
Angular velocity=3 rad/sec at time t=0.
MATLAB PROGRAM
close all
clear all
clc
%Inputs data
L=1 ;
m=1 ;
b=0.05;
g=9.81;
%Initial conditions at t=0 sec (initial disp = 0 and initial vel = 3)
theta_0=[0;3];
%time interval is of20 sec (time point)
t_span=linspace(0,20,250);
%solve ODE
[t,result]=ode45(@(t,theta) ode_function(b,g,L,m,theta,t), t_span, theta_0);
figure(1);
plot(t,result(:,1),'color','r')
hold on
plot(t,result(:,2),'color','b')
xlabel('time')
ylabel('displacement and velocity')
legend('displacement', 'velocity')
grid on
Explanation of matlab programing: -
- FIRST ASSIGNE THE INPUT PARAMETER AND DEFINE THE INITIAL CONDITION.
- ASSIGNE THE TIME MATRIX FROM 0 TO 20 SEC WITH 250 VALUES.
- THE TIME AND RESULTS (ANGULAR POSITION AND VELOCITY) ARE CALCULATED USING THE ode45 FUNCTION WHICH TAKES INPUT PARAMETERS SUCH AS THE OUTPUT OF ANOTHER FUNCTION WHICH CALCULATES THE DIFFERENTIATION OF VARIABLE ‘θ’R.T TIME AND TIME MATRIX AND INITIAL CONDITIONS.
- PLOT THE GRAPH TIME VS DISPLACEMENT ANDVELOCITY.
- USED LEGEND COMMAND TO DIFFERENTIATE DISPLACEMENT AND VELOCITY.
Plot: -
Plotting a graph when time is change then displacement and velocity also very.
PROGRAM FOR ANIMATION OF THE SIMPLE PENDULUM OSCILLATION
close all
clear all
clc
%Inputs
L=1 ;
m=1 ;
b=0.05;
g=9.81;
%Inputs data
L=1 ;
m=1 ;
b=0.05;
g=9.81;
%Initial conditions at t=0 sec (initial disp = 0 and initial vel = 3)
theta_0=[0;3];
%time interval is of20 sec (time point)
t_span=linspace(0,20,250);
%solve ODE
[t,result]=ode45(@(t,theta) ode_function(b,g,L,m,theta,t), t_span, theta_0);
figure(1);
plot(t,result(:,1),'color','r')
hold on
plot(t,result(:,2),'color','b')
xlabel('time')
ylabel('displacement and velocity')
legend('displacement', 'velocity')
grid on
figure(2)
hold on
x0=0
y0=0
ct=1
for i=1:length(t)
x1=L*sin(result(i,1))
y1=-L*cos(result(i,1))
plot([x0,x1],[y0,y1],'color','r','linewidth',2)
hold on
plot([-1.5,1.5],[0,0],'linewidth',4)
hold on
plot(x1,y1,'o','markersize',20,'markerfacecolor','y')
axis([-1.5 1.5 -1.5 1.5])
grid on
hold off
pause(0.06)
M(ct)=getframe(gcf)
ct = ct+1;
end
movie(M)
videofile=VideoWriter('pendulum.avi','uncompressed avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Explanation of matlab programing: -
- After plotting figure one we start the programming of figure two
- Then figure 2 is the animation
- First x0 and y0 is defined
- To counter is initiated
- For loop is constructed by using the length of the time array
- Then x1 and y1 is defined using geometric relation and uses the output displacement in column 1 and uses the counter ‘i’ for row counter
- Pendulum string is plotted using array brackets
- Wall is plotted using array brackets
- Ball of the pendulum is plotted using a marker 0
- Define the axis
- then hold off was used to remove the plot in order to have only one plot on the screen after hold on was used to draw all the elements of the plot
- Then it is slowed down to 0.06
- Frame is captured
- Counter is increased by 1
- Make a movie
- Name is 'pendulum.avi'
- FUNCTION PROGRAM
- function dtheta_dt=ode_function(b,g,L,m,theta,t)
- 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
Explanation of function matlab programing: -
- Function output define, which is theta ‘,’ array.
- Theta 1 and theta 2 define 1st and 2nd element of array
- Write equation and output write in column matrix
Workspace data:
Programming Error:
- Used cosine to find x1 and sine to fin y1 which lead motion of the pendulum wrong. Check and find the error.
ANIMATION OF THE ODE
https://youtu.be/SNgRaaNmVjE