# Part I: Plotting

This tutorial contains many Mathematica scripts. You, as the user, are free to use all codes for your needs, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately. I would like to extend my gratitude to all of the students that aided me in developing this tutorial. The coding, testing, and debugging required a concerted effort, and the following students deserve recognition for their input: Emmet and Jesse Golden-Marx (Fall 2011), Pawel Golyski (Fall 2012). Any comments and/or contributions for this tutorial are welcome; you can send your remarks to <Vladimir_Dobrushkin@brown.edu>

# 1.1. Plotting functions

One of the best characteristics of Mathematica is its plotting ability. It is very easy to plot a variety of functions using Mathematica. For a plot, it is necessary to define the independent variable that you are graphing with respect to. Mathematica automatically adjusts the range over which you are graphing the function.

Plot[2*Sin[3*x]-2*Cos[x], {x,0,2Pi}]

In the above code, we use a natural domain for the independent variable to be $$[0,2\pi ] .$$ In general, the domain of the independent variable is usually chosen based on a particular interest; one may try different options before obtaining a desired figure.
Plot[{Cos@x, ArcCos@x}, {x, -Pi, Pi}, PlotStyle -> Thick]

In this command sequence, the independent variable is x and the range is 0 to $$2\pi .$$ For Plot, after entering the function that you wish to graph, you separate the equation and add {independent variable, lower bound, upper bound}.
This is a simple Plot command. In this example, we are just plotting a function using Mathematica default capabilities, but it is possible to specify the range with PlotRange command. The above graph can also be obtained with the following script:

Plot[2*Sin[3*x]-2*Cos[x], {x,0,2Pi}, PlotRange -> Automatic]

