Graphics primitives in Mathematica

The code to construct a slope field that is given below uses some Mathematica graphics primitives. Here's an example of some (essentially random) graphics primitives displayed using Show[].

Show[
Graphics[{
AbsolutePointSize[
            5], 
{RGBColor[1, 0, 0], 
            Point[{1, 2}]}, 
Line[{{1, 2}, {7, -6}, {3, 
                4}}], 
Circle[{0, 0}, 
            3]
}], 
AspectRatio -> 
        Automatic, 
Frame -> 
        True
]

Example 1

The next set of inputs generates a slope field for the differential equation (dy/dt)=f(t, , y). The variables tmin, tmax, ymin, and ymax do the obvious thing.  The variable n controls the density of tangent line segments.  The variable tsl controls the length of tangent line segments.

Clear[f, t, y];
f[t_, y_] = y;
tmin = 0;
tmax = 5;
ymin = 0;
ymax = 5;
n = 20;
tsl = 0.07;
tanseg[t_, 
          y_] = 
Line[{
{t, y} - 
              tsl*{1, f[t, y]}/Sqrt[1 + f[t, y]^2], {t, y} + 
              tsl*{1, f[t, y]}/
                  Sqrt[1 + 
                      f[t, y]^2]
}
];
slopefield = 
Show[
        Graphics[
Table[
            tanseg[t, y], {t, tmin, 
              tmax, (tmax - tmin)/n}, 
{y, ymin, 
              ymax, (ymax - ymin)/n}]], 
Frame -> 
          True, 
AspectRatio -> Automatic]

We can generate an approximate solution (for a specific initial condition) and the produce a plot.

f[t_, y_] :=  y;
t0 = 0. ;
y0 = 0.1;
tf = 5;
n = 100;
h = (tf - t0)/n;
tylist = {{t0, y0}};
Do[
tylist = 
          Append[tylist, {t + h, 
              tylist[[-1, 2]] + 
                h*f[t, tylist[[-1, 2]]]}], {t, t0, tf - h, 
          h}];
approxsolnplot = 
      ListPlot[tylist, PlotRange -> All, 
        PlotStyle -> RGBColor[1, 0, 0]]

Here's the approximate solution with the slope field.

Show[slopefield, approxsolnplot, 
      PlotRange -> {{0, 5}, {0, 5}}]

Example 2

This example shows how plotting an approximate solution on a slope field can help you detect "numerical garbage".

Clear[f, t, y];
f[t_, y_] = 10  y (1 - y);
tmin = 0;
tmax = 2;
ymin = 0;
ymax = 2;
n = 20;
tsl = 0.03;
tanseg[t_, 
          y_] = 
Line[{
{t, y} - 
              tsl*{1, f[t, y]}/Sqrt[1 + f[t, y]^2], {t, y} + 
              tsl*{1, f[t, y]}/
                  Sqrt[1 + 
                      f[t, y]^2]
}
];
slopefield = 
Show[
        Graphics[
Table[
            tanseg[t, y], {t, tmin, 
              tmax, (tmax - tmin)/n}, 
{y, ymin, 
              ymax, (ymax - ymin)/n}]], 
Frame -> 
          True, 
AspectRatio -> Automatic]

We can generate an approximate solution (for a specific initial condition) and the produce a plot.

f[t_, y_] :=  10  y (1 - y);
t0 = 0. ;
y0 = 0.5;
tf = 2;
n = 8;
h = (tf - t0)/n;
tylist = {{t0, y0}};
Do[
tylist = 
          Append[tylist, {t + h, 
              tylist[[-1, 2]] + 
                h*f[t, tylist[[-1, 2]]]}], {t, t0, tf - h, 
          h}];
approxsolnplot = 
ListPlot[
        tylist, 
PlotJoined -> 
          True, 
PlotRange -> 
          All, 
PlotStyle -> 
          RGBColor[1, 0, 0]]

Here's the approximate solution with the slope field.

Show[slopefield, approxsolnplot, 
      PlotRange -> {{0, 2}, {0, 2}}]