Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Placements

Student Reviews


For Business


More

Academic Training

Informative Articles

Find Jobs

We are Hiring!


All Courses

Choose a category

Loading...

All Courses

All Courses

logo

Loading...
Executive Programs
Workshops
For Business

Success Stories

Placements

Student Reviews

More

Projects

Blogs

Academic Training

Find Jobs

Informative Articles

We're Hiring!

phone+91 9342691281Log in
  1. Home/
  2. Dushyanth Srinivasan/
  3. Week 4.1 - Solving second order ODEs

Week 4.1 - Solving second order ODEs

In this project, I will be writing code in MATLAB to solve the motion of a simple pendulum. A simple pendulum motion's depends on Newton's Second Law. The equation which governs the motion of a simple pendulum is (with damping) d2θdt2+bmdθdt+gLsinθ=0d2θdt2+bmdθdt+gLsinθ=0 Where, θθ is the angular displacement…

  • MATLAB
  • Dushyanth Srinivasan

    updated on 23 Aug 2022

In this project, I will be writing code in MATLAB to solve the motion of a simple pendulum.

A simple pendulum motion's depends on Newton's Second Law. The equation which governs the motion of a simple pendulum is (with damping)

d2θdt2+bmdθdt+gLsinθ=0d2θdt2+bmdθdt+gLsinθ=0

Where, θθ is the angular displacement of the pendulum (radrad), bb is the damping coefficient (Ns/mNs/m), mm is the mass of the pendulum (kgkg), gg is the gravitational acceleration (m/s2m/s2) and LL is the length of the pendulum's string (mm).

This equation is a second order differential equation, these equations cannot be directly solved in MATLAB and hence will be subjected to a few changes as follows:

Letdθ1dt=θ2andθ=θ1Letdθ1dt=θ2andθ=θ1

Substituting θ=θ1θ=θ1 in d2θdt2+bmdθdt+gLsinθ=0d2θdt2+bmdθdt+gLsinθ=0

d2θ1dt2+bmdθ1dt+gLsinθ1=0d2θ1dt2+bmdθ1dt+gLsinθ1=0

d(dθ1dt)dt+bmdθ1dt+gLsinθ1=0d(dθ1dt)dt+bmdθ1dt+gLsinθ1=0

Substituting dθ1dt=θ2dθ1dt=θ2,

dθ2dt+bmθ2+gLsinθ1=0dθ2dt+bmθ2+gLsinθ1=0

Rearranging,

dθ2dt=−bmθ2−gLsinθ1=0dθ2dt=-bmθ2-gLsinθ1=0

dθ1dt=θ2dθ1dt=θ2

The second order differential equation has been converted into a set of two first order differential equations, which can be solved in MATLAB easily.

The conditions of the simulation are as follows:

L = 1 mm, m = 1 kg, b = 0.05 Ns/mNs/m, g = 9.81 m/s2m/s2.

The intial conditions are as follows:

