Some Examples


This is an evaluated Mathematica notebook. Since the notebook is intended to be interactive, it may be helpful to also view the unevaluated version

Mathematica can solve many of the ODE's encountered in your class. The main package is called DSolve. This routine is built into Mathematica but a more powerful version exist which can be loaded.

In[1]:=

  <<Calculus`DSolve`

In the first example we are looking for the general solution.

In[2]:=

  DSolve[x y'[x] + y[x] == 1, y[x], x]

Out[2]=

                C[1]
  {{y[x] -> 1 + ----}}
                 x

In the second example we will include initial conditions. Notice that the initial conditions are included with the equation. The answer looks strange, but a plot of the solution gives an idea of the solution.

In[3]:=

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

Out[3]=

                                 ((-1 - 3 I Sqrt[7]) x)/8
            (63 + 19 I Sqrt[7]) E
  {{y[x] -> --------------------------------------------- + 
                                 42
   
                            ((-1 + 3 I Sqrt[7]) x)/8
       (63 - 19 I Sqrt[7]) E
       ---------------------------------------------}}
                            42

In[4]:=

  Plot[y[x] /. %, {x,0,10}]

Out[5]=

  -Graphics-

Often Mathematica can evaluate the differential equation but then not solve for y in terms of x.

In[6]:=

  DSolve[{y'[x] == (2 x - 3 y[x])/(-x + y[x]), y[1] == 2},
          y[x],x]

Out[6]=

                   x + y[x]
         2 ArcTanh[---------]           2                  2
                   Sqrt[3] x    Log[-2 x  + 2 x y[x] + y[x] ]
  {Solve[-------------------- + ----------------------------- == 
               Sqrt[3]                        2
   
      4 Sqrt[3] ArcTanh[Sqrt[3]] + 3 Log[6]
      -------------------------------------, y[x]]}
                        6

If DSolve fails, often a numerical approximation of the solution can be obtained.

In[7]:=

  NDSolve[{y'[x] == Cos[y[x]] + Sin[x]/(1+x), y[0] == 1},
           y[x], {x,0,20}]

Out[7]=

  {{y[x] -> InterpolatingFunction[{0., 20.}, <>][x]}}

In[8]:=

  Plot[Evaluate[y[x] /. %] , {x,0,20}]

Out[9]=

  -Graphics-

It is also possible to plot direction fields. Suppose the differential equation is y'[x] == Cos[x] y + Sin[x] y^2.

In[10]:=

  <<Graphics`PlotField`;
  
  PlotVectorField[{1, Cos[x] y + Sin[x] y^2}, {x,0,6},
    {y,-2,2}, ScaleFunction -> (.5 &)]

Out[11]=

  -Graphics-

Go up to the Applied Math Page.