Preface


This is a tutorial made solely for the purpose of education and it was designed for students taking Applied Math 0330. It is primarily for students who have very little experience or have never used Mathematica before and would like to learn more of the basics for this computer algebra system. As a friendly reminder, don't forget to clear variables in use and/or the kernel.

Finally, the commands in this tutorial are all written in bold black font, while Mathematica output is in normal font. This means that you can copy and paste all commands into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts for your needs to learn the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.

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

Figures with Arrows


When one wants to plot a figure that is built from straight lines, it can be done as follows

ListLinePlot[{3, 4, 1, -2, 0, 3, 4, 1, 2}, PlotStyle->Thick]

A directed graph can be plotted as well

Graph[{1 -> 3, 1 -> 2, 2 -> 4, 4 -> 5, 5 -> 1}]

 

 

With[{q = Pi/6},
Graphics[{Circle[{0, 0}, 1, {q, 2 Pi - q}],
Arrowheads[{{.05, .8}}],
Arrow[{{Cos[q] + 2, Sin[q]}, {Cos[q], Sin[q]}}],
Arrow[{{Cos[q], Sin[-q]}, {Cos[q] + 2, Sin[-q]}}],
FontSize -> Medium, Text["\[ScriptCapitalC]", {2, Sin[q]}, {0, -2}]},
Axes -> True, PlotRange -> {{-4, 6}, {-2, 2}}]]

If you want the actual function for contour, then maybe something like the following:

contour[t_, t0_: (5 Pi/6)] := Piecewise[{ {Exp[I (t + Pi)], -t0 < t < t0},
{t - t0 + Exp[I (t0 + Pi)], t >= t0}, {-t - t0 + Exp[-I (t0 + Pi)], t <= -t0}}]
ParametricPlot[Through[{Re, Im}[contour[t]]], {t, -8, 8}, PlotPoints -> 30]

Another option:

c = 0.5;
t0 = ArcSin[c];
PolarPlot[If[Abs[t] < t0, Abs[Sin[t0]/Sin[t]], 1], {t, -\[Pi], \[Pi]},
Epilog -> { Arrow[{{2, c}, {1, c}}], Arrow[{{1, -c}, {2, -c}}], Arrow[{{-1, .1}, {-1, -.1}}],
Text["C", {1.5, c + .1}], Text["C", {1.5, -(c + .1)}] }]



Now we show how to add arrows into the graph.

g1=Graphics[Line[{{0,0},{20,0}}]]
g2=Graphics[Line[{{0,0},{15,15}}]]
g3=Graphics[{Opacity[0.2],Brown,Rotate[Rectangle[{8,8},{12,12}], 45 Degree, {Left, Bottom}]}]
g4=Graphics[{Blue, Thickness[0.01], Arrow[{{8,10.8}, {8,2.8}}]}]
g5=Graphics[{Cyan, Thickness[0.01], Arrow[{{8,10.8}, {3.8,15}}]}]
g6=Graphics[{Green, Thickness[0.01], Arrow[{{2.4,5.2}, {6.6,9.4}}]}]
g7 = Graphics[ Text[StyleForm["Weight", FontSize -> 14, FontWeight -> "Bold", FontColor -> Blue], {9.4, 6.8}, {0.4, 1}, {0, -1}]]
g8=Graphics[ Text[StyleForm["Normal", FontSize -> 14, FontWeight -> "Bold", FontColor -> Cyan], {6, 13}, {0, -1}, {1, -1}]]
g9=Graphics[ Text[StyleForm["Friction", FontSize -> 14, FontWeight -> "Bold", FontColor -> Green], {4.5, 7.2}, {0, 1}, {1, 1}]]
Show[g1,g2,g3,g4,g5,g6,g7,g8,g9]
ContourPlot[x^2 + y^2 == 9, {x, -2, 2}, {y, -2, -3.1}, AspectRatio -> 0.5] /.
Line[x__] :> Sequence[Arrowheads[{-.04, .04}], Arrow[x]]

We can add arrows to multiple plots:

