Week 3 - Solving second order ODEs
Aim:Study of Simple Pendulum using MATLAB
Theory:
Differntial equations are used to represent various phenomena in engineering.Most of them are indeed non -linear differential equation.
Simple Pendulum is linear system which is represented by
b:-Damping Coefficient
m:-Mass of bob
g:-Gravitional Acceleration
L:-Length of string
`theta`:-Angle made by string of pendulum with the vertical
Simulation representating the motion of the pendulum
Corresponding Code
clc %clears the command window
clear all %Clears the Workspace
close all %Closes all opened figures
%Inputs
b=0.05; %Damping Coefficient
g=9.81; %Gravity
l=1; %Length of string
m=0.1; %Mass of Bob
%Initial conditions
theta_0=[0;3]; % Displcement =0 and velocity =3 rads/s
%Time interval of solition
t_span=linspace(0,20,500); %From 0 to 20 s and 1000 intervals
%Solving the differential equation
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%Initial cordinate of the fixed point
x0=2;
y0=2;
%Counter
ct=1;
%Looping for each value of theta
for i=1:length(results)
disp=results(i,1); %Storing theta1 in variable theta1
x1=x0-l*cos(disp); % Corordinates of the bob for each valu of theta
y1=y0-l*sin(disp);
%Plotting the fucntion
plot([y0 y1],[x0 x1],'LineWidth',3); %Plotting Line
hold on
%Displaying corresponding tspan
delete(findall(gcf,'type','annotation')); %Clear the annotation
st=['Time ' num2str(t_span(i)) ' sec' ]; %Diaplay time in a box
annotation('textbox',[0.7 0.75 0.1 0.1],'String',st ); %code for box
yline(x0,'-.k');
xline(y0,'-.k');
plot(y1,x1,'o','MarkerFaceColor','r','MarkerSize',20) %Plotting Bob
hold off
axis([1 3 0.4 2.4]) %Setting Axis
set(gca,'XAxisLocation','top')
xlabel('Xaxis');
ylabel('Yaxis');
% Getting the current figure
M(ct)=getframe(gcf);
%incrementing the counter
ct=ct+1;
end
%Makes a Movie
movie(M);
%Creating a video object
videof=VideoWriter('simple_pendulum.avi','Uncompressed AVI');
%Opening the created object
open(videof);
%Writting the created movie inside the video object
writeVideo(videof,M);
%Closing the video
close(videof)
%Function to solve the ode
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
The code starts with clearing all variables in workspace as well the command window.Then close all command closes all current figures.Then input for the differential equation is defined.Ode 45 is used to solve the equation.
About ODE45
Ode 45 is a numerical method for solving th first order differential equations also known as R-K Method.
Syntax of ode 45
[t_sol Y_sol] = ode45(@(t,theta)odefunction,t_span,initial_condition)
t_sol:- Vector containing values of independent variable
Y_sol:-Vector containing values of dependent variable
The ode function is used to define system of differential equation as a function.
eg:-
Plotting Representating the Displacement and Velocity of Simple Pendulum.
Video for better intituation
A small addition of code could plot the above simulation
*************
%Counter
ct=1;
subplot(2,1,2)
hold off
delete(findall(gcf,'type','annotation'))
plot(t_span,results(:,1));
hold on
plot(t_span,results(:,2));
xlabel('Time(Sec)');
ylabel('Output')
hold off
%Looping for each value of theta
for i=1:length(results)
***********
********
*******
x1=x0-l*cos(disp); % Corordinates of the bob for each valu of theta
y1=y0-l*sin(disp);
%Plotting the fucntion
subplot(2,1,1)
plot([y0 y1],[x0 x1],'LineWidth',3); %Plotting Line
hold on
*******
******
******
ylabel('Yaxis');
subplot(2,1,2)
delete(z)
z= xline(t_span(i))
legend('Displacement','Velocity','Current Position');
% Getting the current figure
M(ct)=getframe(gcf);
****
***
Conclusion
The Variation of displacement and velocity of a Simple pendulum is thus studied using MATLAB.