PlotFest: Plotting a Function

\(Off[General::spell]; 
Off[General::spell1]; 
Off[Plot::plnr]; 

    Off[ParametricPlot::ppcom]; 

    If[MemberQ[{"Global`", \ "Miscellaneous`SIUnits`", \ 
          "Graphics`Colors`"}, Context[Gray]], \ Remove["*`Gray"]]; 
    
<< "Miscellaneous`Units`"; 
Remove["*`Gray"]; 
    
<< "Graphics`Colors`"; 
Clear[Derivative]; \)

Axes3D[u,v]

Axes3D::usage = "Axes3D[a, b] creates a Graphics3D object of cartesian axes with x, y, and z running from -a/3 to a, and with axes labels b units beyond the tips of the axes.  Axes3D[a] is Axes3D[a, a/8].";

Axes3D[u_,v_] := 
      Graphics3D[{
            {Blue,Line[{{-u/3,0,0},{u,0,0}}]},
                    Text["x",{u + v,0,0}],
            {Blue,Line[{{0,-u/3,0},{0,u,0}}]},
                    Text["y",{0, u + v,0}],
            {Blue,Line[{{0,0,-u/3},{0,0,u}}]},
                    Text["z",{0,0,u + v}]}]; 
Axes3D[u_] := Axes3D[u,u/8];

CMView  =  {2.7,  1.6,  1.2};

Plotting a function

Here's how you program the function 
        f[x] = 5.0 (E^(-0.1 x)) Sin[ x]
into Mathematica:

Clear[f, x]f[x_] = 5.0  E^(-0.1 x)  Sin[ x]

Here is how you get a table of the values
       {x, f[x]} 
for x = 0, 0.5, 1, 1.5, 2, 2.5, 3,  ... , 9.5, 10:

\(\(
xjump\  = \ 0.5; 
Table[{x, f[x]}, {x, 1, 10, xjump}]\)\)

See these points:

\(dataplot\  = \ Table[Graphics[Point[{x, f[x]}]], {x, 1, 10, xjump}]; 

    Show[dataplot, Axes -> True]; \)

You can make the dots bigger:

dataplot = Table[Graphics[{PointSize[0.03],Point[{x, f[x]}]}],{x, 1, 10, xjump}];
Show[dataplot, Axes -> True];

You can label the x and f[x] axes:

Show[dataplot,Axes -> True,AxesLabel -> {"x", "f[x]"}];

You can put the points in color:

dataplot = Table[Graphics[{Blue,PointSize[0.03],Point[{x, f[x]}]}],{x, 1, 10, xjump}];
colorplot  = Show[dataplot, Axes -> True];

Nice graphics, Mathematica!

But why settle for point plots when you can see the whole curve for 0 ≤ x ≤ 10:

curveplot = 
      Plot[f[x], {x, 0, 10}, 
        PlotStyle -> {{Thickness[0.01], CadmiumOrange}}, 
        AxesLabel -> {"x", "f[x]"}];

See the curve and the data:

Show[curveplot, colorplot];

If you want to spot a certain point on the curve, you can do it.  For instance, here is the curve shown with a data point at {x, f[x]} for x = 5.9:

\(x = 5.9; 

    datapoint = Graphics[{Blue, PointSize[0.03], Point[{x, f[x]}]}]; 

    \(Show[curveplot, datapoint]; \)\)

Here's how to spot the data point {x, f[x]} for x = 2.9:

\(x = 2.9; 

    datapoint = Graphics[{Blue, PointSize[0.03], Point[{x, f[x]}]}]; 

    \(Show[curveplot, datapoint]; \)\)

See {x, f[x]} dance along the curve as x runs from 0 to 10.

Clear[datapoint, x]\(datapoint[x_]\  = \ 
      Graphics[{Blue, PointSize[0.03], Point[{x, f[x]}]}]; 
xjump\  = \ 1; 

    Table[Show[curveplot, datapoint[x], PlotLabel -> x\ "= x"], {x, 0.5, 
        9.5, xjump}]; \)

To see a movie of {x, f[x]} dancing along the curve,
-> select all the plots immediately above and then 
-> go to the Cell menu and select :  Animate Selected Graphics.

Plotting several functions

