Demo 2.1

Using DSolve for first order ODE's and plotting solutions

Finding the general solution

Buttons for the command DSolve can be found in the BasicCalculations Palette under Calculus/Differential Equations. Move the cursor to a new cell and press the first DSolve button.

DSolve[\[Placeholder]  ==  \[Placeholder],  \[Placeholder],  
      \[Placeholder]]

The part before the comma is for the equation. It must be expressed in terms of y'[t], y[t], and t.
(Recall that the equals sign for equations is ==.) Then comes the function we are solving for, y[t], and finally the variable t.

DSolve[y'[t] - 2  y[t]  ==  4  t,  y[t],  t]

This gives the general solution in terms of an arbitrary constant C[1]. Let's give this a name so that we can work with it. We should always clear y before solving.

Clear[y]

soln = 
      DSolve[y'[t] - 2  y[t]  ==  4  t,  y[t],  t]  //  
        Simplify

The result is a list of lists. The first (and in this case only) list on the inside contains a rule that a solution should follow. We convert this to a function and replace C[1] by c.

y[t_, c_] = y[t]  /.  First[soln]  /.  C[1]  ->  c

y[1, 2]

y[t, 2]

Solving initial value problems

Move the cursor to a new cell and press the second DSolve button.

DSolve[{\[SelectionPlaceholder] == \[Placeholder], 
        \[Placeholder] == \[Placeholder]}, \[Placeholder], 
      \[Placeholder]]

The second equation is the initial condition, y[0]=c. (Of course 0 could be replaced by another number).

Clear[y]

soln = 
      DSolve[{y'[t] - 2  y[t] == 4  t, y[0] == c}, y[t], t]
            //  Simplify

y[t_, c_] = y[t]  /.  First[soln]

y[1, 2]

Note that the two types of solutions give different answers.

Plotting solutions

We want to plot on one graph several solutions of a given differential equation. To do so, we collect the solutions in a Table and then Plot the Table.

?Plot

?Table

For a Table the range {c,-5,5,2} will make c take the values -5,-3,-1,1,3, and 5, i.e., c goes from -5 to 5 in increments of 2.

Clear[y]

Table[y[t, c], {c, -5, 5, 2}]

We can  take y[t,c] to be either the general solution or the solution in terms of an initial condition. The second command will put  all the functions in the Table. Be sure to use a lower case "c", since the upper case "C" is protected and cannot be used. The range for t  and the choices for c may be edited for each problem.

Plot[Evaluate[Table[y[t, c], {c, -5, 5, 2}]], {t, -5, 5}, 
      AspectRatio  ->  1]

Example 1.                    y' - 2 y = 4t

Let's apply this to the first example above.

Clear[y]

soln = 
      DSolve[y'[t] - 2  y[t]  ==  4  t,  y[t],  t]  //  
        Simplify

y[t_, c_] = y[t]  /.  First[soln]  /.  C[1]  ->  c

Plot[Evaluate[Table[y[t, c], {c, -5, 5, 2}]], {t, -5, 5}, 
      AspectRatio  ->  1]

Plotting just a single function, say  y(t,1), can be done simply by

Plot[y[t, 1], {t, -5, 5}, AspectRatio  ->  1]

Example 2.                              y' + 3 y  = (E^(-t))

Clear[y]

soln = 
      DSolve[y'[t] + 3  y[t]  ==  E^(-t),  y[t],  t]
            //  Simplify

y[t_, c_] = y[t]  /.  First[soln]  /.  C[1]  ->  c

Plot[Evaluate[Table[y[t, c], {c, -5, 5, 2}]], {t, -5, 5},  
      AspectRatio  ->  1]

Example 3.                               y' + (( 1)/t) y = -3 (t^2)

Let's find and plot the solutions in terms of the initial values at t=1.

Clear[y]

soln = 
      DSolve[{y'[t] + 1/t y[t] == -3 t^2, y[1] == c}, 
          y[t], t]  //  Simplify

y[t_, c_] = y[t]  /.  First[soln]

Plot[Evaluate[Table[y[t, c], {c, -5, 5, 2}]], {t, 0.1, 3}, 
      AspectRatio  ->  1]

A Palette

Here is a Palette for the commands above. It was generated in the same way as the Palette for PlotVectorField in Demo 1.1.

({{General Solution}, {\(Clear[y]; 

            soln = DSolve[
                  ■\  == \ 
                    ■, \ y[t], \ t]\  // \ Simplify
                \)}, {y[t_, c_] = 
              y[t]  /.  First[soln]  /.  C[1]  ->  c}, {General Initial Value Problem}, {\(Clear[y]; 

            soln = DSolve[{
                    ■ == ■, 
                    y[■] == c}, y[t], t]\  // \ 
                Simplify\)}, {y[t_, c_] = y[t]  /.  First[soln]}, {\(Plot[Evaluate[
                Table[y[t, c], 
                  \ {c, ■, ■, 
                    ■}]], \ 

              	{t, \ ■, ■}, \ 
              AspectRatio\  -> \ 1]\)}})

The following two examples were done using the Palette.

Example 4.                   y' = y

\(Clear[y]; 

    soln = DSolve[\(y'\)[t]\  == \ y[t], \ y[t], \ t]\  // \ Simplify
        \)

y[t_, c_] = y[t]  /.  First[soln]  /.  C[1]  ->  c

\(Plot[Evaluate[Table[y[t, c], \ {c, \(-4\), 4, 2}]], \ 

      	{t, \ \(-1\), 5}, \ AspectRatio\  -> \ 1]\)

Example 5.                    y' = y,   y[1]=c

\(Clear[y]; 

    soln = DSolve[{\(y'\)[t] == y[t], y[1] == c}, y[t], t]\  // \ 
        Simplify\)

y[t_, c_] = y[t]  /.  First[soln]

y[2, 3]

\(Plot[Evaluate[Table[y[t, c], \ {c, \(-4\), 4, 2}]], \ 

      	{t, \ \(-1\), 3}, \ AspectRatio\  -> \ 1]\)