Challenge

Week 3 - Solving second order ODEs

AIM :

  • To simulate a program in Matlab and to slove the motion of a Simple Pendulum with damping effect ,using a second oder differential equation(ODE) .

OBJECTIVE :

  • To write a program to simulate the transient behaviour of Simple Pendulum .
  • To create an animation of motion of simple Pendulum using Matlab .

THEORY : ORDINARY DIFFERENTIAL EQUATION (ODE)

  • In mathematics ,an ODE is a differential equation containing one or more independent variables and the derivatives of those function .The differential equation classified into many equation they are Linear ,Homogenous , non - Homogenous , non - Linear equation .
  • In engineering ,ODE is used to describe the transient behaviour of system .An example given for this is Simple Pendulum .

SIMPLE PENDULUM :

  • A simple pendulum is a mechanical system of mass attached to a long massless inextensible string that performs Oscillatory motion .
  • Pendulum were used to keep a track of time in ancient periods .
  • The pendulum is also used for identifying the beats.

DIAGRAM :

MOTION OF SIMPLE PENDULUM :

  • The differentail equation for a motion of a simple pendulum with damping is given below

`d^2theta/dt^2 +b/m(d theta/dt) +g/l sintheta = 0`

where

b = Damping coefficient ,

m = mass of the pendulum in kg ,

l = length of the pendulum in m,

g = gravity in `m/s^2`.

  • This is Second Oder Ordinary Differential Equation ,because this equation is in form of oder '2'.

DERIVATION : Let us consider ,

`theta = theta_1` and so ,

`d theta /dt = d theta_1/dt = theta_2` equation 1

`d^2 theta/dt^2 = d^2theta_1/dt^2 = d/dt(d theta_1/dt) = d theta_2/dt` equation 2

  • substituting equation 1 and 2 in these given equation .

`d^2theta/dt^2 +b/m(d theta/dt) +g/l sintheta = 0`

`d^2 theta/dt^2 +b/m(d theta_1/dt) + g/lsintheta_1 = 0`

`d^2 theta_2/dt +b/mtheta_2+g/lsintheta_1 = 0`

Therefore,

`d theta_1/dt = theta_2`

`d theta_2/dt = -b/mtheta_2 -g/lsintheta_1`

Now, let us convert this equation into a matrix form

`d/dt[theta_1;theta_2] = [theta_2;-b/mtheta_2-g/lsintheta]`

where

`theta_1`= Angular Displacement of pendulum ,

`theta_2`= Angular Velcity of pendulum .

PROGRAM :

clear all 
close all 
clc
%  Inputs 
% damping coefficient 
 b = 0.05;
% length of the pendulum in (m)
 l = 1;
% mass of the pendulum in (kg ) converted into (m)
 m = 0.1;
% gravity in (m/s^2)
 g = 9.81;
% Initial condition 
 theta_0 = [0;3];
% time point 
 t_span = linspace(0,20,450);
% solving ODE 
 [t,result] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);

% plotting the angular displacement and angular velocity 
 plot(t,result(:,1),'linewidth',1)
 hold on 
 plot(t,result(:,2),'linewidth',1.5)
 title('Simple Pendulum with damping ')
 xlabel('Time in (s)')
 ylabel('Angular dispalcement and Angular velocity')
 legend('Angular displacement','Angular velocity')
 grid on 

 % Animation 
  ct = 1 ;
  for i = 1:length(t_span);
      x0 = 0;
      y0 = 0;

      x1 =  l*sin(result(i,1));
      y1 = -l*cos(result(i,1));

      plot([-1 1],[0 0],'linewidth',5,'color','g')
      axis([-2 2 -2 2]);
      line([x0 x1],[y0 y1],'linewidth',4,'color','b')
      hold on 
      plot(x1,y1,'o','markersize',30,'markerfacecolor','r','markeredgecolor','r')
      hold off
      grid on 
      pause(0.1)
      M(ct) = getframe(gcf);
      ct = ct+1;
  end 

  % Animation of motion of Simple Pendulum 
   movie(M)
   videofile = VideoWriter('Simple_pendulum.avi','Uncompressed AVI')
   open(videofile)
   writeVideo(videofile,M)
   close(videofile)

CODE EXPLANATION :

  • The program starts with clear all ,close all , clc command.
  • 'clear all ' command is used to clear the data stored in the workspace .
  • 'close all ' command is used to close the plot and figures .
  • 'clc' command is used to clear the command window .
  • Then the given input values of b,g,l,m entered and inital conditions for angular displacement is `theta_1`is 0 and angular velocity `theta_2`is 3 rad/sec.
  • And time span is taken to be 0 to 20 with 450 intervals.
  • A function code is used in the case to solve the second oder differential equation .
  • 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​
  • ode45 - is inbuilt function in matlab.
  • counter vaule command is used to ,so that the variable increments by specific step value in every iteration of a loop.
  • 'for' loop is used for continous motion to the pendulum.
  • The 'pause ' command is used for animation speed and slow.
  • The 'getframe ' command is used to capture the motion.
  • To save the video 'videofile' commad is used.and finally 'close' command is used to close the videofile.

ERRORS :

  • These are the errors made while programming

rectified = missed parentheses.

rectified = instead markerfacecolor, markeredgecolor made mistake of markface,markedge.

OUTPUT :

ANIMATION :

https://youtu.be/IG67Kr3YFVA

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.