CPSC 215

January (21^st), 2002

C. Jacob / S. Kaufmann

Assignment 1 is Out!

Go to the CPSC 215 Assignments web page and check it out!

Graphs of Functions of One Variable

Terminology: "Graph"

First a word about the terminology: The term graph has a mathematical definition as a set. A computer program can only visualize a finite part of this–possibly infinite–set. Furthermore, we often use axes, headings, etc. to expand on the information. The resulting object will still be called a “graph”. Mathematica can also create graphics which are not graphs. In our terminology a “graph” is a special kind of “graphic”.

Palettes to Help for Graphs

When visualizing mathematical functions and mappings it is important to consider the dimensions of the domain and of the range. This immediately leads to the appropriate Mathematica function. The palette BasicCalculations > Graphics contains templates for the common cases.

Plotting Graphs of Functions with one Variable

We start by plotting graphs of functions \(TraditionalForm
      \`ℝ → ℝ\).

First we create the graph of the function x → sin(x) over a period using Plot.

Plot[Sin[x], {x, 0, 2  Pi}]

Since we are mainly interested in the graphics themselves, we will suppress the output cells using a semicolon.

Plotting Several Functions at Once

We can also plot several functions at once by passing a list.

Plot[{Sin[x], Sin[2 x], Sin[3 x]}, {x, 0, 2 π}];

Defining Functions First

Naturally the function can also first be defined and then plotted.

function1[x_] = Sin[x]/x;

Plot[function1[x], {x, 0, 2 π}] // Timing

The above definition was written using an immediate definition, since this is just a shortcut and the right-hand side of the definition does not need to be evaluated every time it is called up. A delayed definition produces the same result by evaluating the function definition for every point of the plot. For large calculations, the CPU times of these two variants can differ significantly–but either one can be faster (see the exercise “Efficiency”).

function2[x_] := Sin[x]/x

Plot[function2[x], {x, 0, 2 π}] // Timing

Options

We can change the appearance of graphics in various ways using options. The options are set as transformation rules of the form name -> value. The documentation on Plot in the Help Browser lists the specific options of Plot (Compiled etc.). It tells us that all options of Graphics objects can be used as well. In the documentation on Graphics we find a long list of options (from AspectRatio to Ticks) with their default values. Each option again has a documentation of its own.

Let us take a look at some of the most-used options, with which we can edit our graphic. An immediate definition gives it a name for later reference:

Clear[f];

f[x_] := (x^2) Sin[(1/x)]

demoPlot = Plot[f[x], {x, -π, π}];

As we can see, the range in this case is automatically limited by Mathematica. To clearly display the linear behavior of this function at large arguments, we use PlotRange->All. This plots all values.

Plot[f[x], {x, -π, π}, PlotRange -> All];

The option value AspectRatio->Automatic scales both axes identically. We can add a value for ImageSize to get a graphic of a given size.

Show[%, AspectRatio -> Automatic, ImageSize -> 350];

Instead of re-calculating the whole graphic after changing an option, it can also just be re-drawn with the Show command and any changed options.

AxesOrigin moves origin of the coordinate system.

Show[demoPlot, AxesOrigin -> {-1, 0}];

Clear[g];

g[x_] := 2 (((x - 4))^2) + 1

Plot[g[x], {x, 3, 5}];

Plot[g[x], {x, 3, 5}, AxesOrigin -> {0, 0}];

Plot[g[x], {x, 3, 5}, 
        PlotRange -> {{0, 5}, {0, 3}}];

AxesLabel labels the axes using strings that must be placed in quotation marks.

\(\(Show[demoPlot, AxesOrigin -> {\(-1\), 0}, 
        AxesLabel -> {"x", \*""\!\(x\^2\) Sin[x]""}];\)\)

PlotLabel adds a title.

\(\(Show[demoPlot, AxesOrigin -> {\(-1\), 0}, 
        PlotLabel -> \*""Plot of \!\(x\^2\) Sin[x]""];\)\)

More In Depth

Frames

Frame->True creates a frame around the graphic.

Axes->False suppresses the axes.

GridLines->Automatic shows a grid.

Show[demoPlot, 
        GridLines -> {{-0.75, -0.5, -0.25, 0, 0.25, 0.5, 
              0.75}, {-0.1, 0, 0.1}}, Frame -> True];

Table[n, {n, 1, 5}]

Show[demoPlot, 
        GridLines -> {Table[x, {x, -0.75, 0.75, 0.25}], {-0.1, 
              0, 0.1}}, Frame -> True];

FrameLabel creates a label for the axes (the vertical label appears vertically on the printout).

Changing the Character Format

The option TextStyle uses a list of sub-options with which we can change the format of the text.

If we want to change only the title:

StyleForm lets us use a pre-defined notebook style.

Or we can put a formula into the title.

Changing Lines

The option PlotStyle can be used to change the thickness and color of lines and to draw dashed lines. Because this is an option of Plot and not of Graphics, displaying with Show will not work and the graph must be re-calculated. AbsoluteThickness sets the line thickness to the amount of pixels given.

The option value Dashing defines dashed lines. Its argument determines the lengths of the segments. As with line thickness, there are two versions: those whose values are given as a fraction of the width of the graphic (Thickness, Dashing) and those which use an absolute number of pixels (AbsoluteThickness, AbsoluteDashing) .

When several graphs are plotted, the functions are handled cyclically by PlotStyle.

Colors can easily be defined with the function Hue, which represents the color circle at full brightness and saturation when a single argument in the interval [0, 1] is given.

Exercises

Efficiency

The function Timing displays the time used to evaluate an expression. Compare the timings of plots of the following two functions:

Interpret the results.

Plot[timingTest1[x], {x, 0, π}] // Timing

Plot[timingTest2[x], {x, 0, π}] // Timing