The Plot[...] Function & Associated Operations:

Mathematica is More Versatile Than a Graphing Calculator

Original Author: Robert Kennedy

Introduction to the Plot[...] Function

The purpose of this slide show is to demonstrate how to use Mathematica’s Plot[...] function in as user-friendly a manner as possible.  As you  read through this slide show you should immediately work your own examples to reinforce your reading.  Also, you should read through the relevant portions of the Documentation Center to improve your ability to read intelligibly this very powerful and effective resource.  Please keep in mind the following as well.

Remeber that all the plot images produced can be copied and inserted in other documents like Word.  When pasting into Word you will need to paste “as special.”

Mathematica allows students and teachers to easily save and return to their work later.

Mathematica is a professional software package that can do just about anything.

Taking the time to learn Mathematica will greatly improve your computing and programming options over the graphing calculator.  I can now produce answers with Mathematica just as quickly, and in many cases faster than I can with the graphing calculator

The Most Basic Plot[...] Function Call

Plot[...] is used to plot somewhat continuous functions in Mathematica.  I say somewhat continuous since points are plotted with ListPlot[...].  Using ListPlot[...] is very similar to using Plot[...], so you should experiment with it after completing this Slide Show.

Consider the following examples, and pay careful attention to the characters used and their order.

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

Notice that the most basic structure for a Mathematica Plot[...] is Plot[ function to be graphed, { function domain (sort of ) } ] .  Officially, if you read the Documentation Center you will see Plot[ f, {x, (Subscript[x, min]), (Subscript[x, max])} ].  I claim that the {...} portion of the Plot[...] call is the “domain” of the function since for continuous functions this is a reasonable interpretation; however, for discontinous functions this label is inappropriate.  Consider

Plot[ Tan[ x ], {x, - Pi, 2 Pi } ]

Mathematica is using the discontinuous values of the specified “domain” in the plot and creating vertical lines as a result of its internal algorithm.  In order to avoid this difficulty we must add the attribute Exclusions and remove each x value that produces a discontinuity.

Plot[ Tan[ x ], {x, - Pi, 2 Pi }, Exclusions->Table[ (2 k + 1 ) Pi /2, {k, -1, 2}]  ]

Dashed vertical lines can be included in the plot using the attribute ExclusionsStyle

Plot[ Tan[ x ], {x, - Pi, 2 Pi }, Exclusions->Table[ (2 k + 1 ) Pi /2, {k, -1, 2}], ExclusionsStyle->Directive[ Dashed, Gray ]  ]

The previous discussion will be discontinued since it is beyond the intended scope of this Slide Show.

Try Your Own Examples or Modify the Givens Below

PlotRange & ImageSize Specify the Viewing Window

It is possible to change the default viewing window by using the attribute PlotRange.  Once you have considered the following examples try reading about PlotRange in the Documentation Center.

Using the Specified “Domain” and Changing the Vertical Viewing Range

Plot[ Sin[ x ], {x, - Pi, 2 Pi }, PlotRange->{ - 2, 3 }  ]

Specifying an Arbitrary Viewing Window

Plot[ Sin[ x ], {x, - Pi, 2 Pi }, PlotRange->{ { - 2 Pi, 3 Pi }, { - 2, 3 } } ]

It is possible to change the image size with the attribute ImageSize.  Refer to the Documentation Center to specify an arbitrary image size.

Plot[ Sin[ x ], {x, - Pi, 2 Pi }, PlotRange->{ { - 2 Pi, 3 Pi }, { - 2, 3 } }, ImageSize->Large ]

Try Your Own Examples

Ticks Specifies the Partition of the x- and y-axes

Consider...

Plot[ Sin[ x ], {x, - Pi, 2 Pi }, PlotRange->{ { - 2 Pi, 3 Pi }, { - 2, 3 } }, Ticks->{ { - Pi,- Pi / 2,  0, Pi, 2 Pi}, { - 1, 1, 2.5 } } ]

Try Your Own Examples

PlotStyle controls Curve Color and Other Characteristics

Plot[ Sin[ x ], {x, - Pi, 2 Pi }, PlotRange->{ { - 2 Pi, 3 Pi }, { - 2, 3 } }, Ticks->{ { - Pi,-Pi / 2,  0, Pi, 2 Pi}, { - 1, 1, 2.5 } }, PlotStyle->Red   ]

When trying to modify more than one style element for a particular function a Directive[...] must be used.  The Directive[...] applies multiple styles to one element.  Review the following two examples carefully.