To place a text inside a figure, Mathematica has a special command Text[expr, coordinates, offset] that specifies an offset for the block of text relative to the coordinate given. Providing an offset { dx, dy } specifies that the point ( x, y ) should lie at relative coordinates { dx, dy } within the bounding rectangle that encloses the text. We demonstrate it with the following codes:
Plot[Sin[x] - Cos[3*x]/3, {x, 0, 3},
Epilog -> {Text[Style["hello", 25], Scaled[{0.5, 0.5}], #], Red, Point@{.5, .5}},
PlotLabel -> ToString@#] & /@ {{-2.5, 0}, {2.5, 0}, {0, -2}, {0, 2}, {2, 2}, {-3, -2}}
However, when we change dimensions of the graph, the text is displayed differently.
Plot[Sin[x] - Cos[3*x]/3, {x, 0, 3},
Epilog -> {Text[Style["hello", 25], Scaled[{0.5, 0.5}], #], Red, Point@{.5, .5}},
PlotLabel -> ToString@#] & /@ {{-2.5, 0}, {2.5, 0}, {0, -2}, {0, 2}, {2, 2}, {-3, -2}}
The following table of graphs can be displayed using GraphicsGrid command. GraphicsGrid by default puts a narrow border around each of the plots in the array it gives. You can change the size of this border by setting the option Spacings -> { h, v} . The parameters h and v give the horizontal and vertical spacings to be used. The Spacings option uses the width and height of characters in the default font to scale the h and v parameters by default, but it is generally more useful in graphics to use Scaled coordinates. Scaled scales widths and heights so that a value of 1 represents the width and height of one element of the grid. How to add text to a graph, see
To add test outside the picture, see
The general reference is

On most computer systems, Mathematica can produce not only graphics but also sound. Mathematica treats graphics and sound in a closely analogous way, using command Play. For instance, the previous function can be used to play, on a suitable computer system, a pure tone with a frequency of 440/2π hertz for one second.

Play[2*Sin[3*x*440] - 2*Cos[x*440], {x, 0, 1}]

For multiple plots, use either command Show or you can use {} with commas. Show can be used to change the options of an existing graphic or to combine multiple graphics.

Plot[{2 Sin[3 x] - 2 Cos[x], Sin[x] - 1/3 Cos[3 x]}, {x, 0, 3}, PlotStyle -> Thick]
Show command can be used to adjust the Background option of an existing graphic
g1 = Plot[Sin[x] - 1/3 Cos[3 x], {x, -1, 3}, PlotStyle -> Thick]
Show[{g1, Graphics[Circle[]]}, Background -> Yellow, AspectRatio -> Automatic]

A graphic of a function can be made discrete:

Graphics[{Blue,
Point[Table[{x, Sin[x] - 1/3 Cos[3 x]}, {x, 0, 6, .2}]]}, Axes -> True]
We can shift the origin to another point, as the following example shows.
g1[x_] = Sin[x] - 1/3 Cos[3 x]
g1[0]
Out[3]= -1/3
bp = Plot[g1[x], {x, 0, 6}]
Show[bp, AxesOrigin -> {0, -1/3}, AxesLabel -> {"x", "y"}]

When you need to restrict the vertical range, use PlotRange command as the following example shows.

Plot[(x - 1)*(x - 2)*(x - 3)*Exp[x], {x, -5, 3.5}, PlotStyle -> {Black, Thick},
AxesLabel -> {x, (x - 1)*(x - 2)*(x - 3)*Exp[x]}]
Plot[(x - 1)*(x - 2)*(x - 3)*Exp[x], {x, -5, 3.5}, PlotRange -> {-5, 8},
PlotStyle -> {Black, Thick}, AxesLabel -> {x, (x - 1)*(x - 2)*(x - 3)*Exp[x]}]

Plot sine function downward:

data1 = Table[{x, Sin[x]}, {x, -5, 5, 0.1}];
ListLinePlot[data1, PlotRange -> All];
ticks = Table[{-x, x}, {x, -5, 5, .2}];
ListLinePlot[{#, -#2} & @@@ data1, PlotRange -> {All, 1}, Ticks -> {All, ticks}, Axes -> True, PlotStyle -> Thick]
data1 = Table[{x, Sin[x]}, {x, -5, 5, 0.1}];
ListLinePlot[data1, PlotRange -> All];
ticks = Table[{-x, x}, {x, -5, 5, .2}];
ListLinePlot[{#, -#2} & @@@ data1, PlotRange -> {-1.3, 1.3},
Ticks -> {All, ticks}, Frame -> False, PlotRange -> All,
Epilog -> {Text["x", {5.5, 0}], Text["y", {0, -1.4}]},
PlotRangeClipping -> False, ImagePadding -> {{20, 20}, {20, 20}}]

Now plot with arrows, but without units:

axes[x_, y_, f_, a_] :=
Arrow[{{0, 0}, #}] & /@ {{x, 0}, {0, y}}, {Text[
Style["x", FontSize -> Scaled[f]], {0.95*x, 0.1*y}],
Text[Style["y", FontSize -> Scaled[f]], {0.1 x, 1*y}]}]]

data1 = Table[{x, Sin[x]}, {x, 0, 5, 0.1}];
ListLinePlot[data1, PlotRange -> All];
ticks = Table[{-x, x}, {x, -5, 5, .2}];
Show[ListLinePlot[{#, -#2} & @@@ data1, PlotRange -> {-1.3, 0},
Ticks -> {None, None}, Frame -> False, PlotRange -> All,
PlotRangeClipping -> False, ImagePadding -> {{20, 20}, {20, 20}}],
axes[5.3, -1.33, .06, .05], Axes -> False]

Example. Tractrix (from the Latin verb "trahere" -- pull, drag; plural: tractrices) is the curve along which an object moves, under the influence of friction, when pulled on a horizontal plane by a line segment attached to a tractor (pulling) point that moves at a right angle to the initial line between the object and the puller at an infinitesimal speed. By associating the object with a dog, the string with a leash, and the pull along a horizontal line with the dog's master, the curve has the descriptive name hundkurve (dog curve) in German. It is therefore a curve of pursuit. It was first introduced by Claude Perrault (1613 -- 1688) in 1670. Trained as a physician, Claude was invited in 1666 to become a founding member of the French Academie des Sciences, where he earned a reputation as an anatomist. The first known solution was given by Christian Huygens (1692), who also named the curve the tractrix. Its parametric equation is

$x = a\, \sin (t), \qquad y = a\left( \cos (t) + \ln \left(\tan (t/2)\right) \right) .$

To plot tractrix curve, we use the following code:
tractrix[a_][t_] := a*{Sin[t], Cos[t] + Log[Tan[t/2]]};
Manipulate[
ParametricPlot[tractrix[a][t] // Evaluate, {t, 0, .99*\[Pi]},
PlotRange -> {0, 7}], {a, 1, 6}]
Export["tractrix1.gif",%]
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}]