Challenge

Solving second order ODE  for Pendulum Motion

Solving second order ODE for Pendulum Motion

AIM:

A program to simulate the transient behaviour of a simple pendulum and to create an animation of its motion.

OBJECTIVES:

  • Write a program to solve the ODE
  • Plot the displacement v/s time & velocity v/s time graphs.
  • Create an animation of the motion of the pendulum

THEORY:

In mathematics, an ordinary differential equation(ODE) is a differential equation containing one or more functions of one independent variable and the derivatives of those functions. The term ordinary is used in contrast with the partial differential equation which may be with respect to more than one independent variable.

Here, we are using the ODE that represents the motion of a simple pendulum with damping. The general equations for this is:

We are going to use this 2nd order ODE equation to find out the velocity and position of the pendulum ball with respect to time.

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.

Solving Methodology:

Code:

ODE Function program

function [dtheta_dt] = ode_function(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

Program to simulate pendulum motion and creating animation

%Program to solve the ODE and simulate pendulum motion
clear all
close all
clc

%Inputs
b = 0.05;   %damping coefficient value
g = 9.81;   %gravitational value on m/s^2
l = 1;      %length of the pendulum in m
m = 1;      %mass of the body in Kg

theta_θ = [0;3];    %initial condition for displacement and velocity
t_span = linspace(0,20,400);   %Time span points

%Solving ODE
[t,results] = ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_θ);

%plotting
figure(1)
plot(t,results(:,1))
hold on
grid on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
legend('Displacement','Velocity')

figure(2)
ct = 1;
for i = 1:length(results(:,1))
    THETA_1 = results(i,1);
  
    %Initial coordinates
    x0 = 0;
    y0 = 0;

    x1 = 1*sin(THETA_1);
    y1 = -1*cos(THETA_1);

    %Plot
    plot([-0.5 0.5],[0,0],'color','g')
    line([x0 x1],[y0 y1],'color','r')
    hold on
    plot(x1,y1,'marker','0','markersize',25,'markerfacecoor','b')
    grid on
    axis([-1.5 1.5 -1.5 1])
    hold off
    pause(0.0001)
    M(ct) = getframe(gcf);
    ct = ct + 1;
end
movie(M)
videofile = VideoWriter('Second_ODE_Pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Results

The displacement v/s time and velocity v/s time graphs were plotted.

The animation video of the pendulum motion was created and uploaded in youtube.

Link for video: https://youtu.be/nuWEF_jv3vE

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.