odes1

Upload: tushar-deshpande

Post on 03-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 ODEs1

    1/15

    MEEN 364 ParasuramJuly 27, 2001

    1

    HANDOUT M.4 - SOLUTIONS OF ORDINARY DIFFERENTIALEQUATIONS

    Introduction

    The majority of physical systems of interest in this course can be represented in the formof ordinary differential equations. In order to study the response of the system modeling,the differential equations must be solved. The general solution of a linear differentialequation is the sum of two components, the particular integral and the complementaryfunction. Often the particular integral is the steady-state component of the solution of thedifferential equation, while the complimentary function, which is the solution of thecorresponding homogenous equation, is the transient component of the solution. Oftenthe steady state component of the solution has the same form as the driving or (external)function.

    Solution of a second order differential equation

    The model of a majority of mechanical systems can be approximated with a second order differential equation. So the solution of a second order differential equation is important,as it can be used to study the response of many physical systems.

    Case 1: Constant forcing function

    Consider, for example a mass spring damper system subjected to a constant force F, asshown in the following figure:

    F

    m

    k c

    The governing differential equation of motion of the mass, m is given by

    ,)()()(...

    F t kxt xct xm =++ (1)

    where dividing the entire equation by m, we have

  • 8/11/2019 ODEs1

    2/15

    MEEN 364 ParasuramJuly 27, 2001

    2

    Note that the time dependence is assumed and hence it is omitted for futuremanipulations.

    ....

    m F

    xmk

    xmc

    x =++ (2)

    Let

    .

    ,2

    2n

    n

    mk mc

    =

    =

    So equation (2) reduces to

    .2 2...

    m F

    x x x nn =++ (3)

    As explained earlier, the solution to the above differential equation has two parts, the particular integral and the complimentary function. To find the particular integral, whichis nothing but the steady-state solution, equate all of the derivate terms to zero, i.e.,

    .0

    ,0.

    ..

    =

    =

    x

    x

    Substituting the above relation in equation (3), we have

    m F

    x

    m F

    x

    n s

    n

    2

    2

    =

    = (4)

    The second of equations (4) represents the steady-state response of the system.

    To find the complimentary function or the transient response, we need to consider the

    homogenous equation, i.e.,

    02 2...

    =++ x x x nn . (5)

    Equation (5) is the left hand side of equation (3), with the right hand side set to zero.

    To solve the above equation, first assume a solution of the form

  • 8/11/2019 ODEs1

    3/15

    MEEN 364 ParasuramJuly 27, 2001

    3

    t Ae x = . (6)

    Substituting the above relation in equation (5), we get

    02 22 =++ t nt nt e Ae Ae A (7) 0)2( 22 =++ t nn e A A A (8)

    0=t e is a trivial solution, as equation (6) will then be identically equal to zero, i.e.,0 x . But we need to find the non-trivial solution, so

    0t e .

    So from equation (8), we have

    02 22 =++ nn (9)

    Solving the above quadratic equation, we find the following roots

    .1

    where

    1

    2

    442

    2

    2,1

    22,1

    222

    2,1

    =

    ==

    =

    nd

    d n

    nn

    nnn

    Therefore the transient solution of the system is given as

    t t t

    d nd n Be Ae x )()( + += (10)

    where A and B can be found based on the initial conditions. So the final solution of thedifferential equation given by equation (1) is

    t t

    n

    t s

    d nd n Be Aem F

    x

    x x x

    )()(

    2

    + ++=

    +=

    For an unforced system, the differential equation is given by

    0...

    =++ kx xc xm . (11)

  • 8/11/2019 ODEs1

    4/15

    MEEN 364 ParasuramJuly 27, 2001

    4

    Since the forcing function is equal to zero, here the steady state response of the systemwill be equal to zero, while the transient response will be the same as that of the forcedsystem. In other words the solution of the equation (11) is given as

    t t

    t t

    t s

    d nd n

    d nd n

    Be Ae x

    Be Ae x

    x x x

    )()(

    )()(0

    +

    +

    +=++=

    +=

    Case 2: Time varying forcing function

    In this case, instead of a constant forcing function driving the system as in case 1, let usconsider a harmonic function, Fe jwt , which by Eulers expansion is equal to

    )sin(cos t jt F + , driving the system. w is the frequency with which the drivingfunction is applied on the system. The governing differential equation will be

    .2 2...

    ...

    jwt o

    jwt nn

    jwt

    e F em F

    x x x

    Fekx xc xm

    ==++

    =++

    (12)

    Usually the form of the solution is similar to that of the driving function. In this case, toobtain the steady state component, we assume a solution of the form

    jwt Ce x = .

    Substituting the above relation in equation (12), we have

    022

    022

    }2){(

    2

    F C j

    e F eC e jC eC

    nn

    jwt jwt n

    jwt n

    jwt

    =+=++

    (13)

    Diving the second equation of equation (13) by 2n , results in

    .

    where

    ,}2)1{(}2)1{( 22

    20

    n

    n

    r

    r r k

    F

    r r

    F

    C

    =

    +=

    +=

    Once the value of C is obtained, the steady state solution of the system is

    jwt s Ce x = .

  • 8/11/2019 ODEs1

    5/15

    MEEN 364 ParasuramJuly 27, 2001

    5

    The complimentary function or the transient response of this system is the same as that of the previous case, as the homogenous equation is the same. So the entire solution to thesystem subjected to a harmonic driving function is given by

    .

    where

    ,}2)1{(

    conditionsinitialfromobtainedBandA

    where,

    2

    )()(

    n

    jwt t t

    st

    r

    r r k

    F C

    Ce Be Ae x

    x x xd nd n

    =

    +=

    ++=

    +=+

    Using MATLAB to solve the differential equations

    The ODE command in MATLAB can be used to solve various differential equations.The use of this command is explained in detail with the help of the following example.

    Example 1

    Consider the system below.

    m

    k c

    This system is similar to the one in case 1, except that the forcing function is equal tozero. Therefore the governing differential equation of motion of the system is

    0...

    =++ kx xc xm . (14)

  • 8/11/2019 ODEs1

    6/15

    MEEN 364 ParasuramJuly 27, 2001

    6

    Problem statement

    Solve for the response of the unforced system defined by the differential equation given by equation (14). Assume = 0.1; m = 1Kg; k = 100 N/m. Let the initial conditions bechosen as

    sm x

    m x

    /0)0(

    02.0)0(.

    =

    =

    Solution

    Equation (14) is a second order constant-coefficient differential equation. To solve thisequation we have to reduce it into two first order differential equations. This step is taken

    because MATLAB uses a Runge-Kutta method to solve differential equations, which isvalid only for first order differential equations.

    Let

    ..

    v x = (15)

    From the above expression we see that

    0.

    =++ kxcvvm , (16)

    so equation (16) reduces to

    ].)()[(.

    xmk v

    mcv = (17)

    We can see that the second order differential equation (14) has been reduced to two firstorder differential equations (15) and (17).

    For our convenience, put

    x = y (1);

    );2(.

    yv x ==

    Note that, the variables x and v are the states of the system, and are each equated toanother variable y(1) and y(2) respectively to be compatible with MATLAB. Thesevariables are not the outputs of the system.

  • 8/11/2019 ODEs1

    7/15

    MEEN 364 ParasuramJuly 27, 2001

    7

    Equations (15) and (17) reduce to

    )1(.

    y = y (2); (18)

    )2(.

    y = [(-c/m)*y (2) (k/m)*y (1)]; (19)

    To calculate the value of c, compare equation (14) with the following generalizedequation.

    02 2...

    =++ nn x x .

    Equating the coefficients of the similar terms we have

    nmc 2= (20)

    mk

    n =2 . (21)

    Using the values of m and k, calculate the value of c corresponding to the value of .Once the value of c is known, equations (18) and (19) can be solved using MATLAB.

    Some of the properties of the system are defined as follows.

    Natural frequency n = )/( mk = 10 rad/sec.For = 0.1Damped natural frequency d = n 21 = 9.95 rad/sec.Damped time period T d = 2/ d = 0.63 sec.Therefore for to plot five time cycles, the time interval should be 5 times the dampedtime period, i.e., 3.16 sec.

    MATLAB Code

    In order to apply the ODE45 or any other numerical integration procedure, a separatefunction file must be generated to define equations (18) and (19). Actually, the right handside of equations (18) and (19) are what is stored in the file. The equations are written inthe form of a vector.

    The MATLAB code is given below.

    function yp = unforced(t,y)yp = [y(2);(-((c/m)*y(2))-((k/m)*y(1)))]; (22)

    In the above code yp is a two dimensional vector representing the right hand side of theequations (18) and (19), respectively. Open a new M-file and write down the above two

  • 8/11/2019 ODEs1

    8/15

    MEEN 364 ParasuramJuly 27, 2001

    8

    lines. The first line of the function file must start with the word function and the filemust be saved corresponding to the function call; i.e., in this case, the file is saved asunforced.m. The derivatives are stored in the form of a vector.

    This example problem has been solved for = 0.1. We need to find the value of c/mand k/m so that the values can be substituted in equation (22). Substituting the values of and n in equations (20) and (21) the values of c/m and k/m can be found out. After finding the values, substitute them into equation (22). In this example

    .1001

    100

    ,2101.02

    ==

    ==

    mk mc

    Now we need to write a code, which calls the above function and solves the differentialequation and plots the required result. First open another M-file and type the followingcode.

    tspan=[0 4];y0=[0.02;0];[t,y]=ode45( 'unforced' ,tspan,y0);

    figure(1);plot(t,y(:,1));grid onxlabel(time)ylabel(Displacement)title(Displacement Vs Time)

    The ode45 command in the main body calls the function unforced, which defines thesystems first order derivatives. The response is then plotted using the plot command.tspan represents the time interval and y0 represents the initial conditions for y(1) andy(2), which in turn represent the displacement x and the first derivative of x, i.e., thevelocity. In this example, the initial conditions are taken as 0.02 m for x and 0 m/sec for the first derivative of x or the velocity of the mass m.

    MATLAB uses a default step value for the vector tspan. In order to change the step sizeuse the following code

    tspan=0:0.001:4;

    This tells MATLAB that the vector tspan is from 0 to 4 with a step size of 0.001. Thisexample uses an arbitrary step size. If the step size is too large, the plot obtained will not

    be a smooth curve. So it is always better to use relatively smaller step size. But it alsotakes longer to simulate programs with smaller step size. So you have to decide which isthe best step size you can use for a given problem.

  • 8/11/2019 ODEs1

    9/15

    MEEN 364 ParasuramJuly 27, 2001

    9

    In the above code y(:,1) represents the displacement x. To plot the velocity, change thevariable in the plot command line to y(:,2).

    The plot is attached below

    Note: Any n th order differential equation can be solved using MATLAB by following the procedure outlined in example 1. In other words, the n th order differential equation can bereduced to n first order differential equations and the ode command in MATLAB can

    be used to solve the system of n first order differential equations.

  • 8/11/2019 ODEs1

    10/15

    MEEN 364 ParasuramJuly 27, 2001

    10

    Example 2. Double Pendulum

    Derive the governing differential equation of motion of a double pendulum. Solve thederived differential equation and plot the values of 1 and 2 with respect to time.Choose l 1 = 1m; l 2 = 1m; m 1 = m 2 = 5Kg. The initial conditions for 1 and 2 are 0.5233and 0.5233 radians respectively.

    1

    l1, m 1

    2 l2 , m 2

    Solution

    When represented in a matrix form, the differential equations of motion for the abovesystem is

    ++=

    +

    )122

    1

    .

    1122

    )12

    2.

    222121..

    2

    ..

    1

    221212

    1222112

    sin(sin

    sin(sin)()cos(

    )cos()(

    l mw

    l mwwl ml m

    l ml mm

    MATLAB Code

    These coupled second order differential equations must be converted into a vector of firstorder differential equation. This step is done by the following substitutions.

    Substituting the above relations in the original nonlinear differential equation, we get thefollowing first order differential equations,

    );4();3(

    );2(

    );1(

    .

    2

    2

    .

    1

    1

    y y

    y

    y

    ==

    =

    =

    ++=

    +

    )sin(sin

    )4(

    )sin(sin)(

    )2(

    )4(

    )3(

    )2(

    )1(

    0)cos(0

    0100

    )cos(0)(0

    0001

    12

    21

    .

    1122

    12

    2.

    222121

    .

    .

    .

    .

    221212

    1222112

    l mw

    y

    l mww

    y

    y

    y

    y

    y

    l ml m

    l ml mm

  • 8/11/2019 ODEs1

    11/15

    MEEN 364 ParasuramJuly 27, 2001

    11

    In this type of a problem where the coefficient matrix is a function of the states or thevariables, a separate M-file must be written which incorporates a switch/case

    programming with a flag case of mass.

    For example if the differential equation is of the form,

    ),()(),(.

    yt F t y yt M =

    then the right hand side of the above equation has to be stored in a separate m-file calledF.m. Similarly the coefficient matrix should be stored in a separate m-file namedM.m. So, when the flag is set to the default, the function F.m is called and later whenthe flag is set to mass the function M.m is called.

    The code with the switch/case is given below. Note that it is a function file and should besaved as indmot_ode.m in the current directory.

    function varargout=indmot_ode(t,y,flag)

    switch flagcase '' %no input flag varargout{1}=pend(t,y);case 'mass' %flag of mass calls mass.m varargout{1}=mass(t,y);otherwise error([ 'unknown flag ''' flag '''.' ]);end

    To store the right hand side of the state variable matrix form of the model, a separatefunction file must be written as shown below. Note that the name of the function ispend, so this file must be saved as pend.m.

    %the following function contains the right hand side of the%differential equation of the form%M(t,y)*y'=F(t,y)%i.e. it contains F(t,y).it is also stored in a separate file named,pend.m.

    function yp= pend(t,y)M1=5;M2=5;g=9.81;l1=1;l2=1;w2=M2*9.81;w1=M1*9.81;yp=zeros(4,1);yp(1)=y(2);yp(2)=-(w1+w2)*sin(y(1))+M2*l2*(y(4)^2)*sin(y(3)-y(1));yp(3)=y(4);yp(4)=-w2*sin(y(3))-M2*l1*(y(2)^2)*sin(y(3)-y(1));

  • 8/11/2019 ODEs1

    12/15

    MEEN 364 ParasuramJuly 27, 2001

    12

    Similarly, to store the coefficient matrix a separate function file is written which is storedas mass.m.

    % the following function contains the mass matrix.%it is separately stored in a file named, mass.m

    function m = mass(t,y)M1=5;M2=5;g=9.81;l1=1;l2=1;m1=[1 0 0 0];m2=[0 (M1+M2)*l1 0 M2*l2*cos(y(3)-y(1))];m3=[0 0 1 0];m4=[0 M2*l1*cos(y(3)-y(1)) 0 M2*l2];m=[m1;m2;m3;m4];

    To plot the response, the main file should call the function indmot_ode.m, which hasthe switch/case programming which in turn calls the corresponding functions dependingon the value of the flag. For the main file to recognize the coefficient matrix, theMATLAB command ODESET is used to set the mass to M (t, y).

    The MATLAB code for the main file is given below

    % this is the main file, which calls the equations and solves usingode113.%it then plots the first variable.

    tspan=[0 10]y0=[0.5233;0;0.5233;0]

    options=odeset( 'mass' , 'M(t,y)' )[t,y]=ode113( 'indmot_ode' ,tspan,y0,options)

    subplot(2,1,1)plot(t,y(:,1))gridxlabel( 'Time' )ylabel( 'Theta1' )

    subplot(2,1,2)plot(t,y(:,3))gridxlabel( 'Time' )ylabel( 'Theta2' )For help regarding subplot use the online help in MATLAB by typing the followingstatement at the command prompt.

    help subplot

    subplot(m,n,p), breaks the Figure window into an m-by-n matrix of small axes andselects the p-th axes for the current plot. The axes are counted along the top row of theFigure window, then the second row, etc. For example,

  • 8/11/2019 ODEs1

    13/15

    MEEN 364 ParasuramJuly 27, 2001

    13

    subplot(2,1,1), plot(income) subplot(2,1,2), plot(outgo)

    plots income on the top half of the window and outgo on the bottom half.

    The plot has been attached.

    Assignment

    1) For the system shown below,

  • 8/11/2019 ODEs1

    14/15

    MEEN 364 ParasuramJuly 27, 2001

    14

    l

    m

    m = 10 g;l = 5 cms;

    The initial conditions are (0) = 90 and 0)0(.

    =

    a) Derive the governing differential equation. b) Solve the above differential equation obtained analytically and plot the value of thetawith respect to time.c) Solve the differential equation using MATLAB and again plot the value of theta withrespect to time. Compare the results obtained from (2) and (3).

    2) For the system shown below

    F

    m

    k c

    Using MATLAB, plot the response of a forced system given by the equation

    t f kx xc xm sin...

    =++

    For = 0.1.Take m = 5 kg; k = 1000 N/m; f = 50 N; = 4 n, x(0) = 5 cms;

    .

    x (0) = 0.

  • 8/11/2019 ODEs1

    15/15

    MEEN 364 ParasuramJuly 27, 2001

    15