CHAPTER 2

image

First Order Differential Equations. Exact Equations, Separation of Variables, Homogeneous and Linear Equations

First Order Differential Equations

Although it implements only a relatively small number of commands related to this topic, MATLAB’s treatment of differential equations is nevertheless very efficient. We shall see how we can use these commands to solve each type of differential equation algebraically. Numerical methods for the approximate solution of equations and systems of equations are also implemented.

The basic command used to solve differential equations is dsolve. This command finds symbolic solutions of ordinary differential equations and systems of ordinary differential equations. The equations are specified by symbolic expressions where the letter D is used to denote differentiation, or D2, D3, etc, to denote differentiation of order 2,3,…, etc. The letter preceded by D (or D2, etc) is the dependent variable (which is usually y), and any letter that is not preceded by D (or D2, etc) is a candidate for the independent variable. If the independent variable is not specified, it is taken to be x by default. If x is specified as the dependent variable, then the independent variable is t. That is, x is the independent variable by default, unless it is declared as the dependent variable, in which case the independent variable is understood to be t.

You can specify initial conditions using additional equations, which take the form y(a) = b or Dy(a) = b,…, etc. If the initial conditions are not specified, the solutions of the differential equations will contain constants of integration, C1, C2,…, etc. The most important MATLAB commands that solve differential equations are the following:

dsolve(‘equation’, ‘v’): This solves the given differential equation, where v is the independent variable (if ‘v’ is not specified, the independent variable is x by default). This returns only explicit solutions.

dsolve(‘equation’, ‘initial_condition’,…, ‘v’): This solves the given differential equation subject to the specified initial condition.

dsolve(‘equation’, ‘cond1’, ‘cond2’,…, ‘condn’, ‘v’): This solves the given differential equation subject to the specified initial conditions.

dsolve(‘equation’, ‘cond1, cond2,…, condn’, ‘v’): This solves the given differential equation subject to the specified initial conditions.

dsolve(‘eq1’, ‘eq2’,…, ‘eqn’, ‘cond1’, ‘cond2’,…, ‘condn’ , ‘v’): This solves the given system of differential equations subject to the specified initial conditions.

dsolve(‘eq1, eq2,…, eqn’, ‘cond1, cond2,…, condn’ , ‘v’): This solves the given system of differential equations subject to the specified initial conditions.

maple(‘dsolve(equation, func(var))’): This solves the given differential equation, where var is the independent variable and func is the dependent variable (returns implicit solutions).

maple(‘dsolve({equation, cond1, cond2,… condn}, func(var))’): This solves the given differential equation subject to the specified initial conditions.

maple(‘dsolve({eq1, eq2,…, eqn}, {func1(var), func2(var),… funcn(var)})’): This solves the given system of differential equations (returns implicit solutions).

maple(‘dsolve(equation, func(var), ’explicit’)’): This solves the given differential equation, offering the solution in explicit form, if possible.

Examples are given below.

First, we solve differential equations of first order and first degree, both with and without initial values.

» pretty(dsolve('Dy = a*y'))

  C2 exp(a t)

» pretty(dsolve('Df = f + sin(t)'))

              sin(t)   cos(t)
  C6 exp(t) - ------ - ------
                2        2

The previous two equations can also be solved in the following way:

» pretty(sym(maple('dsolve(diff(y(x), x) = a * y, y(x))')))

y(x) = exp(a x) _C1

» pretty(maple('dsolve(diff(f(t),t)=f+sin(t),f(t))'))

f(t) = - 1/2 cos(t) - 1/2 sin(t) + exp(t) _C1

» pretty(dsolve('Dy = a*y', 'y(0) = b'))

exp(a x) b

» pretty(dsolve('Df = f + sin(t)', 'f(pi/2) = 0'))

     /   pi
  exp| - -- | exp(t)
        2  /          sin(t)   cos(t)
  ------------------ - ------ - ------
          2              2        2

Now we solve an equation of second degree and first order.

» y = dsolve('(Dy) ^ 2 + y ^ 2 = 1', ' y(0) = 0', 's')

y =

 cosh((pi*i)/2 + s*i)
 cosh((pi*i)/2 - s*i)

We can also solve this in the following way:

» pretty(maple('dsolve({diff(y(s),s)^2 + y(s)^2 = 1, y(0) = 0}, y(s))'))

y(s) = sin(s), y(s) = - sin(s)

Now we solve an equation of second order and first degree.

» pretty(dsolve('D2y = - a ^ 2 * y ', 'y(0) = 1, Dy(pi/a) = 0'))

exp(-a t i)   exp(a t i)
----------- + ----------
       2            2

Next we solve a couple of systems, both with and without initial values.

>> dsolve('Dx = y', 'Dy = -x')

ans =

    y: [1x1 sym]
    x: [1x1 sym]

>> y

y =

 cosh((pi*i)/2 + s*i)
 cosh((pi*i)/2 - s*i)

