Challenge

Week 3 - Solving second order ODEs

Aim:- Solving second order ODEs

  • 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. Mathematical descriptions of change use differentials and derivatives. Various differentials, derivatives, and functions become related via equations, such that a differential equation is a result that describes dynamically changing phenomena.

In engineering, ODE is used to describe the transient behaviour of a system. A example is a simple pendulum. The position(theta) of the bob with respect to time of a simple pendulum can be determined by using the second order differential equations.

  • Governing equations:-

The second order ode for simple pendulum will be as follows,

Programme written to solve 2nd order ordinary differential equation (ODE)

To solve 2nd order ODE converted to 1st order such as expaline below:

`1st order ODE is [(dθ1/dt)=0] ,

2nd order ODE is [ (dθ1^2/dt + (b/m.(dθ1/dt))+(g/l.(sinθ1))=0 ]`

Now,

(dθ^2/dt) = (dθ2/dt),

hence dθ/dt=θ2

hence equation becomes [(dθ1^2/dt + (b/m.(dθ1/dt))+(g/l.(sinθ1))= (dθ2/dt)+ b/m.(θ2)+ g/l.(sinθ2)]

Where, b = damping coefficient

m = mass of the bob ( kg),

g = acceleration due to gravity( m/s^2),

l = length of the string.(m)

The simple pendulum problem can be solved by using the second order ode functions and by solving these we will get the theta1 an theta2 values of the simple pendulum

3.Objectives of the project:

  1. to write a program that solves the following ODE which represents the equation of motion of a simple pendulum with damping.

4.Body of the content:

Introduction:

Steps in code

1.Programme wtitten in metlab software

2.All the Input parameters values are given to programme such as: b,l,g,m

3.Initial values of theta, time,angular velocity are entered

4. Use of ode45 function is done to solve problem.

  1. Explanation of ‘ode45’ function syntax
  2. @(t,theta)-defines ode is function of t &theta,
  3. ode_func(t,theta,b,g,l,m)-defining function,
  4. t_span-time array to decide integration limits,
  5. theta_0-initial condition of pendulum.

5.Commands to generate plot and position of pendulum and video file are written.

%% It is metlab Code
%% Programme for ordinary differential equation(ODE)
%% 1st order ODE is [(dθ1/dt)=0] ,
%% 2nd order ODE is [ (dθ1^2/dt + (b/m.(dθ1/dt))+(g/l.(sinθ1))=0 ]
%% b-damping coefficient, m- mass, g-gravity, l-length of pendulum

%%%%%%%%%%%%%%%%%%%%%%%%
%% Programme written to solve 2nd order ordinary differential equation(ODE)
%% to solve 2nd order ODE converted to 1st order  
%%[(dθ1^2/dt + (b/m.(dθ1/dt))+(g/l.(sinθ1))= (dθ2/dt)+ b/m.(θ2)+ g/l.(sinθ2)]

%%%
clc
close all
clear all

b=0.05;  %% dampig coefficient
g=9.81;  %% gravitational force [m/s^2)
l=1 ;    %% lengh of pendulum [m]
m=1;     %% mass of pendulum bob [kg]

%% Intial condition for pendulum
theta_0 = [0;3];  %% intial displacemnt of pendulum is 0 with velocity 8 m/s

%% time points
t_span= linspace(0,20,100);   %% time array i.e time increseaed from 0 to 30 sec in 400 divisions

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

figure(1)
plot(t,theta(:,1));
hold on;
plot(t,theta(:,2));
xlabel('time')
ylabel('Velocity')

figure(2)
plot(theta(:,1),theta(:,2))
xlabel('time')
ylabel('velocity')
ct=1;
for i= 1:length(Results)
  x1=0;
  y1=0;
  x2=1*sin(theta(i,1));
  y2=-1*cos(theta(i,1));
  x3=-1;
  y3=0;
  x4=1;
  y4=0;
  figure(3)
  plot([x1,x2],[y1,y2],'linewidth',4,'color','r')
  hold on
  plot([x3,x4],[y3,y4],'linewidth',4,'color','b')
  hold off
  axis([-3 3 -3 3]);
  pause(0.04);
 M(ct)=getframe(gcf);
 ct=ct+1;

end
%%% Creating the animation
movie(M)
videofile=VideoWriter('ode_func.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Code for ode function

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

Plots:

fig.2

fig.3

Error in command widow: Error showing for ode function written

solved as [t,results] replaced with [t,theta] as written in prgramme to save results .

Error 2: at line 55 syntax error

Error solved by correcting Brackets as per format.

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.