You probably know that y = (Sqrt[1 - (x^2)]) plots out to be the top of a circle with radius 1 and center at {0, 0}in the xy-plane:

\(Clear[f, x]; 
f[x_] = \@\(1 - x\^2\); 

    Plot[f[x], {x, \(-1\), 1}, AspectRatio -> Automatic]; \)

If you want the whole circle, you just plot the top y = (Sqrt[1 - (x^2)]) and the bottom: y = -(Sqrt[1 - (x^2)]) together.

Plot[{f[x], -f[x]},{x, -1, 1},AspectRatio -> Automatic];

You can squash this circle into an ellipse by setting the AspectRatio, the shape of the window and x and y axis scales.

Plot[{f[x], -f[x]},{x, -1, 1},AspectRatio -> 1/2];

That's a circle but it looks like an ellipse because the setting AspectRatio->1/2 made units on the horizontal axis twice as long as units on on the vertical axis.
You can color your plots too:

circleplot=Plot[{f[x], -f[x]},{x, -1, 1},AspectRatio -> Automatic,PlotStyle 
                -> {{Blue, Thickness[0.008]}, {Brown, Thickness[0.008]}}
                  ];

Here's a parabola:

\(Clear[g, x]; 
g[x_] = 0.5 - 3  x\^2; 

    paraplot = 
      Plot[g[x], {x, \(-1\), 1}, 
        PlotStyle -> {{HotPink, Thickness[0.008]}}]; \)

See the parabola plot and the circle plot together:

Show[circleplot, paraplot];

That's OK, but it might be nicer if you only had to look at the top part.  
Set the PlotRange.

Show[circleplot,paraplot,PlotRange -> {-1, 1}];

You can set the PlotRange to look very closely at the place on the right where the parabola crosses the circle on the right:

Show[circleplot,paraplot,PlotRange -> {{0.6, 0.7}, {-0.8, -0.7}}];

What more could you ask?

One more function plot

Pictures aren't always as pretty as we'd like them to be.

Plot[25 Sin[x], {x, 0, 2  π},  AspectRatio -> Automatic];

It's hard to see what's up on this one.  That picture window is awfully tall and narrow.

Plot[25 Sin[x], {x, 0, 2  π},  AspectRatio -> 1/GoldenRatio];

Much better.  Now dress it up.

\(\(Plot[25\ Sin[x], {x, 0, 2  π}, \ 
      PlotStyle -> {{Thickness[0.008], Red}}, Background -> PaleGreen, 
	
      AspectRatio -> 1/GoldenRatio]; \)\)

Not bad.

Build a house

You can build up objects from sticks and stones in Mathematica.  
Start with a grid.

grid = 
      Plot[0, {x, 0, 6}, PlotRange -> {{0, 6}, {0, 6}}, 
        GridLines -> Automatic];

You aren't bound by gravity, so put on a roof first.

\(roof = 
      Graphics[{DarkGreen, \ Thickness[0.02], 
          Line[{{0, 3}, {3, 6}, {6, 3}}]}]; 
Show[grid, roof]; \)

Put a wall on the left, on the right, and a door.

\(walls = 
      Graphics[{DeepCadmiumRed, Thickness[0.01], Line[{{1, 0}, {1, 4}}], 
          Line[{{5, 0}, {5, 4}}]}]; 

    door = Graphics[{DeepCadmiumRed, Thickness[0.008], 
          Line[{{2, 0}, {2, 2.5}, {3, 2.5}, {3, 0}}]}]; 

    floor = Graphics[{Black, Thickness[0.008], Line[{{1, 0}, {5, 0}}]}]; 

    Show[grid, walls, roof, door, floor]; \)

Nail on some windows:

\(windows = 
      Graphics[{Brown, Thickness[0.01], 
          Line[{{3.5, 1}, {3.5, 1.8}, {4.5, 1.8}, {4.5, 1}, {3.5, 1}}], 
          
Line[{{1.5, 3.2}, {4.5, 3.2}, {4.5, 4}, {1.5, 
                4}, {1.5, 3.2}}
], Circle[{3, 5}, .25]}]; 

    Show[grid, walls, roof, door, floor, windows]; \)

Take off the grid:

Show[walls, roof, door, floor, windows];

There you go.