Return to computing page for the first course APMA0330
Return to computing page for the second course APMA0340
Return to computing page for the fourth course APMA0360
Return to Mathematica tutorial for the first course APMA0330
Return to Mathematica tutorial for the second course APMA0340
Return to Mathematica tutorial for the fourth course APMA0360
Return to the main page for the course APMA0330
Return to the main page for the course APMA0340
Return to the main page for the course APMA0360
Return to Part I of the course APMA0330

Plotting Solutions of ODEs


When Mathematica is capable to find a solution (in explicit or implicit form) to an initial value problem, it can be plotted as follows. Let us consider the initial value problem

\[ y' + 2x\,y =0, \qquad y(0) =1 . \]
Its solution can be plotted as follows

a = DSolve[{y'[x] == -2 x y[x], y[0] ==1}, y[x],x];
Plot[y[x] /.a, {x,-2,2}, PlotStyle -> Thick]

In this command sequence, I am first defining the differential equation that I want to solve. In this line, I define the equation and the initial condition as well as the independent and dependent variables.
In the second line, I am commanding Mathematica to evaluate the given differential equation and plot its result. I then command Mathematica to solve the equation and plot the given result of the initial value problem for the range listed above. Typing these two commands together allows Mathematica to solve the initial value problem for you and to graph the initial value problem's solution.

      Another option:
a = DSolve[{y'[x] == -2 x y[x], y[0] == 1}, y[x], x];
Trace[DSolve[{y'[x] == -2 x y[x], y[0] == 1}, y[x], x]]
Plot[y[x] /. a, {x, -2, 2}, PlotStyle -> Thick, PlotLabel -> a[[1, 1]]]
       Solution to the IVP.            Mathematica code

If you need to plot a sequence of solutions with different initial conditions, one can use the following script:

myODE = t^2*y'[t] == (y[t])^3 - 2*t*y[t];
IC = {{0.5, 0.7}, {0.5,(*4*).8}, {0.5, 1}};
Do[ansODE[i] = Flatten[DSolve[{myODE, y[IC[[i, 1]]] == IC[[i, 2]]}, y[t], t]]; myplot[i] = Plot[Evaluate[y[t] /. ansODE[i]], {t, 0.02, 5}, GridLines -> All, PlotRange -> All, PlotStyle -> Thick];
Print[myplot[i]];, {i, 1, Length[IC]}]
Out[1]= t^2 Derivative[1][y][t] == -2 t y[t] + y[t]^3

DSolve::bvnul: For some branches of the general solution, the given boundary conditions lead to an empty solution. >>

DSolve::bvnul: For some branches of the general solution, the given boundary conditions lead to an empty solution. >>

DSolve::bvnul: For some branches of the general solution, the given boundary conditions lead to an empty solution. >>

General::stop: Further output of DSolve::bvnul will be suppressed
during this calculation. >>

In the above script, you define the differential equation that you want to solve and the initial conditions (the respective x and y values). Flattening creates a list of the equations. Then you are asking Mathematica to evaluate the different equations according to their different initial conditions.
Finally, the Print command tells Mathematica to plot the graphs and the size that you want the lines on the graphs to be.

When you enter these commands, you may receive a line saying that some of the branches lead to empty solutions, but this just means that DSolve cannot find a unique solution for part of the equation. However, this does not appear to affect the final graph.

To plot a family of solutions to Clairaut's equation, we type:

f1[x_] = x*c + c/Sqrt[c*c + 1]
samples1 = Table[f1[x], {c, -20/3, 8, 1}]
Plot[Evaluate[samples1], {x, -3, 3}, PlotStyle -> {Black}]


In this command, I am plotting a family of solutions to a differential equation. This command is not really any different from a normal plot command. The primary difference is that I have created a list of samples in a table. These samples have the variable c and then I can plug in different values of c to create the values in this table. This means that I am asking Mathematica to plot solutions with different values of the differential constant c. This command will plot a variety of different graphs over the range of c. The command on the second line also yields the different members of this family of equations associated with each value of c.

 

Manipulate[
Plot[y'[x] = -Sqrt[a^2 - x^2]/x, {x, 0, 20},
PlotRange -> {-10, 10}], {a, 0, 20}]
sol = DSolve[{y'[x] == -Sqrt[a^2 - x^2]/x, y[a] == 0}, y, x]
Out[2]= {{y -> Function[{x}, -Sqrt[a^2 - x^2] + a Log[a] - a Log[a^2] -
a Log[x] + a Log[a^2 + a Sqrt[a^2 - x^2]]]}}

Manipulate[
Plot[-Sqrt[a^2 - x^2] + a Log[a] - a Log[a^2] - a Log[x] + a Log[a^2 + a Sqrt[a^2 - x^2]], {x, 0, 20}, PlotRange -> All], {a, 1, 20}]

 

Now suppose we want to plot solutions to Riccati differential equation subject to different initial conditions:
\[ y' = x^2 - y^2 , \qquad y(0) = y_0 \in \mathbb{R} . \]
An appropriate command is to use ParametricNDSolve. Since your system becomes stiff around −0.6 (it is where separatrix is observed), the use of Quiet avoids the inevitable messages in a couple of places.
sol = DSolve[{y'[x] == -Sqrt[a^2 - x^2]/x, y[a] == 0}, y, Clear[x, y];
parSol = Quiet[ ParametricNDSolve[{y'[x] == x^2 - (y[x])^2, y[0] == y0}, y, {x, -2, 10}, {y0}, WorkingPrecision -> 22]];
vals = {1, 2, 0, -.3, -.5, -.6, -1}
This creates the list of functions to plot. There is no need for this to display, but it makes it easier for you to see what has been created.
sol = DSolve[{y'[x] == -Sqrt[a^2 - x^2]/x, y[a] == 0}, y, funs = Quiet[Table[y[k][x] /. parSol, {k, vals}]];
Since we have the start values in a list we may as well use them to create a legend for the plot.
Quiet@Plot[funs, {x, -2, 2}, PlotStyle -> {Thick, Red}, PlotRange -> {-5, 5}, WorkingPrecision -> 8, PlotLegends -> vals] // Framed[#, RoundingRadius -> 8] &

You still can use NDSolve, but for a single eigenvalue:

s1 = NDSolve[{y'[x] == x^2 - (y[x])^2, y[0] == 1}, y, {x, -2, 10}, Method -> {"StiffnessSwitching", "NonstiffTest" -> False}];
p1 = Plot[Evaluate[y[x] /. s1], {x, -2, 10}, PlotStyle -> {Thick, Purple}, PlotRange -> {-5, 5}]

 

Return to Mathematica page
Return to the main page (APMA0330)
Return to the Part 1 (Plotting)
Return to the Part 2 (First Order ODEs)
Return to the Part 3 (Numerical Methods)
Return to the Part 4 (Second and Higher Order ODEs)
Return to the Part 5 (Series and Recurrences)
Return to the Part 6 (Laplace Transform)
Return to the Part 7 (Boundary Value Problems)