Mathematica Lab 1:  Introduction

MST 306, Spring 2019
Dr. John Gemmer
Wake Forest University

About Mathematica:  Mathematica \[MathematicaIcon] is a powerful software that can be used in many disciplines.  We will use it extensively in our course to help visualize concepts.  For those of you who used calculators to study calculus in high school, Mathematica can do all of those tasks, and much more -- professional-quality, manipulable graphics; programming & algorithms; encyclopedic data lookup, to mention a few.

Mathematica Labs for Math 306

This semester, you will complete Mathematica labs for MST 306.  Each one will feature a number of Tasks for you to complete. 

Below, I’ve “closed” the sections & subsections, for improved readability.  To open them, right-click on the dividers at the right, and click on “open/close group”.

Grading Labs by Specifications Grading

Labs will be graded using Specifications Grading -- your lab be graded out of five points. I will be assesing your work on the following:

Correct mathematics.

Proper labeling of figures.

Appropriate amount of writing.

Correct grammar.

Lab Submission Requirements

Formatting

Each lab must have a filename given by your lastname, your first initial, -Lab#.nb  For example, if I were submitting this week’s lab, I would save it as GemmerJ-Lab1.nb -- Mathematica uses the .nb extension, meaning “notebook”.

Each lab must have a title of “Mathematica Lab 1, using Mathematica formatting.

Each lab must contain a subtitle, with your name.  (You may include other info in the title or subtitle if you like.)

Each task should be listed as a separate chapter.

If you are assigned exercises from the book, each should be its own section.

Labs are due on Fridays by the start of class, 9:00 am.  They should be uploaded to Sakai.  Late submissions are not accepted.

You are encouraged to use lots of text to explain your answers.  For all but the simplest tasks, not explaining your answers could lead to points take off of your grade.

Using Mathematica on Homework

You are welcome to use Mathematica to assist with any homework problems you like.  Simply write on your written assignment that this computation/graph/etc. was done in Mathematica, and then include a printout of your Mathematica work (use the formatting conventions above).  If your Mathematica work is particularly long, you may upload it to Sakai and only print the first sheet, with a note to find the rest on Sakai.

Getting Started

To use Mathematica, you will need to either be on campus or connected via VPN to a Wake server.  For instructions on setting up a VPN, see https://is.wfu.edu/services/vpn/.

Save your work often (Ctrl-S).  Mathematica can crash; you might lose your work; no one would be happy in this case.

Writing & Formatting in Mathematica

Mathematica notebooks are organized by cells -- where you enter text, commands, or headings.  You will see a small, gray + connected to a gray line.  Clicking on the + gives you various options for how to enter text.  (So does clicking on Format -> Style in the menu above.)  The default option is “Wolfram Language input” -- where you enter commands for Mathematica.  Or you can use keyboard shortcuts.  

Inputs:

Title.  [Alt-1]  Each lab must have a filename given by your lastname, your first initial, -Lab#.nb  For example, if I were submitting this week’s lab, I would save it as ParsleyJ-Lab1.nb -- Mathematica uses the .nb extension, meaning “notebook”.

Subtitle.  Each lab must contain a subtitle, with your name.  (You may include other info in the title or subtitle if you like.)

Chapter.  [Alt-2]  Each task should be listed as a separate chapter.

Section.  [Alt-4]  If you are assigned exercises from the book, each should be its own section.

Subsection.  [Alt-5]  helpful sometimes

Text.  [Alt-7]  You should get in the habit of describing what work you are doing.

Input.  [just start typing, or Alt-9]  Commands

Free-form input.  [=]  Don’t know the exact command?  Start typing and if Mathematica can understand what you mean, it will give you the result, and the commands.

Wolfram Alpha. [==]  Wolfram Alpha is a wonderful web-based encyclopedia of facts.  It can do streamlined computations of what Mathematica can.

Math cellsThe Palettes->Writing Assistant has an interface for “Typesetting” mathematical formulas in text, such as (∑_(n=1)^k)(n^2).

Basic Commands in Mathematica

Notation

Parentheses () show order of operations.

Functions are capitalized, e.g., Sin[x].  Their inputs are set off by square brackets [].

Important!!!  To evaluate a function, hit Shift-Enter.

Unknown functions and variables appear in blue.  Known ones appear in black.

Curly brackets denote lists & vectors.  e.g., {1,2,3} and {New York, Los Angeles, Chicago}

a semicolon hides the output of a command

Mathematica as a calculator

A good place to start with Mathematica is with entering expressions and evaluating them. Here are some examples:

7!

Sin[π/2]

E

Pi

N[π,20]

1/3

1.0/3.0

SetPrecision[1.0/3.0,19]

Notice that:

You hit Shift-Return to evaluate the expression. Return just moves you down in the text window.

Pi is entered as Pi.  To type π, you enter Esc-pi-Esc (this works for other greek letters, too). Mathematica knows what π is automatically.

e is written with a capital ‘E’ (lowercase ‘e’) is just another variable name to Mathematica.

Numbers with a decimal point are treated as machine floating point numbers (actually, they are `doubles’). Numbers without are kept in fractional form.  N[x] gives you the decimal verison of a number.

If you want to control the precision you can use SetPrecision.

Mathematica does Calculus

Functions

To write a function of x, do this:

f[x_] := x^2* Exp[x]

f[7.0]
f[-0.1]

There are several things to notice about the function syntax.

On the left hand side, the variable appears with an underscore, like x_. No underscore, no live function. (Also, you won’t get an error message; most people make this mistake at least once.

The assignment is “:=” and not “=”. Again, no colon, no live function (but again, no error message).

Note the color coding on the right hand side:

Black. This is already defined.

Blue. This is not defined yet.

Limits

Limit[Sin[x]/x,x->0]

To obtain the right arrow, I entered ->.

Limit[((3x^4+8x^3-2x+4)/(4x^4-4x^1+1)),x->Infinity]

Derivatives & Integrals

The command for differentiation is D; the command for integrate is, um, Integrate.  You must put the independent variable, x, following the function, separated by a comma.  For definite integrals, provide a list with x and the integration limits. You can force Mathematica to numerically integrate using NIntegrate.

D[x^3,x]

f[x_]:=Sin[x]
D[f[x],x]

Here’s how we can take higher order derivatives.  The second and seventh derivatives of sin(x) are below.

D[f[x],x,x]
D[f[x],{x,7}]

D[E[x]*Sin[x^2],x]

Integrate[x^2,x]

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

Integrate[Sin[Sin[x]],{x,0,1}]

This is not an elementary integral, so Mathematica just returns the integral.  [Um, thanks a lot.]  But we can numerically integrate it

NIntegrate[Sin[Sin[x]],{x,0,1}]

Plots

Mathematica can produce extraordinarily good graphics.  We will explore only a small bit of its capabilities here.

g[x_]:=x^3 - 4x

Plot[g[x],{x,-2.5,2.5},AxesLabel->{"x","y"}]

If you said Plot[g[x_]..., it won’t work. 

Cool Fact about plots:  unless you tell it otherwise, Mathematica will always return a plot whose length-to-height ratio is the Golden Ratio, ϕ=1.618...  Below we adjust this ratio, and shade regions.

Plot[g[x],{x,-3,3},AspectRatio->Automatic,Filling->Axis,PlotRange->{{-2.5,2.5},{-6,6}},AxesLabel->{"x","y"}]

f[x_]:=Tan[x]Exp[x]
Plot[f[x],{x,0,1},Filling->Axis,PlotRange->{{0,Pi/4},{0,2.5}},AxesLabel->{"x","y"}]

Often, you will need to display multiple plots at the same time.  You can also use the Show command to display various plots together.

Plot[{Sin[x],g[x]},{x,0,Pi},Filling->Axis]
Show[Plot[g[x],{x,0,Pi},Filling->Axis],Plot[Sin[x],{x,0,Pi},Filling->Axis]]

Taylor Polynomials & Taylor Series

We won’t use series much in Math 113, but Mathematica loves them!

f[x_]:=Exp[x]
Series[f[x],{x,0,5}]

Notice the syntax: Series[function,{variable,point to expand around,highest order term}]. You can convert this to a polynomial with Normal

poly = Normal[Series[f[x],{x,0,5}]]

Mathematica is sometimes too smart for our good.  Above, poly isn’t a function yet. Why not? Because x isn’t a variable. To convert poly into a function automatically, we’ll need some trickery. In the meantime, we can always put it into InputForm and copy and paste to use it to define a function.

poly // InputForm

T[x_]:=1+x+x^2/2+x^3/6+x^4/24+x^5/120

T[1.]
Exp[1.]

Mathematica Resources & Help

You will have questions at some point about Mathematica.  To get answers, let me suggest a few ways.

Google.  I usually start by googling “Mathematica [whatever it is I want to do]”

Mathematica’s built-in help.  Access help on a command by typing ?[Command]

?Limit

Office hours & Math Center.  Warning:  not every tutor knows Mathematica, but many do.

Mathematica’s Getting Started Guide for Students.  http://www.wolfram.com/language/fast-introduction-for-math-students/en/

Mathematica Task #1:  Computations

Create your own notebook, and answer the following.  There are two tasks this week.  One is on basic Mathematica skills; the second is on parametric plots.

Compute the following:  8!; sin(π/12)

Find the limit of sin(x)/x as x approaches 0

Find the limit of (1+2/n)^n as n approaches infinity

Take the derivative of f(x) = x sin(x).  Then find its second & third derivatives.

Integrate this f(x).

Plot f(x) from -π to π.

Parametric Functions in Mathematica

Parametric Plots

Parametric plots in Mathematica are done using the command ParametricPlot. Here is an example of a spiral:

plot1=ParametricPlot[{t*Cos[2*π*t],t*Sin[2*π*t]},{t,0,2*π},AxesLabel->{"x","y"},PlotStyle->Red]

Here is an example of another spiral.

plot2=ParametricPlot[{t*Sin[2*π*t],t*Cos[2*π*t]},{t,0,2*π},AxesLabel->{"x","y"},PlotStyle->Blue]

Here is both of them combined

Show[plot1,plot2]

Animating Parametric Plots

I am first going to defined a function. Notice that this function takes in one number t and outputs two numbers which denote the coordinate of the point.

f[t_]:={Sin[t],Cos[Sqrt[2]*t]};

f[0]

f[π/2]

To animate the plot we use the command Animate and a range of values.

Animate[ParametricPlot[f[t],{t,0,s},AxesLabel->{"x","y"},PlotRange->{{-1,1},{-1,1}},PlotStyle->Pink,PlotPoints->100],{s,0,10*π}]

Mathematica Task #2:  Parametric Plots

Here are your tasks.

Plot 3 of your favorite parametric plots on the same graph. Each curve should have its own color.

Animate one of these parametric plots.

For the plot you used in part two, make it rainbow colored. Hint: In the help file look up parametric plot and look under neat examples.