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

Mechanical

Electrical

Civil

Computer Science

Electronics

Offline Program

All Courses

All Courses

logo

CHOOSE A CATEGORY

Mechanical

Electrical

Civil

Computer Science

Electronics

Offline Program

Top Job Leading Courses

Automotive

CFD

FEA

Design

MBD

Med Tech

Courses by Software

Design

Solver

Automation

Vehicle Dynamics

CFD Solver

Preprocessor

Courses by Semester

First Year

Second Year

Third Year

Fourth Year

Courses by Domain

Automotive

CFD

Design

FEA

Tool-focused Courses

Design

Solver

Automation

Preprocessor

CFD Solver

Vehicle Dynamics

Machine learning

Machine Learning and AI

POPULAR COURSES

coursePost Graduate Program in Hybrid Electric Vehicle Design and Analysis
coursePost Graduate Program in Computational Fluid Dynamics
coursePost Graduate Program in CAD
coursePost Graduate Program in CAE
coursePost Graduate Program in Manufacturing Design
coursePost Graduate Program in Computational Design and Pre-processing
coursePost Graduate Program in Complete Passenger Car Design & Product Development
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. Amit Chilap/
  3. Solving second order ODEs

Solving second order ODEs

1.AIM: To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.   2.GOVERNING EQUATIONS IF ANY:  The main function used in this program is “ode45”, its information is provided below. Ode45 is a function in MATLAB that solves ordinary…

  • MATLAB
  • Amit Chilap

    updated on 06 Feb 2021

1.AIM:

To write a program that solves the following ODE.

This ODE represents the equation of motion of a simple pendulum with damping.

 

2.GOVERNING EQUATIONS IF ANY: 

  • The main function used in this program is “ode45”, its information is provided below.

Ode45 is a function in MATLAB that solves ordinary or partial differential equations using a numerical method. The numerical method in question here is the 4th order Runge Kutta method. 

Before looking at the syntax and the function, let us understand what we mean by solving an ode and the Runge-Kutta method. 

To solve an ODE, we have to integrate and solve for the variable. Assuming we have an ode du, we would solve for u by integrating du provided some limits of integration. However, rather than direct integration, we can use a numerical solver, by discretizing the equation, that is, we convert the ODE into a linear equation and then solve this equation to approximate the solution.

For our purpose, we will be looking at the Runge-Kutta method, which is a first order ODE solver for initial value problems. That is, provided an initial condition, we will be able to use the 4th order RK method to solve the solution. 

 

How the RK method works: 

The RK method requires, 

An initial condition (y0).

Step size (h).

Assuming we have a problem, y’ = f(t,y)

For the RK method, we need to define 4 terms, 

where h is the step size or time step. 

Using these terms, we use the relation provided below to solve for the solution at the next time step. 

Let us take the example and take a look at an example. 

From this we get y at the first time step, h=0.1 to be 1.1107. The value at the 2nd time step can be figured out using this value as a stepping point and we can step up to our end time. 

 

How ode45 works: 

The function ode45 takes 3 inputs. The 3 inputs are the function to be integrated, the time span, and the initial conditions. 

The function would be one like shown in the above example. The time span would be the integration limits. This can be provided as a vector of 2 values, the initial and final time, or with a spacing using the linspace function. The solver will choose a step size that is most suitable for the solution to obtain a solution in the most optimal time possible. The initial condition would be the value of the function at t=0s. 

The function can be provided in the form of an anonymous function, 

 

[t,results] = ode45(@(x) x^2, tspan,y0)

 

Here, we are saying that we don’t know the value of x and that x is an input to the anonymous function that we have defined here. This is equivalent to defining a function that takes the input of x and gives the output of x^2. 

 

The function can also be defined as a separate function. This function must take at least 2 inputs, which should be t and y, apart from other arguments, where t would be the current time and y would be the value of the function at the previous time step. 

 

Assuming we have a function ode, the function must be defined as, 

 

function [output] = ode(t,y,args) 

 

where args refer to any other arguments that the function may take. 

 

This function must then be input to ode45 as 

 

[t,results] = ode45(@(t,y) ode(t,y,args), tspan, y0)

 

This is necessary to tell the function which arguments of the function are to be found by the solver.

As mentioned above, the RK method is a first order ODE solver and hence, so is ode45. Thus, any ODE to be integrated by ode45 must be provided as a set of first order ODEs.

 

3.OBJECTIVES OF THE PROJECT:

  • The Objective is to write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.

   

Given data:

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

Simulate the motion between 0-20 sec, for angular displacement=0, angular velocity=3 rad/sec at time t=0.

  • Following is the workflow of the program.
    • We first convert the 2nd ODE into the 1st ODE.
    • The conversation from 2nd ODE to 1st ODE is done in the supplementary program.
    • We use the main inbuild function “ode45” to solve the 1st ODE.
    • Ode45 using the RK method as mentioned above solve the 1st ODE and provides the results of the given equation.
    • As in the results, the angular displacement and the angular velocity are calculated for each step value for a given time span.
    • After getting the results then we plot the angular displacement and angular velocity vs time for a given time span.
    • After getting every position of the pendulum from the results we plot its position for each time step.
    • And by collecting every plot position of the pendulum we create the animation movie of the motion of the pendulum from the given initial conditions.

 

