First-Order Ordinary Differential Equations

Mathematica has two routines that are designed to solve differential equations, DSolve and NDSolve. DSolve is used to find closed-form solutions, and NDSolve finds solutions numerically. Both can find solutions to ordinary as well as partial differential equations. See Mathematica's Help Browser for syntax and other useful information.

Mathematica can solve the homogeneous linear ODE:

DSolve [ y ' [ x ] + p [ x ] y [ x ] 0 , y [ x ] , x ]

{ { y [ x ] 1 x - p [ K$26117 ] K$26117 C [ 1 ] } }

The dummy integration variables and any integration constants are picked by Mathematica .  Note that we asked Mathematica to solve the most general form of homogeneous linear first-order ODE, and we got a solution in a very general form that is equivalent to Kreyszig Eq. (3), p. 33.  Here is a more specific problem and the solution found by Mathematica:

DSolve [ y ' [ x ] + ( 2 x + 1 ) y [ x ] 0 , y [ x ] , x ]

{ { y [ x ] - x - x 2 C [ 1 ] } }

Boundary conditions are introduced in the following way to generate a particular solution:

DSolve [ { y ' [ x ] + ( 2 x + 1 ) y [ x ] 0 , y [ 0 ] == 4 } , y [ x ] , x ]

{ { y [ x ] 4 - x - x 2 } }

Mathematica can solve the heterogeneous linear ODE:

DSolve [ y ' [ x ] + p [ x ] y [ x ] r [ x ] , y [ x ] , x ]

{ { y [ x ] 1 x - p [ K$26153 ] K$26153 C [ 1 ] + 1 x - p [ K$26153 ] K$26153 1 x - 1 K$26169 - p [ K$26153 ] K$26153 r [ K$26169 ] K$26169 } }

Again, we asked Mathematica to solve the most general form of homogeneous linear first-order ODE, and we got a solution in a very general form that is equivalent to Kreyszig Eq. (4), p. 34.  Here is a more specific problem and the solution found by Mathematica (see Kreyszig Example 1, p. 34):

DSolve [ y ' [ x ] - y [ x ] 2 x , y [ x ] , x ]

{ { y [ x ] 2 x + x C [ 1 ] } }

The Bernoulli equation is a first-order nonlinear ODE that has a form that can be reduced to a linear ODE as described in Kreyszig, pp. 36–38.

BernoulliEquation = y ' [ x ] + p [ x ] y [ x ] == r [ x ] ( y [ x ] ) ^ ( a )

p [ x ] y [ x ] + y [ x ] r [ x ] y [ x ] a

The substitution y(x) = ( u ( x ) ) 1 / ( 1 - a ) is made, resulting in a linear ODE that can be solved for u(x):

yRep = u[x]^1/(1 - a)

DyRep = D[yRep, x]

u [ x ] 1 1 - a

General :: spell1 : Possible spelling error: new symbol name \" DyRep \" is similar to existing symbol \" yRep \". More… "Possible spelling error: new symbol name \\\"\\!\\(DyRep\\)\\\" is similar to existing symbol \\\"\\!\\(yRep\\)\\\". \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, ButtonData:>\\\"General::spell1\\\"]\\)"

u [ x ] - 1 + 1 1 - a u [ x ] 1 - a

step1 = BernoulliEquation /. { y [ x ] yRep , y ' [ x ] DyRep }

p [ x ] u [ x ] 1 1 - a + u [ x ] - 1 + 1 1 - a u [ x ] 1 - a r [ x ] ( u [ x ] 1 1 - a ) a

step2 = PowerExpand [ step1 ]

p [ x ] u [ x ] 1 1 - a + u [ x ] - 1 + 1 1 - a u [ x ] 1 - a r [ x ] u [ x ] a 1 - a

step3 = Simplify [ step2 ]

u [ x ] - a - 1 + a ( ( - 1 + a ) r [ x ] - ( - 1 + a ) p [ x ] u [ x ] + u [ x ] ) - 1 + a 0

Solve [ step3 , u ' [ x ] ]

{ { u [ x ] - ( - 1 + a ) ( r [ x ] - p [ x ] u [ x ] ) } }

This last result is the first-order linear ODE that results from the Bernoulli equation. Its solution gives the function u(x) which can be converted back to y(x) with the relation y(x) = ( u ( x ) ) 1 / ( 1 - a ) .

Numerical Solutions to ODEs

The following nonlinear ODE either does not have a solution, or takes too long to find.

DSolve [ Sin [ 2 Pi y ' [ x ] ^ 2 ] == y [ x ] x , y [ x ] , x ]

Solve :: ifun : Inverse functions are being used by Solve , so some solutions may not be found; use Reduce for complete solution information. More… "Inverse functions are being used by \\!\\(Solve\\), so some solutions may not be found; use Reduce for complete solution information. \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, ButtonData:>\\\"Solve::ifun\\\"]\\)"

DSolve [ Sin [ 2 π y [ x ] 2 ] x y [ x ] , y [ x ] , x ]

NDSolve is a numerical method for finding a solution. An initial condition and the desired range of solution are required.

solution = NDSolve [ { Sin [ 2 Pi y ' [ x ] ^ 2 ] == y [ x ] x , y [ 0 ] == 1 } , y , { x , 0 , 3.5 } ]

Solve :: ifun : Inverse functions are being used by Solve , so some solutions may not be found; use Reduce for complete solution information. More… "Inverse functions are being used by \\!\\(Solve\\), so some solutions may not be found; use Reduce for complete solution information. \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, ButtonData:>\\\"Solve::ifun\\\"]\\)"

{ { y InterpolatingFunction [ { { 0. , 3.5 } } , <> ] } , { y InterpolatingFunction [ { { 0. , 3.5 } } , <> ] } }

The results look kind of strange, perhaps, but they are a set of rules that provide a function that interpolates between values.   
    Here is how to find the approximate solution at three different values of x on the specified interval:

y [ 0.5 ] /. solution

{ 0.9074370101865736 , 1.0973325823998172 + 0. }

y [ 1 ] /. solution

{ 0.744970458518884 , 1.308772544883195 - 0.014353625780704213 }

y [ Pi ] /. solution

{ 0.05249827705955056 , 2.5018649128156945 - 0.6106702474967095 }

Note that Mathematica has found two solutions, the first is real and the second is complex. Below are plots of the real and imaginary parts for both solutions:

Plot [ Evaluate [ Re [ y [ x ] /. solution ] ] , { x , 0 , 3.5 } , PlotStyle { { Hue [ 1 ] , Thickness [ 0.01 ] } , { Hue [ 0.6 ] , Thickness [ 0.01 ] } } ]

[Graphics:HTMLFiles/Lecture-20_3.gif]

Graphics

Plot [ Evaluate [ Im [ y [ x ] /. solution ] ] , { x , 0 , 3.5 } , PlotStyle { { Hue [ 1 ] , Thickness [ 0.01 ] } , { Hue [ 0.6 ] , Thickness [ 0.01 ] } } ]

[Graphics:HTMLFiles/Lecture-20_4.gif]

Graphics

Note that for x < 0.85 (approximately), both solutions are real.


Created by Mathematica  (November 6, 2005) Valid XHTML 1.1!