Plot[ Sin[ x ], {x, - Pi, 2 Pi }, PlotRange->{ { - 2 Pi, 3 Pi }, { - 2, 3 } }, Ticks->{ { - Pi,-Pi/2,  0, Pi, 2 Pi}, { - 1, 1, 2.5 } }, PlotStyle->Directive[ Red, Dashed]   ]

In the following example, a Directive[...] is used to modify 3 PlotStyle elements.

Plot[ Sin[ x ], {x, - Pi, 2 Pi }, PlotRange->{ { - 2 Pi, 3 Pi }, { - 2, 3 } }, Ticks->{ { - Pi,-Pi/2,  0, Pi, 2 Pi}, { - 1, 1, 2.5 } }, PlotStyle->Directive[ Red, Dashed, Thick ]   ]

PlotLegends & AxesLabel Add Important Details

Plot[ Sin[ x ], {x, - Pi, 2 Pi }, PlotRange->{ { - 2 Pi, 3 Pi }, { - 2, 3 } }, Ticks->{ { - Pi,-Pi/2,  0, Pi, 2 Pi}, { - 1, 1, 2.5 } }, PlotStyle->Directive[ Red, Dashed, Thick ], PlotLegends->"Expressions"   ]

Plot[ Sin[ x ], {x, - Pi, 2 Pi }, PlotRange->{ { - 2 Pi, 3 Pi }, { - 2, 3 } }, Ticks->{ { - Pi,-Pi/2,  0, Pi, 2 Pi}, { - 1, 1, 2.5 } }, PlotStyle->Directive[ Red, Dashed, Thick ],  PlotLegends->"Expressions", AxesLabel->{ "x-axis", "y-axis" }   ]

Manipulate[...]: Making a Plot Dynamic

Any plot can be made dynamic by wrapping the Plot[...] call with a Manipulate[...] function and adding the appropriate variables.  Read the following two examples carefully.  Then search the Documentation Center for “manipulate” and read the entry.

Manipulate[

Plot[ Sin[f x ], {x, - Pi, 2 Pi }, PlotRange->{ { - 2 Pi, 3 Pi }, { - 2, 3 } }, Ticks->{ { - Pi,-Pi/2,  0, Pi, 2 Pi}, { - 1, 1, 2.5 } }, PlotStyle->Directive[ Red, Dashed, Thick ],  PlotLegends->"Expressions", AxesLabel->{ "x-axis", "y-axis" }   ],

{f, 0, 6 }
]

Manipulate[

Plot[ A Sin[f x ], {x, - Pi, 2 Pi }, PlotRange->{ { - 2 Pi, 3 Pi }, { - 2, 3 } }, Ticks->{ { - Pi,-Pi/2,  0, Pi, 2 Pi}, { - 1, 1, 2.5 } }, PlotStyle->Directive[ Red, Dashed, Thick ],  PlotLegends->"Expressions", AxesLabel->{ "x-axis", "y-axis" }   ],

{f, 0, 6 }, {A, 0, 1.5 }
]

Plotting Multiple Curves with a Common “Domain” on a Common Set of Axes

Plotting two curves on the same axes is done in the following way.  Notice the addition of the {...} around the list of functions to be graphed.

Plot[{ Sin[ x ], Exp[ x ], x^2 - x }, {x, - Pi, Pi } ]

The Tooltip[...] can be added to Plot[...] so that sliding the cursor over a curve will reveal its type.

Plot[ Tooltip[{ Sin[ x ], Exp[ x ], x^2 - x }], {x, - Pi, Pi } ]

Now Try to Apply Some of the Attributes Discussed to the above Plot.  Consult the Documentation Center if Necessary.

Show[...]: Plotting Multiple Curves with Different “Domains” on a Common Set of Axes

Show[
Plot[ Sin[ x ], {x, - Pi, Pi } ],
Plot[ E^x, {x, - 1, 1 } ],
Plot[ x^2 - x, {x, - 2, 2 } ]
]

Show[
Plot[ Sin[ x ], {x, - Pi, Pi } ],
Plot[ E^x, {x, - 1, 1 } ],
Plot[ x^2 - x, {x, - 2, 2 } ],
PlotRange->{ { - 3 Pi / 2, 3 Pi / 2 }, { - 1, 5 } }, Ticks->{ { - Pi,-Pi/2,  0, Pi / 2, Pi, 2 Pi}, { - 1, 1, 2, 3, 4, 5 } }, AxesLabel-> { "x-axis", "y-axis" }
]