rules = {GraphicsComplex[x_, y_] :>
GraphicsComplex[SortBy[x, ArcTan[#[[1]], #[[2]]] + (\[Pi]/2) &],
y /. Line[pts_] :> {Arrowheads[{{-0.05, 1/8}, {-0.05, 5/8}}],
Arrow[pts]}]}
Show @@ Table[
ContourPlot[x^2 + 2*y^2 == r^2, {x, -r, r}, {y, -r, r},
PlotPoints -> 100, ContourShading -> False,
ContourStyle -> {Black, Thick}] /. rules, {r, 5, 1, -1}]

To add uniform arrows to the graph, we use Alexey Popkov's completePlotRange for computing the actual PlotRange of a plot.

completePlotRange[plot : (_Graphics | _Graphics3D | _Graph)] :=
Last@Last@
Reap[Rasterize[
Show[plot, Axes -> True, Frame -> False,
Ticks -> ((Sow[{##}]; Automatic) &),
DisplayFunction -> Identity, ImageSize -> 0],
ImageResolution -> 1]]

Block[{f, f0, df0, ar, plot}, f = (0.9 - x^2)^(-0.5);
f0 = Log[f] /. x -> x0;
df0 = D[Log[f], x] /. x -> x0;
plot = LogPlot[f, {x, 0, 1}, PlotRange -> {{0, 1.3}, {0.66, 20}},
Frame -> {{True, True}, {True, False}}, ImageSize -> 400,
PlotStyle -> {Directive[Black, Thick], Automatic},
BaseStyle -> {FontSize -> 30}, RotateLabel -> False,
AspectRatio -> 1];
ar = If[(AspectRatio /. Options[plot, AspectRatio]) === Automatic,
1, (AspectRatio /. Options[plot, AspectRatio])/
Ratios[Differences /@ completePlotRange@plot][[1, 1]]];
Show[plot, Graphics[{Red, Thick,
Table[Arrow[{{x0, f0}, {x0, f0} + 0.5 {1, df0}/Norm[{1, df0*ar}]}], {x0, 0.1, 0.9, 0.2}]}]]]

 Now we show how to plot figure when arrows are attached to curves/circles.

Show[ParametricPlot[#[[1]]*{Cos[θ], Sin[θ]}, {θ, #[[2]], #[[3]]},
Axes -> False, PlotStyle -> #[[4]]] /.
Line[x_] :> Sequence[Arrowheads[{-0.05, 0.05}], Arrow[x]] & /@
{{1, 0 Degree, 90 Degree, Red}, {1.25, 0 Degree, 270 Degree, Blue},
{1.5, 0 Degree, 180 Degree, Green}}, PlotRange -> All]
A function using a single ParametricPlot with multiple circles with arrows:
ClearAll[arcsWArrows];
arcsWArrows[args1 : {{_, {_, _}} ..},
dir_List: {Directive[GrayLevel[.3],
Arrowheads[{{-0.05, 0}, {0.05, 1}}]]}] :=
ParametricPlot[
Evaluate[#[[1]]*{Cos[Rescale[u, {0, 2 Pi}, Abs@#[[2]]]],
Sin[Rescale[u, {0, 2 Pi}, Abs@#[[2]]]]} & /@ args1], {u, 0,
2 Pi}, PlotStyle -> dir, Axes -> False, PlotRangePadding -> .2,
ImageSize -> 200] /. Line[x_, ___] :> Arrow[x]
Then we use this function:
dsAndAngls = {{1, {0, \[Pi]/
2}}, {1.25, {0, \[Pi]}}, {1.5, {0, (3 \[Pi])/2}}, {2, {\[Pi]/
4, (4 \[Pi])/2}}};
directives = {Directive[Red, Thick,
Arrowheads[{{-0.05, 0}, {0.05, 1}}]],
Directive[Blue, Dashed, Arrowheads[{{-0.05, 0}, {0.05, 1}}]],
Directive[Green, Arrowheads[{{-0.05, 0}, {0.05, 1}}]],
Directive[Orange, Thickness[.02], Arrowheads[{{-0.07, 0}, {0.07, 1}}]]};

Row[{arcsWArrows[rdsAndAngls],
arcsWArrows[rdsAndAngls, {directives[[1]]}],
arcsWArrows[rdsAndAngls, directives],
arcsWArrows[rdsAndAngls, directives[[-1 ;; 2 ;; -1]]]}]
Another option gives Manipulate command:
Manipulate[ pts = N@Table[{Cos[k], Sin[k]}*r + o, {k, α Degree, β Degree, (β Degree - α Degree)/d}];
Show[
Graphics[{Lighter@Pink, AbsoluteThickness@10, Circle[o, r, {α Degree, β Degree}]}],
Graphics[{Arrow[pts, 0]}],
PlotRange -> {{-1.3, 1.3}, {-1.3, 1.3}}, AspectRatio -> 1,
Axes -> True, ImageSize -> 250 ],
{{d, 20, "res."}, 1, 100, Appearance -> "Labeled"},
{{α, 0, "α"}, 0, 360, Appearance -> "Labeled"},
{{β, 250, "β"}, 0, 360, Appearance -> "Labeled"},
{{r, 1, "r"}, 0.01, 2, Appearance -> "Labeled"},
{{o, {0, 0}, "origo"}, {-1, -1}, {1, 1}},
ControlPlacement -> Left ]
Next, we plot a simple curve that is generated by splines and is finished with arrow:
Graphics[Arrow[ BSplineCurve[Table[{Cos[x], Sin[x]}, {x, 0, Pi*3/4, Pi/20}]]]]
Finally, we plot a function within some domain which graph is ended with arrows.
f[x_] := Sin[12 x^2]; xmin = -1; xmax = 1; small = .01;
Plot[f[x], {x, xmin, xmax}, PlotLabel -> y == f[x],
AxesLabel -> {x, y},
Epilog -> {Blue,
Arrow[{{xmin, f[xmin]}, {xmin - small, f[xmin - small]}}],
Arrow[{{xmax, f[xmax]}, {xmax + small, f[xmax + small]}}]}]

 

 

Discontinuous Functions

Direction Fields

Implicit Plot

Parametric Plot

Labeling Figures

Figures with Arrows

Electric circuits

Plotting with Filling

Polar Plots

Some Famous Curves

Cycloids

 

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)