This notebook contains the Mathematica commands from Chapter 7 of Introduction to Scientific Programming by Joseph L. Zachary.

(7.7)  We use "Solve" to find the two times at which a projectile is at ground level.

Solve[v*t*Sin[theta] - (1/2)*g*t^2 == 0, t]

(7.9)  We create a function "horizontal" that returns the horizonal position in meters of a projectile "t" seconds after it is fired with initial speed "v" m/sec and initial angle "theta" radians.  This function ignores what happens when the projectile hits the ground.

horizontal[v_, theta_, t_] := v * t * Cos[theta]

(7.10)  We create a function "vertical" that returns the vertical position in meters of a projectile "t" seconds after it is fired with initial speed "v" m/sec and initial angle "theta" radians.  This function ignores what happens when the projectile hits the ground.

vertical[v_, theta_, t_] := v * t * Sin[theta] - 1/2 * g * t^2

(7.11)  We create a function "duration" that reports the time in seconds that elapses before a projectile fired with initial speed "v" m/sec and an initial angle "theta" radians hits the ground.

duration[v_, theta_] := 2 * v * Sin[theta] / g

(7.12)  The horizontal position of a projectile fired straight up will always be zero.

horizontal[100, Pi/2, 10]

(7.13)  We calculate the horizontal position after 10 seconds of a projectile fired at 45 degrees with a speed of 100 m/sec.

horizontal[100, Pi/4, 10]

(7.14)  Trigonometric functions such as "Cos" give exact answers if their parameters are exact multiples of pi.

Cos[Pi/4]

(7.15)  Even though this result is not completely reduced, it is still exact.

horizontal[100, Pi/8, 10]

(7.16)  We use "N" to convert the result from (7.13) into floating-point form.

N[horizontal[100, Pi/4, 10]]

(7.17)  The symbolic constant "g" appears in this result because "g" is used in the implementation of "vertical" but is nowhere given a value.

N[vertical[100, Pi/4, 10]]

(7.18)  The symbolic constant "g" appears in this result for the same reason that it appeared in the result of (7.17).

vertical[100, Pi/4, 10]

(7.19)  This illustrates in a bit more detail what happens when the call to "vertical" from (7.18) is made.  Mathematica substitutes 100 for "v", Pi/4 for "theta", and 10 for "t" in the body of "vertical", which results in the following expression.  Mathematica then evaluates the expression as if it had been typed in.

100 * 10 * Sin[Pi/4] - 1/2 * g * 10^2

(7.20)  We assign "g" a value of 9.8 m/sec/sec.

g = 9.8

(7.21)  Now when we repeat (7.17) we get a numerical answer.

N[vertical[100, Pi/4, 10]]

(7.22)  This calculates the duration of the flight of a projectile fired at 100 m/sec at an angle of 45 degrees.

N[duration[100, Pi/4]]

(7.23)  We might have chosen to define "vertical" so that acceleration due to gravity was coded directly in, instead of using the free variable "g" as we did in (7.10).

vertical[v_, theta_, t_] := v * t * Sin[theta] - 1/2 * 9.8 * t^2

(7.24)  We might also have chosen to define "vertical" so that acceleration due to gravity was a fourth parameter.

vertical[v_, theta_, t_, g_] := v * t * Sin[theta] - 1/2 * g * t^2

(7.25)  We could then have specified gravitational constants other than 9.8 as a fourth parameter.

N[vertical[100, Pi/4, 10, 4.9]]

(7.26)  We could also have given "g" a default value.

vertical[v_, theta_, t_, g_:9.8] := v * t * Sin[theta] - 1/2 * g * t^2

Let's revert to the original definition of "vertical" from (7.10).

vertical[v_, theta_, t_] := v * t * Sin[theta] - 1/2 * g * t^2

(7.27)  Because "t" is symbolic, the result here is given in terms of "t".

vertical[100, Pi/4, t]

(7.28)  The first parameter to this form of "Plot" must be a symbolic expression in the independent variable.  This variable is identified, and its range specified, by the second parameter.

Plot[vertical[100, Pi/4, t], {t, 0, 20}]

(7.29)  The plot from (7.26) shows the height of the projectile as if it continues falling even after it hits the ground.  Here we specify bounds on "t" that correspond only to the duration of the projectile's flight.

Plot[vertical[100, Pi/4, t],
     {t, 0, 20},
     PlotRange->{0, 260}]

(7.30)  In (7.29), we "guessed at" the appropriate bounds to put on the vertical axis.  Here, we specify the bounds on the horizonal axis exactly, which means that the vertical axis is automatically scaled properly.

Plot[vertical[100, Pi/4, t],
     {t, 0, duration[100, Pi/4]}]