Show[
Plot[ Sin[ x ], {x, - Pi, Pi }, PlotLegends->"Expressions" ],
Plot[ E^x, {x, - 1, 1 }, PlotStyle->Red, PlotLegends->"Expressions" ],
Plot[ x^2 - x, {x, - 2, 2 }, PlotStyle->Black, PlotLegends->"Expressions" ],
PlotRange->{ { - 3 Pi / 2, 3 Pi / 2 }, { - 1, 5 } }, Ticks->{ { - Pi,-Pi/2,  0, Pi / 2, Pi, 2 Pi}, { - 1, 1, 2, 3, 4, 5 } }, AxesLabel-> { "x-axis", "y-axis" }
]

FindRoot[...] & Solve[...]: Finding the x-value or y-value of an intersection of two curves

It is easy to find the intersection points of curves using FindRoot[...] or Solve[...].  FindRoot[...] is used for transcedental functions and Solve[...] is used for algebraic functions.

Plot[{ Cos[ x ], x^2 }, { x, 0, Pi / 2 }  ]

Since cos(x) is a transcendental function FindRoot[...] must be used.  FindRoot[...] uses a recurrsive algorithm like the Newton-Raphson.  Consequently, an initial value near the root must be supplied.

FindRoot[ Cos[ x ]==x^2, { x, 0.7 } ]

Equivalently,

FindRoot[ Cos[ x ] - x^2== 0, { x, 0.7 } ]

FindRoot[...] will give the intersection ( x, y ) using the following input.

FindRoot[{ y == Cos[ x ], y==x^2 }, { { x, 0.7 }, { y, 0.6 } } ]

Using algebraic equations it is possible to find the intersection using Solve[...] or FindRoot[...]

Plot[{2 + x - x^3, x^2 }, { x, 0, 2 }]

Using Solve[...] will give all complex solutions as well.

Solve[ 2 + x - x^3 == x^2, x ]

Decimal approximations can be obtained by

N[ Solve[ 2 + x - x^3 == x^2, x ], 8 ]

Or by the shortcut // N

Solve[ 2 + x - x^3 == x^2, x ]//N

Equivalently,

Solve[ 2 + x - x^3 - x^2 == 0, x ]

Solve[ 2 + x - x^3 - x^2 == 0, x ]//N

The (x, y) can be obtained using Solve[...]

Solve[{ y==2 + x - x^3,  y==x^2 }, { x, y } ]

Solve[{ y==2 + x - x^3,  y==x^2 }, { x, y } ]//N

FindRoot[...] can be used with algebraic functions but initial guesses must be supplied

FindRoot[ 2 + x - x^3 == x^2, { x, 1.2 } ]

FindRoot[{ y==2 + x - x^3, y == x^2 }, { { x, 1.2 }, { y, 1 } } ]

Finally, please note that syntactically the above answer is equivalent to

{ {{ x, 1.20557 }}, {{ y, 1.4534 }} }

This is important to know when attempting to use the Part[...] function or [[...]] as a shortcut.  For example if the y root is to be selected

{ {{ x, 1.20557 }}, {{ y, 1.4534 }} }[[ 2, 1, 2 ]]

Or

Part[ 
Part[ 
Part[{ {{ x, 1.20557 }}, {{ y, 1.4534 }} }, 2 ],
1],
2]

Try your own examples

ReplaceAll[...]: Evaluating Functions Using Values Computed from FindRoot[...] and Solve[...]

ReplaceAll[...] replaces all instances of a particular variable in one function with some value from a list.  The shortcut command is /.

ReplaceAll[ 2 + x - x^3, { x-> 1} ]

2 + x - x^3 /. {x-> 1}

Since FindRoot[...] and Solve[...] give the answers in list form

ReplaceAll[ 2 + x - x^3, FindRoot[ 2 + x - x^3 - x^2 == 0, { x, 1.2 } ] ]

2 + x - x^3 /. FindRoot[ 2 + x - x^3 - x^2 == 0, { x, 1.2 } ]

Suppose you would like to use the Real y-value solution from the following intersection in 1+y^3

Solve[{ y==2 + x - x^3,  y==x^2 }, { x, y } ]//N

Select the first list from the list of lists [[1]] (is the shortcut command), and then select the second element of the first list [[2]]

ReplaceAll[  1 + y^3,  Solve[{ y==2 + x - x^3,  y==x^2 }, { x, y } ][[1]][[2]] ]//N

The most compact code

( 1 + y^3 )/.Solve[{ y==2 + x - x^3,  y==x^2 }, { x, y } ][[1]][[2]] //N

The long form is

N[
ReplaceAll[ 1 + y^3,
Part[
Part[ Solve[{ y==2 + x - x^3,  y==x^2 }, { x, y } ], 1 ], 
2]
],
8 ]

Open the Documentation Center and refer to it as you create your own examples.

Try your own examples