Project

Motion Of The Pendulum

Theory

A Pendulum is a ball suspended from a piviot point so that it can swing freely. When the pendulum is displaced from its resting position the ball or bob goes to and fro motion and comes to rest. The time for one complete oscillation is called Period. The period depends on the length of the pendulum and angle with which the bob is displaced from the y-axis. The motion of the pendulum can be expressed mathematically by the second order ordinary differential equation.

Description

In this matlab code the second order ODE of the pendulum is solved and the motion of the pendulum is animated. The Inputs of the problem are damping coefficient b = 0.05, acceleration due to gravity g = 9.81 m/s^2, mass of the ball m = 1 kg, length of the pendulum l = 1 m. The Initial angular displacement = 0 radians, angular velocity = 3 rad/s^2 and the time period = 0- 20 seconds. In this code ODE 45 is used. The second order ode depends on the time period and angle. The second order ode is splitted into two first order ode. The result of the ode contains two columns. For creating the animations the for loop is used. The condition for the for loop is length of the time period. For each iteration of the for loop the position of the bob is calculated based on the trigonometric functions. Then using viscircles command the circle for the origin and the ball is created. Based on the origin and the position the line is created. To see the animation more clearly the previous instance of the object should be deleted. For deleting the previous instance if statement is used. The condition for the if statement is the iterative should be less than the length of the time period. Then using videowriter the animation of the video is captured.

%Solving 2nd order ODE and Animation of a Simple Pendulum 
clear all
close all
clc

%Inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;

%Initial Condition
theta_0 = [0;3];

%Initial Time Period
t_span = linspace(0,20,600);

%ODE 
[t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span,theta_0);

%Plot
figure(1)
plot(t,results(:,1))
hold on 
plot(t,results(:,2))

%Animation
figure(2)
O = [0 0];          
axis([-1.5 1.2 -1.5 0.5]);      
grid on

ct=1;                          
for i=1:length(t_span)
    
    %Position
    P = l*[-sin(results(i)) -cos(results(i))];
    
    %Circle of origin
    Orgin_circle = viscircles(O,0.01);
    
    %Pendulum Line
    pendulum = line([O(1) P(1)],[O(2) P(2)]);
    
    %Ball
    ball = viscircles(P,0.10);
    
    %Time intervals 
    pause(0.01);
    M(ct) = getframe(gcf);
    ct = ct+1;
    
    %Deletes previous object 
    if i<length(t_span)
        delete(pendulum);
        delete(ball);
        delete(Orgin_circle);
    end
end

% Video of the animation
movie(M) 
videofile = VideoWriter(\'Pendulum.AVI\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Function of ODE

In order to solve second order ode the equation should be splitted into two first order ode. The function of the ODE depends on the damping coefficient, acceleration due to gravity, length of the pendulum, mass of the bob and theta. The splitted equation should to assigned to a variable called dtheta_dt.

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

Result

https://www.youtube.com/watch?v=E19aPUvAPyo

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.