Week 3 - Solving second order ODEs
Aim: - To solve ODE of simple pendulum using MATLAB
Introduction: - In this, I had solved ordinary differential equation of simple pendulum. And plot graph of angular displacement and angular velocity with respect to time variation. And also perform animation of damped vibration of pendulum.
Theorey: -
Simple pendulum is nothing but the mechanical system that moves in oscillatory motion. i.e.Ideal simple pendulum consist of a point mass suspended by a weightless inextensible perfectly flexible thread and free to vibrate without friction. Dampers are used to minimize the vibration.In the damped simple harmonic motion, the energy of the oscillator dissipates continuously.
Governing Equations: -
`(d^2 θ)/dt^2 + b/m (dθ)/dt + g/l sinθ=0
which is equation of motion of a simple pendulum with damping
i.e. where b=damping factor
m = point mass
l= length of string
Code: -
clear all;
close all;
clc;
b=0.05; %damping coefficient
g=9.81; %Accleration due to gravity
l=1; %length of string
m=1; %mass of the bob
%initial conditions
% for displacement and velocity
theta_0=[0;3];
%time points
t_span=linspace(0,20,500);
%solve ODE
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0); %ode_func is a %fuction defined containing seprate 2 ode
%For plotting graphs of displacement and velcoity w.r.t. time
figure(1)
plot(t,results(:,1));
hold on;
plot(t,results(:,2));
xlabel('plot')
ylabel('time')
legend('displacement','velocity');
% programming for modelling of pendulum
ct=1;
for i=1:length(results(:,1))
figure(2) % for plotting in seprate window figure
x0=0; %fixed point
y0=0; %fixed point
x1=l*sin(results(i,1));
y1=l*cos(results(i,1));
plot([-0.5 0.5],[0 0],'linewidth',4,'color','k'); %fixed bar
hold on;
line([x0 x1],[y0,-y1],'linewidth',2,'color','b'); % string
hold on;
%plotting sphere bob
plot(x1,-y1,'-o','markers',20,'markerfacecolor','g');
axis([-1.25 1.25 -2 0.75]);
hold off;
pause(0.0003);
M(ct)=getframe(gcf);
ct=ct+1; %counter for looping multiple frames
end
%to convert into movie (animation)
movie(M)
videofile=VideoWriter('pendulum_ode_program_animation.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Code for Ode_func: -
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]; %integration of 2 simplified ODE
end
Graphical representation:-
Conclusion:- From this study , I had infer that by using suitable damper we can minimize the energy of oscillation of the simple pendulum. And we can study ODE of any system more easily by using Matlab than traditional way.
Animation Link of Youtube: - https://www.youtube.com/watch?v=RQ93lgVF7Hc