4.BODY OF THE CONTENT:

  • MATLAB Programming language is used.
  • Main Program is attached below. 
  • %Objective is to write a program that solves the following ODE. 
    %This ODE represents the equation of motion of a simple pendulum with damping.
    %(d^2(θ))/(dt^2 )+(b/m)*dθ/dt+(g/L)*sinθ=0.
    %Given data: L=1 metre, m=1 kg, b=0.05, g=9.81 m/s^2.
    %Simulate the motion between 0-20 sec, for angular displacement=0, angular velocity=3 rad/sec at time t=0.
     
    close all
    clear all
    clc
     
    b=0.05; %Damping Coefficient of Pendulum.
    g=9.81; %Gravitational Acceleration. In m/sec^2.
    l=1; %Length of Pendulum String. In m.
    m=1; %Mass of the Pendulum Bob. In kg.
     
    theta_0=[0;3]; %Initial Conditions of the Pendulum Position/Displacement and Velocity.
    t_span=linspace(0,20,500); %Time span of Pendulum Motion.
     
    [t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0); %Solving the Differential Equation.
     
    figure(1) %Figure for plotting Displacement, Velocity VS Time.
    hold on %To add multiple Plots in single Figure.
    plot(t,results(:,1),'color','r') %Plotting Displacement VS Time.
    plot(t,results(:,2),'color','g') %Plotting Velocity VS Time.
    xlabel('TIME'); %Labelling X-Axis.
    ylabel('Displacement(Red) & Velocity(Green)') %Labelling Y-Axis.
    legend('Displacement','Velocity') %Displaying the Legends for Figure(1).
    title('Displacement, Velocity VS Time') %Providing Title to Figure(1).
     
    d=rad2deg(results(:,1))+180; %Converting Radians To Degree.
     
    %Making Animation for Pendulum Motion.
    ct=1;%Creating a string to record both Pendulum Positions.
    for i=1:length(d); %Using 'for' loop to collect every position of Pendulum.
    theta1=d(i); %Providing the angle to collect a position of Pendulum.
     
    %coordinates
    x0=0; %X-Coordinates of string attached to the Celling/Holder.
    y0=l+1; %Y-Coordinates of string attached to the Celling/Holder.
     
    x1 = l*sind(theta1); %X-Coordinates of string attached to the Pendulum Bob.
    y1 = 1+l+l*cosd(theta1); %Y-Coordinates of string attached to the Pendulum Bob.
     
     
    %Plotting
    figure(2) %Figure for plotting Motion of Pendulum.
    plot([x0 x1],[y0 y1],'linewidth',5) %Plotting of String.
    hold on %To add multiple Plots in single Figure.
    plot ([-y0/2 y0/2],[y0 y0],'linewidth',5) %Plotting of Celling/Holder of Pendulum.
    plot(x0,y0,'o','markersize',10,'markerfacecolor','[0 0 0 ]','markeredgecolor','r') %Plotting of Pendulum joint/hinge to Celling.
    plot(x1,y1,'o','markersize',25,'markerfacecolor','r','markeredgecolor','g') %Plotting of Pendulum Bob.
    hold off %Stopping addition of multiple Plots in single Figure.
    title('Motion of Damped Pendulum') %Providing Title to Figure(2).
    text(-1.8,2.75,'Pendulum bob is dropped from the 0 angular displacement &');
    text(-1.8,2.5,'at 3 rad/sec angular velocity with damping coefficient of 0.05.');
    text(-1.8,2.25,‘Given data: L=1 metre, m=1 kg, b=0.05, g=9.81 m/s^2');
     
    axis([-y0 y0 0 y0+1]) %Giving axis limits.
    if ct==1;
    pause(1) %Pause time for Displaying Initial Condition.
    else
    pause(0.002) %Pause time between the two consecutive positions of Pendulum.
    end
    M(ct) = getframe(gcf);%Convert the image of Pendulum Positions to movie frame.
    ct=ct+1; %Changing the string to record the next Pendulum Positions.
     
     
    end %Ending 'for' function.
     
     
    movie(M) %Function using the make a movie/animation from movie frames.
    v_p=VideoWriter('Motion_of_Damped_Pendulum.avi','Uncompressed AVI'); %Creating Video file name and profile of it.
    open(v_p) %Opening the video file.
    writeVideo(v_p,M) %Writing in the video file with collected movie frames.
    close(v_p) %Closing the video file.
    
  • Supplementary Program for this program.
  • %Supplementary programs:Function(ode_func) is to Solve ODE.
     
    function[dtheta_dt]=ode_func(t,theta,b,g,l,m)
     
    %Given Function/ODE is (d^2{theta}/d{t}^2)+(b/m)*(d{theta}/d{t})+(g/l)*sin(theta)=0
     
    %Simplifying to give ODE to solve.
    theta1=theta(1); %Collecting Angular Displacement.
    theta2=theta(2); %Collecting Angular Velocity.
    dtheta1_dt=theta2; %Relation between Angular Velocity & Angular Displacement.
    dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1); %Simplified given
    dtheta_dt=[dtheta1_dt;dtheta2_dt]; %Output equation of function.
    end
    
    

 

  • Step-Wise Explanation is provided above in the code.
  • Since Checking the program at every step, No Error was found during programming.
  • Therefore, no steps are taken to overcome Errors, & no Screenshots showing errors thrown in the command window

 

  • Results

 

  • I have also made a program with different initial conditions mentioned below.
  • The pendulum bob is dropped from the pi/2 angular displacement & at 0 rad/sec angular velocity with a damping coefficient of 0.15(b=0.15) with Length = 4 metre, Mass = 2 kg, g=9.812 m/s2.
  • The output of that program is displayed below in the video.
  •  

 