>> x

x =

x

>> y=dsolve('Df = 3*f+4*g', 'Dg = -4*f+3*g')

y =

    g: [1x1 sym]
    f: [1x1 sym]

>> y.g

ans =

C27*cos(4*t)*exp(3*t) - C28*sin(4*t)*exp(3*t)

>> y.f

ans =

C28*cos(4*t)*exp(3*t) + C27*sin(4*t)*exp(3*t)

>> y=dsolve('Df = 3*f+4*g, Dg = -4*f+3*g', 'f(0)=0, g(0)=1')

y =

    g: [1x1 sym]
    f: [1x1 sym]

>> y.g

ans =

cos(4*t)*exp(3*t)

>> y.f

ans =

sin(4*t)*exp(3*t)

This last system can also be solved in the following way:

» pretty(maple('dsolve({diff(f(x),x)= 3*f(x)+4*g(x), diff(g(x),x)=-4*f(x)+3*g(x),     f(0)=0,g(0)=1}, {f(x),g(x)})'))

{f(x) = exp(3 x) sin(4 x), g(x) = exp(3 x) cos(4 x)}

Separation of Variables

A differential equation is said to have separable variables if it can be written in the form

image

This type of equation can be solved immediately by putting image

If MATLAB cannot directly solve a differential equation with the function dsolve, then we can try to express it in the above form and solve the given integrals algebraically, which does not present particular difficulties for the program, given its versatility in symbolic computation.

EXERCISE 2-1

Solve the differential equation:

image

First of all we try to solve it directly. The equation can be written in the form:

image

» dsolve('Dy = y * cos(x) /(1+y^2)')

ans =
                                                           0
exp(C33 + t*cos(x))*exp(-wrightOmega(2*C33 + 2*t*cos(x))/2)

Thus the differential equation appears not to be solvable with dsolve. However, in this case, the variables are separable, so we can solve the equation as follows:

» pretty(solve('int(cos(x), x) = int((1+y^2) / y, y)'))

  +-                          -+
  |         /           2     |
  |         |          y  |    |
  |     asin| log(y) + -- |    |
  |                   2  /    |
  |                            |
  |           /           2   |
  |           |          y  |  |
  |  pi - asin| log(y) + -- |  |
  |                     2  /  |
  +-                          -+

Thus, after a little rearrangement, we see that the general solution is given by:

image

We now find the value of the constant C via the initial condition, putting x = 0 and y = 1.

» C = simple('solve(subs(x = 0, y = 1, sin(x) = log(y) + 1/2 * y ^ 2 + C), C)')

C =

-1/2

Thus the final solution is image

In the same way you can solve any other differential equation with separable variables.

The above differential equation is also solvable directly by using:

» pretty(maple('dsolve(diff(y(x), x) = y(x) * cos(x) /(1 + y(x) ^ 2), y(x))'))

                                          2
                      log(y(x)) + 1/2 y(x) - sin(x) = _C1

Homogeneous Differential Equations

Consider a general differential equation of first degree and first order of the form

image

This equation is said to be homogeneous of degree n if the functions M and N satisfy:

image

For this type of equation, we can transform the initial differential equation (with variables x and y), via the change of variable x = vy, into another (separable) equation (with variables v and y). The new equation is solved by separation of variables and then the solution of the original equation is found by reversing the change of variable.

EXERCISE 2-2

Solve the differential equation:

image

First we check if the equation is homogeneous

» maple('m:=(x,y) - > x ^ 2 - y ^ 2');
» maple('n:=(x,y) - > x * y ');
» factor('m(t*x,t*y)')

ans =

t ^ 2 *(x-y) *(x + y)

» factor('n(t*x,t*y)')

ans =

t ^ 2 * x * y

Thus the equation is homogeneous of degree 2. To solve it we apply the change of variable x = vy.

Before performing the change of variable, it is advisable to load the library difforms, using the command maple(‘with(difforms)’), which will allow you to work with differential forms. Once this library is loaded it is also convenient to use the command maple(‘defform(v=0,x=0,y=0)’), which allows you to declare all variables which will not be constants or parameters in the differentiation.

» maple('with(difforms)');
» maple('defform(v=0,x=0,y=0)');

Now we can make the change of variable x = vy, and group the terms in d(v) and d(y).

» simplify('subs(x = v * y m(x,y) * d(x) + n(x,y) * d(y))')

ans =

v ^ 2 * y ^ 3 * d(v) + v ^ 3 * y ^ 2 * d(y) - y ^ 3 * d(v)

» pretty(maple('collect(v ^ 2 * y ^ 3 * d(v) + v ^ 3 * y ^ 2 * d(y) - y ^ 3 * d(v) {d(v), d(y)})'))

                2  2    2                       2
              (v  y  - y )(y d(v) + v d(y)) + y  v d(y)