Angular displacement = 0 radrad, angular velocity = 0.5 rad/srad/s. (Note: angular velocity of 3 rad/srad/s meant that the pendulum did multiple full revolutions around the center.

Code (with explanation)

Function

The purpose of this function is to solve the set of ODEs and return the solution. The input paramets are the time span and initial conditions while the angular displacement and angular velocity of the pendulum are the output parameters.

filename: ode_pendulum.m

% purpose of this function is to solve the set of ODEs and return the solution

function [thetadot] = ode_pendulum (t, theta)

    % initialising input values
    b = 0.05;
    m = 1;
    l = 1;
    g = 9.81;
    
    thetadot = zeros (2,1); 
    
    thetadot(1) = theta(2);  % equation 1
    
    thetadot(2) = (-1*(b*theta(2)/m)-(g*sind(theta(1))/l)); % equation 2

end

Main Program

The main program will call the function, and receive the solution. The received solution will be plotted and the animation will be generated as well.

clear all
close all
clc

% initialising input values
b = 0.05; % damping coefficient (Ns/m)
m = 1;    % mass of pendulum's ball (kg)
l = 0.2;  % length of string of pendulum (m)
g = 9.81; % gravitational acceleration (m/s^2)

% the timespan/time period of oscillation
t = linspace(0,20,100);

% initial conditions. initial angular displacement is 0 rad and initial angular
% velocity is 0.5 rad/s
theta0 = [0;0.5];

% ode function time period in theta1, ode45 solves for displacement and velocity
% in theta(:,1) and theta (:,2) respectively
[t ,theta] = ode45('ode_pendulum', t, theta0);

ct = 1; % counter varaible for animation frames

% for loop to generate animation of the pendulum
for i=1:length(theta)

    x=l*sin(theta(i,1));  
    y=-1*l*cos(theta(i,1));  % calculating coordinates of center of pendulum ball

    plot([-0.1 0 0.1],zeros(1,3),'linewidth',3) % plotting horizontal line
    hold on
    plot(linspace(x,0,5) ,linspace(y,0,5),'color','bla') % plotting string of pendulum
    hold on
    plot(x,y,'o','markersize',20,'color','r') % plotting the pendulum
    hold on
    axis([-0.25 0.25 -0.3 0.1]) % making the pendulum centered in the animation
    axis off % removing uneseccary clutter
    M(ct)=getframe(gcf); % storing each frame in structure M
    ct = ct + 1;
    clf 
end  

close all
% converting frames into a video
videofile=VideoWriter('test.avi','Uncompressed AVI');
open (videofile);
writeVideo (videofile,M);
close(videofile);

% figure showing plot of time vs angular displacement

figure (2)
plot(t,theta(:,1),'color','r','linewidth',1) 
xlabel('Time (s)')
ylabel('Angular Displacement (rad)')

% figure showing plot of time vs angular velocity

figure (3)
plot(t,theta(:,2),'color','b','linewidth',1)
xlabel('Time (s)')
ylabel('Angular Velocity (rad/s)')

Output

Angular Displacement vs Time

Graph of angular displacement vs time is generated, the displacement eventually tends to zero due to damping but this is not visible due to limited simulation time.

Angular Velocity vs Time

Graph of angular velocity vs time is generated, the velocity eventually tends to zero due to damping but this is not visible in this graph due to limited simulation time.

Animation of Pendulum

https://youtu.be/HOCVqsgXsg0

Errors Faced

The function ode_pendulum couldn't take more than two input parameters.

Command Window Screenshot

Solution to the Error

Instead of passing b, g, m and l as parameters, they were defined in the function.

Leave a comment

Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.

Please  login to add a comment

Other comments...

No comments yet!
Be the first to add a comment

Read more Projects by Dushyanth Srinivasan (45)

Project 2 - Rankine cycle Simulator

Objective:

  In this project, I will be writing code in MATLAB to simulate a Rankine Cycle for the given parameters. A Rankine Cycle is an ideal thermodynamic heat cycle where mechanical work is extracted from the working fluid as it passes between a heat source and heat sink. This cycle or its derivatives is used in steam engines…

calendar

04 Sep 2022 12:52 PM IST

  • MATLAB
Read more

Project 1 - Parsing NASA thermodynamic data

Objective:

In this project, I will be parsing a data file prepared by NASA. The contents of the data file can be used to generated thermodynamic properties such as Specific Heat at Constant Pressure 'C_p' (J/(kg.K)J/(kg.K)), Enthalpy HorQHorQ (JJ) and Entropy SS (J/(kg.mol)J/(kg.mol)) at various temperatures. The files will be parsed in MATLAB…

calendar

31 Aug 2022 01:07 PM IST

  • MATLAB
Read more

Week 5 - Genetic Algorithm

Objective:

In this project, I will be generating a stalagmite function in MATLAB and find the global maxima of the function using Genetic Algorithm. A stalagmite function is a function which generates Stalactites, which are named after a natural phenomenon where rocks rise up from the floor of due to accumulation of droppings of…

calendar

29 Aug 2022 07:55 AM IST

  • MATLAB
Read more

Week 4.1 - Solving second order ODEs

Objective:

In this project, I will be writing code in MATLAB to solve the motion of a simple pendulum. A simple pendulum motion's depends on Newton's Second Law. The equation which governs the motion of a simple pendulum is (with damping) d2θdt2+bmdθdt+gLsinθ=0d2θdt2+bmdθdt+gLsinθ=0 Where, θθ is the angular displacement…

calendar

23 Aug 2022 08:06 AM IST

  • MATLAB
Read more

Schedule a counselling session

Please enter your name
Please enter a valid email
Please enter a valid number

Related Courses

coursecardcoursetype

Post Graduate Program in Computer Vision for Autonomous Vehicles

4.7

223 Hours of Content

coursecardcoursetype

Post Graduate Program in Autonomous Vehicles

Recently launched

88 Hours of Content

coursecard

Simulation and Design of Power Converters for EV using MATLAB and Simulink

4.9

22 Hours of Content

coursecard

Introduction to Hybrid Electric Vehicle using MATLAB and Simulink

4.8

23 Hours of Content

coursecardcoursetype

Mechanical Engineering Essentials Program

4.7

21 Hours of Content

Schedule a counselling session

Please enter your name
Please enter a valid email
Please enter a valid number

              Do You Want To Showcase Your Technical Skills?
              Sign-Up for our projects.