5.CONCLUSION:

  • Displacement & Velocity is reduced gradually, it directly depends upon the damping coefficient.
  • Higher the damping coefficient faster the rate of reduction of Displacement and Velocity.
  • Similarly, an increase in mass lowers the rate of reduction of Displacement and Velocity, & vice versa.
  • The above conclusions are only possible when the damping coefficient and mass is greater than zero.

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 Amit Chilap (11)

Week-3 Challenge: ADVISOR Tool

Objective:

Introduction to HEV using MATLAB & Simulink Week-3 Challenge: ADVISOR Tool   AIM: To simulate the given data and conditions for an EV using Advisor Tool in MATLAB. About ADVISOR ADVISOR, NREL’s Advanced Vehicle Simulator, is a set of model, data, and script text files for use with MATLAB and Simulink. It…

calendar

04 Jul 2022 11:04 AM IST

  • BIM
  • CAE
  • CFD
  • CSS
  • DEM
  • FEA
  • GIS
  • HEV
  • MATLAB
  • MBD
Read more

Project -BAJA All Terrain Vehicle (ATV) model

Objective:

Simulink for Mechanical & Electrical Engineers - Challenges Final Project Aim To study, analyze and make a detailed report on BAJA All Terrain Vehicle (ATV) model using Simulink & compare between its different modes. Objective Prepare a technical report explaining the model properties & comments on the results.…

calendar

03 Jun 2021 03:25 AM IST

    Read more

    Week - 4

    Objective:

    Simulink for Mechanical & Electrical Engineers Challenges = Week 4 Aim To Make a Simulink model using State-Flow for given questions. Questions & Solution Q1. Implement control logic of a “washing machine” using Stateflow as per given sequence: If the power supply is available, the system gets activated. If the Water supply…

    calendar

    21 May 2021 06:29 PM IST

    • MATLAB
    Read more

    Week -2

    Objective:

    Simulink for Mechanical & Electrical Engineers Challenges = Week 2 Aim To Make a Simulink model of Doorbell using solenoid block. To Use a thermistor to sense the temperature of a heater & turn on or turn off the fan according to temperature. Questions & Solution Q1. Make a Simulink model of Doorbell using…

    calendar

    14 May 2021 12:30 AM IST

      Read more

      Schedule a counselling session

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

      Related Courses

      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

      coursecard

      Vehicle Dynamics using MATLAB

      4.8

      37 Hours of Content

      coursecard

      Introduction to CFD using MATLAB and OpenFOAM

      4.8

      13 Hours of Content

      Schedule a counselling session

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

      logo

      Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.

      https://d27yxarlh48w6q.cloudfront.net/web/v1/images/facebook.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/insta.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/twitter.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/youtube.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/linkedin.svg

      Our Company

      News & EventsBlogCareersGrievance RedressalSkill-Lync ReviewsTermsPrivacy PolicyBecome an Affiliate
      map
      EpowerX Learning Technologies Pvt Ltd.
      4th Floor, BLOCK-B, Velachery - Tambaram Main Rd, Ram Nagar South, Madipakkam, Chennai, Tamil Nadu 600042.
      mail
      info@skill-lync.com
      mail
      ITgrievance@skill-lync.com

      Top Individual Courses

      Computational Combustion Using Python and CanteraIntroduction to Physical Modeling using SimscapeIntroduction to Structural Analysis using ANSYS WorkbenchIntroduction to Structural Analysis using ANSYS Workbench

      Top PG Programs

      Post Graduate Program in Hybrid Electric Vehicle Design and AnalysisPost Graduate Program in Computational Fluid DynamicsPost Graduate Program in CADPost Graduate Program in Electric Vehicle Design & Development

      Skill-Lync Plus

      Executive Program in Electric Vehicle Embedded SoftwareExecutive Program in Electric Vehicle DesignExecutive Program in Cybersecurity

      Trending Blogs

      How to Learn CAD Design: Step-by-Step GuideGD&T Basics: How to Read and Apply GD&T Symbols SolidWorks vs Creo: Best CAD Software to Learn for Mechanical Engineers Engineering Edtech in India: Busting Myths & Building Careers How to Get a Core Mechanical Engineering Job After Graduation

      © 2025 Skill-Lync Inc. All Rights Reserved.

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