(7.31)  We repeat (7.30), but use the "AxesLabel" option to label the axes.

Plot[vertical[100, Pi/4, t],
     {t, 0, duration[100, Pi/4]},
     AxesLabel->{"sec", "m"}]

(7.32)  This is an example of a list.

{2+2, 2-2, 2*2, 2/2}

(7.33)  The "Table" function is useful for building lists.  Here, we construct a list of the squares of the odd numbers between 1 and 21.

Table[n^2, {n, 1, 21, 2}]

(7.34)    We include three different curves in a single plot by specifying a list of three different dependent expressions as the first parameter to "Plot".  Unfortunately, two of the height curves extend below the ground.

Plot[{vertical[100, Pi/6, t],
      vertical[100, Pi/4, t],
      vertical[100, Pi/3, t]},
     {t, 0, duration[100, Pi/3]},
     AxesLabel->{"sec", "m"}]

(7.35)  Here's an alternative way to create the plot from (7.34).  We create each of the three curves individually and save them to variables.  First, the Pi/6 trajectory ...

plot30 = Plot[vertical[100, Pi/6, t],
              {t, 0, duration[100, Pi/6]}]

(7.36)  ... next, the Pi/4 trajectory ...

plot45 = Plot[vertical[100, Pi/4, t],
              {t, 0, duration[100, Pi/4]}]

(7.37)  ... and last, the Pi/3 trajectory ...

plot60 = Plot[vertical[100, Pi/3, t],
              {t, 0, duration[100, Pi/3]}]

(7.38)  ... Finally, we use "Show" to compute the trajectories on one plot.

Show[{plot30, plot45, plot60},
     AxesLabel->{"sec", "m"}]

(7.39)  On the same plot, we show the horizontal and vertical positions of a projectile fired at 100 m/sec at an angle of 45 degrees.

Plot[{horizontal[100, Pi/4, t],
      vertical[100, Pi/4, t]},
     {t, 0, duration[100, Pi/4]},
     AxesLabel->{"sec", "m"}]

(7.40)  We create a parametric plot using the dependent expressions from (7.39).  We do this by using "ParametricPlot" and listing the two dependent expressions.  The horizontal position is plotted against the horizonal axis, and the vertical position is plotted against the vertical axis.  Time does not appear explicitly in the plot.

ParametricPlot[{horizontal[100, Pi/4, t],
                vertical[100, Pi/4, t]},
               {t, 0, duration[100, Pi/4]},
               AxesLabel->{"m", "m"}]

(7.41)  We put three parametric curves on the same plot.  We create the Pi/6 curve ...

pplot30 =
  ParametricPlot[{horizontal[100, Pi/6, t],
                  vertical[100, Pi/6, t]},
                 {t, 0, duration[100, Pi/6]}]

(7.42)  ... the Pi/4 curve ...

pplot45 =
  ParametricPlot[{horizontal[100, Pi/4, t],
                  vertical[100, Pi/4, t]},
                 {t, 0, duration[100, Pi/4]}]

(7.43)  ... and the Pi/3 curve.

pplot60 =
  ParametricPlot[{horizontal[100, Pi/3, t],
                  vertical[100, Pi/3, t]},
                 {t, 0, duration[100, Pi/3]}]

(7.44)  Finally we use "Show" to combine the three curves.  We use the "AspectRatio" optional parameter to ensure that the scales of the two axes are identical.

Show[{pplot30, pplot45, pplot60},
     AxesLabel->{"m", "m"},
     AspectRatio->Automatic]

(7.45)  We create a sequence of plots showing how the height of a projectile depends on both time and the initial angle of its trajectory.  Each plot shows the height of the projectile against time for a particular initial angle.  The initial angle varies from 0 degrees in the first plot to 90 degrees in the last plot.

To animate the 13 plots, double click on the cell bracket that encloses all of the plots.  A single plot will replace it.  Double click this plot to begin the animation.  You can control the animation with the icons that will appear at the bottom of the notebook.

Table[Plot[vertical[100, angle, t],
           {t, 0, duration[100, Pi/2]},
           PlotRange->{0, 550},
           AxesLabel->{"m", "m"}],
      {angle, 0, Pi/2, Pi/24}]

(7.46)  We create an animation showing how the trajectory of a projectile depends on both time and the the initial angle of its trajectory.  In each frame of the animation, a parametric plot of the projectile's position is shown.  During the animation, the initial angle varies from 0 to 90 degrees.

Table[ParametricPlot[{horizontal[100, angle, t],
                      vertical[100, angle, t]},
                     {t, 0, duration[100, Pi/2]},
                     PlotRange->{{0,1100},{0,550}},
                     AxesLabel->{"m", "m"}],
      {angle, 0, Pi/2, Pi/24}]