If we divide the previous expression by v3y3, and group the terms in d(v) and d(y), we already have an equation in separated variables.

» pretty(maple('collect(((v^2*y^3-y^3) * d(v) + v ^ 3 * y ^ 2 * d(y)) /(v^3*y^3), {d(v), d(y)})'))

                     2  3    3
                   (v  y  - y ) d(v)    d(y)
                    ----------------- + ----
                            3  3          y
                           v  y

The previous expression can be simplified.

» pretty(maple('convert(collect(((v^2*y^3-y^3) * d(v) + v ^ 3 * y ^ 2 * d(y)) /(v^3*y^3), {d(v), d(y)}), parfrac, y)'))

                                 2
                         (v  - 1) d(v)    d(y)
                          ------------- + ----
                                 3          y
                                v

Now, we solve the equation:

» pretty(simple('int((v^2-1)/v ^ 3, v) + int(1/y,y)'))

                                        1
                             log(v) + ---- + log(y)
                                         2
                                      2 v

Finally we reverse the change of variable:

» pretty(simple('subs(v = x / y log(v) + 1/2/v ^ 2 + log(y))'))

                                               2
                                             y
                               log(x) + 1/2 ----
                                              2
                                             x

Thus the general solution of the original differential equation is:

image

Now we can represent the solutions of this differential equation graphically. To do this we graph the solutions with parameter C, which is equivalent to the following contour plot of the function defined by the left-hand side of the above general solution (see Figure 18-1):

» [x,y]=meshgrid(0.1:0.05:1/2,-1:0.05:1);
» z=y.^2./(2*x.^2)+log(x);
» contour(z,65)

9781484203118_Fig02-01.jpg

Figure 2-1.

Exact Differential Equations

The differential equation

image

is said to be exact if image. If the equation is exact, then there exists a function F such that its total differential dF coincides with the left-hand side of the above equation, i.e.:

image

therefore the family of solutions is given by F(x,y) = C.

The exercise below follows the usual steps of an algebraic solution to this type of equation.

EXERCISE 2-3

Solve the differential equation:

image

First of all, we try to solve the equation with dsolve:

» maple('m:=(x,y) - > - 1 + y * exp(x*y) + y * cos(x*y)');
» maple('n:=(x,y) - > 1 + x * exp(x*y) + x * cos(x*y)');
» dsolve('m(x,y) + n(x,y) * Dy = 0')

??? Error using ==> dsolve
Explicit solution could not be found.

Thus the function dsolve does not give a solution to the proposed equation. We are going to try to solve the equation using the classical algebraic method.

First we check that the proposed differential equation is exact.

» pretty(simple(diff('m(x,y)','y')))

exp(y x) + x y exp(y x) + cos(y x) - x sin(y x) y

» pretty(simple(diff('n(x,y)','x')))

exp(y x) + x y exp(y x) + cos(y x) - x sin(y x) y

Since the equation is exact, we can find the solution in the following way:

» solution1 = simplify('int(m(x,y), x) + g(y)')

solution1 =

-x+exp(y*x) + sin(y*x) + g(y)

Now we find the function g(y) via the following condition:

image

» pretty(simplify('int(m(x,y), x) + g(y)'))

                   -x + exp(y x) + sin(y x) + g(y)

» pretty(simplify('diff(-x+exp(y*x) + sin(y*x) + g(y), y)'))

                                               d
                  x exp(y x) x + x cos(y x) + -- g(y)
                                              dy

» simplify('solve(x * exp(y*x) + x * cos(y*x) + diff(g(y), y) = n(x,y), diff(g(y), y))')

ans =

1

Thus g'(y) = 1, so the final solution will be, omitting the addition of a constant:

» pretty(simplify('subs(g(y) = int(1,y),-x+exp(y*x) + sin(y*x) + g(y))'))

-x + exp(y x) + sin(y x) + y

To graphically represent the family of solutions, we draw the following contour plot of the above expression (Figure 18-2):

» [x,y]=meshgrid(-2*pi/3:.2:2*pi/3);
» z =-x+exp(y.*x) + sin(y.*x) + y;
» contour(z,100)

9781484203118_Fig02-02.jpg

Figure 2-2.

In the following section we will see how any reducible differential equation can be transformed to an exact equation using an integrating factor.

Linear Differential Equations

A linear first order differential equation is an equation of the form:

image

where P(x) and Q(x) are given functions of x.

Differential equations of this type can be transformed into exact equations by multiplying both sides of the equation by the integrating factor:

image

and the general solution is then given by the expression:

image

MATLAB implements these solutions of linear differential equations, and offers them whenever the integral appearing in the integrating factor can be found.

EXERCISE 2-4

Solve the differential equation:

image

» pretty(simple(dsolve('x * Dy + 3 * y = x * sin(x)')))

                    /   3 t
             C36 exp| - --- |
  x sin(x)              x  /
  -------- - ----------------
     3              3

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset