Functions, plots and calculus on Mathematica 
D. McClendon
Fall 2013

Defining functions

We have seen that assigning numerical values to variables in Mathematica is straight-forward; for example,

z = 3

assigns the value 3 to the variable z.  Mathematica is also capable of using variable names to represent functions, but the syntax is a bit more complicated.  Suppose we wanted to let f(x) = x + sin x.  To do this, execute the command

f[x_] = x + Sin[x]

Note the underscore after the x.  This tells Mathematica that you are defining f to be a function of the variable x (not a number).  The general syntax to define a function is

functionname[variable_] = formula of the variable that gives the function

Once you have defined a function, there are lots of useful things Mathematica can do with that function.  This notebook describes many of the typical applications:

1.  Evaluate expressions involving the function

Suppose f(x) = (2x^2 - 1)/(x + 2).  Suppose you wanted to know what f(2x) - 3f(x) is.  In Mathematica, you can "just do it":

f[x_] = (2x^2 - 1)/(x+2);
f[2x] - 3f[x]

Simplify[%]

In the input In[3], we told Mathematica to perform two commands (separated by the semicolon).  In the first line, we defined f(x), and in the second line, we asked Mathematica to evaluate f(2x) - 3f(x).  NOTE:  we used square brackets to enclose the inputs to f.  Always use square brackets when asking Mathematica to evaluate a function.  After getting an answer, we used the Simplify command to actually add the two fractions together.

Mathematica can also combine functions in the usual ways.  For example, the following command evaluates the composition g ◦ h, given two functions g and h:

g[x_] = 2x Cos[x];
h[x_] = x^2 - 5x + 1;
g[h[x]]

You also can solve equations using the syntax described on the previous Mathematica homework:

r[x_] = x^3 - 5x^2 + 3x - 3;
Solve[r[x] == -7, x]

r[x_] = x^3 - 5x^2 + 3x - 3;
NSolve[r[x] == -7, x]

2.  Generate tables and graphs for a function

Mathematica generates tables of values for a function with the Table command.  For example, this command spits out the y-values of the function f(x) = 2x - 1, where x goes from 1 to 10 and the value of x is increased by 3 each time.

f[x_] = 2x - 1;
Table[{x, f[x]}, {x, 1, 10, 3}]  //TableForm

This output reflects the fact that f(1) = 1, f(4) = 7, f(7) = 13, f(10) = 19.

To graph a function, here is the basic syntax.  To plot the graph of f(x) = (sin 8x + 2)e^(-x^2), use the following command:

f[x_] = (Sin[8x] + 2) * E^(-x^2);
Plot[f[x], {x, -3, 3}]

It is not necessary to assign the function a name (like "f") to plot it.  The following command plots the graph just as well:

Plot[(Sin[8x] + 2) * E^(-x^2), {x, -3, 3}]

The "{x, -3, 3}" is a required part of the command.  The x tells Mathematica what the input variable is (this is important in case the function had multiple variables in it) and the -3 and 3 tell Mathematica what to make the left and right edges of the picture.  If you don't tell Mathematica anything else, it will choose a corresponding range of y-values based on what range it thinks makes a nice graph.

The Plot command can be modified in a variety of ways:

Plotting multiple graphs on the same axis:  Replace the single function with a list of functions enclosed with braces.

Plot[{x^2, x, Sin[x]}, {x, -4, 4}]

Specifying a range of y-values:  After the {x, xmin, xmax} part of the Plot command, add the phrase PlotRange -> {ymin, ymax}.

Plot[{x^2, Cos[x]}, {x, -6, 6}, 
PlotRange -> {-3, 2}
]

Making the scales on the x-axis and y-axis line up:  Notice that on the above picture, 1 unit on the y-axis is about the same distance on the page as 2 units on the x-axis.  If you want a picture where the x- and y-axis are the same scale on the paper, use the AspectRatio -> Automatic command.

Plot[{x^2, Cos[x]}, {x, -6, 6},
 PlotRange -> {-3, 2}, 
AspectRatio -> Automatic
]

Changing the tick-marks on the axes:  Use the Ticks command.  Note the syntax is Ticks -> {{list of x values to make tick marks}, {list of y values to make tick marks}}.

