Week 3 - Solving second order ODEs
Aim: To Calculate SHM of a pendulum and plotting the Graph of displacement and velocity against time.
Theory: Simple Pendulum consists of mass m, length of string L, damping coefficient b and gravity g always acting on it downwards. Simple pendulum is mechanical device used to observe the oscillatery motion from a fixed support end.
The Italian scientist Galileo first noted the constancy of a pendulum's period by comparing the movement of a swinging lamp in a Pisa cathedral with his pulse rate. The Dutch mathematician and scientist Christiaan Huygens invented a clock controlled by the motion of a pendulum in 1656.
In Engineering we use ODE to derive the simple harmonic equation to describe the behaviour of the system. The movement of the pendulum depends on the Newton's second law. And its became easy when solving the function with second order differential equation in matlab.
Errors:
- Major Error faced during the motion running the animation.
- Plotting wrong graph [x0 y0], [x1 y1]
- Axis of rectangle [-0.2 0.4 0.2] forget the fourth coordiante.
- Minimum mistake on semicolon and plotting.
- Don't know the command for creating the circle googled for the command of 'viscircle'.
Process:
YouTube Link: https://www.youtube.com/watch?v=TfMO_fmz1k8
given:
L = length of the string
m = mass
b = damping Coefficient
g = gravity
time_span = 0 to 20sec
Problem:
%%Function
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
%%SHM of Pendulum
% Solving ODE problem of SHM
clear all
close all
clc
%inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;
%initial state
theta_0 = [0;5];
%time state
t_span = linspace(0,20,500);
%solving ODE
[t, result] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span, theta_0)
x0 = 0;
y0 = 0;
theta_1 = result(:,1);
ct = 1
for i=1:length(t_span)
r = theta_1(i)
x1 = 1*sin(r);
y1 = -1*cos(r);
plot([x0 x1], [y0 y1], 'linewidth', 1, 'color', 'bla');
viscircles([x1 y1], 0.05)
rectangle('Position',[-0.2 0 0.4 0.2],'FaceColor','g')
title('Simple Pendulam')
line([0,0],[-5 0],'color','b','linestyle','--')
axis([-1 1 -1.4 0.2])
M(ct) = getframe(gcf)
ct = ct+1
end
movie(M)
videofile = VideoWriter('Pendulum_animation.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
%Plotting the displacement and velocity against Time graph
hold on
%figure 1
plot(t,result)
%figure 2
plot(t,result)
grid on
xlabel('Time')
ylabel('plot')
legend('displacement', 'velocity')