Solving differential equations with Mathematica

Execute the following command (as before, place the cursor in the cell below and press ⇧+⌤):

Off[General::"spell1"];
Off[General::"spell"];

In the examples below you will need to change only the blue formulas to solve different differential equations.

Solve the equation:   y' = y

Below we assume that the differential equation is in the form y' = RHS.  The first line below defines the right hand side (RHS) of the differential equation that we are solving.  Notice that here we have to write the variable t.  To solve another differential equation change the blue formula below.

RHS=y[t];
sol =  
      y[t] /. DSolve[y'[t] == RHS, y[t], t][[1, 
            1]]

This is the general solution of the differential equation. In the next cell we substitute specific values for the constand C[1].

sols = 
      sol /. {C[1] ->  {-5, -4, -3, -2, -1, 0, 1, 
              2, 3, 4, 5}}

tmin=-3; tmax=3; ymin=-3; ymax=3;
ps = 
        Plot[Evaluate[sols], {t, tmin, tmax}, PlotRange -> {ymin, ymax}, 
          PlotStyle -> {{Blue,  Thickness[0.0075]}}];

Solve the equation:   y' =  t

As before we assume that the differential equation is in the form y' = RHS.  The first line below defines the right hand side (RHS) of the differential equation that we are solving.  Notice that here we have to write the variable t.  To solve another differential equation change the blue formula below.

RHS=t;
sol =  
      y[t] /. DSolve[y'[t] == RHS, y[t], t][[1, 
            1]]

This is the general solution of the differential equation. In the next cell we substitute specific values for the constand C[1].

sols = 
      sol /. {C[1] ->  {-5, -4, -3, -2, -1, 0, 1, 
              2, 3, 4, 5}}

tmin=-3; tmax=3; ymin=-3; ymax=3;

ps = 
        Plot[Evaluate[sols], {t, tmin, tmax}, PlotRange -> {ymin, ymax}, 
          PlotStyle -> {{Blue,  Thickness[0.0075]}}];

Solve the equation:   y' =-Cos[t] (y^2)

As before,  we assume that the differential equation is in the form y' = RHS.  The first line below defines the right hand side (RHS) of the differential equation that we are solving.  Notice that here we have to write the variable t.  To solve another differential equation change the blue formula below.

RHS=-Cos[t]*(((y[t])^2));
sol =  
      y[t] /. DSolve[y'[t] == RHS, y[t], t][[1, 
            1]]

This is the general solution of the differential equation. In the next cell we substitute specific values for the constand C[1].

sols = 
      sol /. {C[1] ->  {4, 2, 1.8, 1.6, 1.4, 
              1.2, -1.2, -1.4, -1.6, -1.8, -2, -4}}

tmin=-2 Pi; tmax=2 Pi; ymin=-5; ymax=5;

ps = 
        Plot[Evaluate[sols], {t, tmin, tmax}, PlotRange -> {ymin, ymax}, 
          PlotStyle -> {{Blue,  Thickness[0.0075]}}];

Study the equation:   y' =2  y  + 4 -  t

As always, we assume that the differential equation is in the form y' = RHS.  The first line below defines the right hand side (RHS) of the differential equation that we are solving.  Notice that here we have to write the variable t.  To solve another differential equation change the blue formula below.

RHS=2  y[t] + 4 - t;
sol =  
      y[t] /. DSolve[y'[t] == RHS, y[t], t][[1, 
            1]]

This is the general solution of the differential equation. In the next cell we substitute specific values for the constand C[1].

sols = 
      sol /. {C[1] ->  {-4, -2, - .8, - .4, - .2, 
              0, 0.2, 0.4, 0.8, 2, 4}}

tmin=-2; tmax=2 ; ymin=-5; ymax=3;
ps = 
        Plot[Evaluate[sols], {t, tmin, tmax}, PlotRange -> {ymin, ymax}, 
          PlotStyle -> {{Blue,  Thickness[0.0075]}}];

Solutions together with a direction field

<< Graphics`PlotField`

Solve the equation:   y' = y

Below we assume that the differential equation is in the form y' = RHS.  The first line below defines the right hand side (RHS) of the differential equation that we are solving.  Notice that here we have to write the variable t.  To solve another differential equation change the blue formula below.

RHS=y[t];
sol =  
      y[t] /. DSolve[y'[t] == RHS, y[t], t][[1, 
            1]]

This is the general solution of the differential equation. In the next cell we substitute specific values for the constand C[1].

sols = 
      sol /. {C[1] ->  {-5, -4, -3, -2, -1, 0, 1, 
              2, 3, 4, 5}}

tmin=-3; tmax=3; ymin=-3; ymax=3;
ps = 
        Plot[Evaluate[sols], {t, tmin, tmax}, PlotRange -> {ymin, ymax}, 
          PlotStyle -> {{Blue,  Thickness[0.0075]}}];

Clear[y, t];
slope=y;
tmin=-3; tmax=3; ymin=-3; ymax=3; 

sf=PlotVectorField[{1, slope},{t, tmin, tmax},{y, ymin, ymax}, Axes -> True,AxesLabel -> {t, y},HeadLength -> 0,ScaleFactor->.2,ScaleFunction -> (1 &),AspectRatio -> 1,PlotPoints->30,ImageSize->300];

Show[ps, sf]

Solve the equation:   y' =-Cos[t] (y^2)

Below we assume that the differential equation is in the form y' = RHS.  The first line below defines the right hand side (RHS) of the differential equation that we are solving.  Notice that here we have to write the variable t.  To solve another differential equation change the blue formula below.

RHS=-Cos[t]*(((y[t])^2));
sol =  
      y[t] /. DSolve[y'[t] == RHS, y[t], t][[1, 
            1]]

This is the general solution of the differential equation. In the next cell we substitute specific values for the constand C[1].

sols = 
      sol /. {C[1] ->  {4, 2, 1.8, 1.6, 1.4, 
              1.2, -1.2, -1.4, -1.6, -1.8, -2, -4}}

tmin=-2 Pi; tmax=2 Pi; ymin=-5; ymax=5;

ps = 
        Plot[Evaluate[sols], {t, tmin, tmax}, PlotRange -> {ymin, ymax}, 
          PlotStyle -> {{Blue,  Thickness[0.0075]}}];

Clear[y, t];
slope=-Cos[t] (y^2);
tmin=-2 Pi; tmax=2 Pi; ymin=-5; ymax=5;

sf=PlotVectorField[{1, slope},{t, tmin, tmax},{y, ymin, ymax}, Axes -> True,AxesLabel -> {t, y},HeadLength -> 0,ScaleFactor->.2,ScaleFunction -> (1 &),AspectRatio -> 1,PlotPoints->30,ImageSize->300];

Show[ps, sf]