Plot[{x^2, Cos[x]}, {x, -6, 6}, 
PlotRange -> {-3, 2}, 
AspectRatio -> Automatic, 
Ticks -> {{-4, -3, -2, 3},{-2, 1, 2}}
]

Changing the aesthetic appearance of the graphs:  Use the PlotStyle command.  If you are plotting a single function, the syntax is PlotStyle -> {description of how you want the graph to look}; if you are plotting a list of functions, the syntax is PlotStyle -> {{description of 1st graph}, {description of 2nd graph}}.

Plot[(Sin[8x] + 2) * E^(-x^2), {x, -3, 3}, PlotStyle -> {Blue, Thick}
]

Plot[{(Sin[8x] + 2) * E^(-x^2), x^2, Cos[x]}, {x, -3, 3}, PlotRange -> {-2, 3}, 
AspectRatio -> Automatic, PlotStyle -> {{Blue, Thick}, {Red}, {Green, Thick, Dashing[.05]}}
]

Shading a region between a graph and the x-axis:  Use the Filling and FillingStyle commands.

Plot[{(Sin[8x] + 2) * E^(-x^2), x^2, Cos[x]}, {x, -3, 3}, PlotRange -> {-2, 3}, 
AspectRatio -> Automatic, PlotStyle -> {{Blue, Thick}, {Red}, {Green, Thick, Dashing[.05]}}, Filling -> Axis, 
FillingStyle -> Automatic]

The Filling and FillingStyle commands can be altered to shade regions between graphs, with specified colors and shading styles.  For help on the syntax with these commands, ask Mathematica.

Plot[{(Sin[8x] + 2) * E^(-x^2), x^2, Cos[x]}, {x, -3, 3}, PlotRange -> {-2, 3}, 
AspectRatio -> Automatic, PlotStyle -> {{Blue, Thick}, {Red}, {Green, Thick, Dashing[.05]}}, Filling -> {1 -> {2}}, FillingStyle -> {{Orange, Opacity[.25]}, {Orange,Opacity[.75]}}]

2.  Calculus

Limits

Mathematica will correctly evaluate limits, derivatives and integrals of functions.  There are a variety of ways to type in these commands.  Let's start with a limit.  Suppose we want to evaluate the limit as x approaches 0 of (sin^2 x)/(x^2).  Here are two commands which evaluate this:

Limit[(Sin[x])^2/x^2, x -> 0]

f[x_] = (Sin[x])^2/x^2;
Limit[f[x], x -> 0]

Notice Mathematica gives the correct value of the limit, which is 1.  To get the right arrow, you can type a hyphen "-" followed by the greater than sign ">"; Mathematica will automatically convert this into an arrow.

Should you need to evaluate a limit as x goes to infinity, you can type Infinity (Mathematica knows what that is) or use the ∞ button found on the Basic Math Assistant pallette.

WARNING:  Mathematica sometimes tries to tell you that limits exist when they really don't.  See the lab assignment.

Derivatives

There are a couple of ways to ask Mathematica to find derivatives.  Here's a command that spits out the first derivative of x^3 - 3x:

D[x^3 - 3x, x]

The x is necessary at the end because you need to tell Mathematica which variable you want to differentiate with respect to.  If you want a higher-order derivative of a function, use this syntax:

D[x ^2 Cos[x], {x, 4}]

The above command calculates the fourth derivative of x^2 cos x.  An alternate method to find derivatives is to store the function and use traditional "prime" notation:

f[x_] = x^9 - x^2

f'[x]

f'[4]

f'''''[x]

Integrals

Mathematica evaluates integrals with the Integrate command.  To evaluate a definite integral, the syntax is

Integrate[x^2, {x, 0, 5}]

This calculates the integral (∫_0^5)x^2 dx.  If you want a decimal approximation, use the NIntegrate command:

NIntegrate[x^2, {x, 0, 5}]

For indefinite integrals, simply leave off the range of the integral from the Integrate command:

Integrate[x^2, x]

WARNING:  There is a problem with this answer (there is no + C).

You can actually get an integral sign from the Basic Math Assistant Pallette under "Basic Commands"; by clicking on the integral sign the integral appears in the notebook; you can subsequently fill in the blanks and ask Mathematica to execute the integral.