Mathematical Models, Spring 2016
Mathematica
This page is for a past course. Find your current course here.
 
Quick links to: ConceptsAlgebraListsPlotting

Important Concepts

  • If you need help with a command, use '?' (Example: ? Table)
    OR go directly to the Documentation Center.
  • To go to the next line, push Return.
  • To evaluate a cell, push Shift-Return (you can also use the Enter key on the number pad).
  • To evaluate ALL cells in the notebook, go to the Evaluation menu and click on Evaluate Notebook.
  • To stop an evaluation that is taking too long:
    • Type Ctrl-. (Period) on a PC or Apple-. on a Mac.
    • If that doesn't work, you can go to the Evaluation menu; at the bottom, click Quit Kernel then Local. [Careful: This will remove all definitions that you have entered in the session.]
    • Or you can always Save, Quit, and Restart.
  • Cells can be input cells (useful for doing Math) or text cells (useful for discussing what Math you're doing).
  • If you do not wish to see the output of an expression, use a semicolon ; at its end.
Many of your initial errors will come about because of one of the following two problems:
  • Mathematica is Case-SenSitive (AA is not the same as aA), so be careful about what you type.
    In particular, all built-in Mathematica functions are spelled out and capitalized, such as Table, ListPlot, IntegerPart, etc.
  • In Mathematica, it is important to distinguish between parentheses (), brackets [], and braces {}:
    • Parentheses (): Used to group mathematical expressions, such as (3+4)/(5+7).
    • Brackets []: Used when calling functions, such as N[Pi].
    • Braces {}: Used when making lists, such as {i,1,20}.
    If you use the wrong symbols in the wrong places or if you do not have a closing symbol for every opening symbol, Mathematica will give you an error message.
  • In Mathematica, there are four types of equals: =, :=, ==, and ===.
    • To define a variable to store it in memory, use =. For example, to define z to be 3, write z=3.
    • You use == to check for equality. For example, 1-1==0 will evaluate to True and 1==0 will evaluate to False.
    • You use := to define your own command. (This is advanced.)
    • You will likely not use === in this class.

Algebra and Calculus

Mathematica will do everything your calculator can and more.
  • Use ^ to put something to a power.
  • pi is Pi, e is E and sqrt(-1) is I.
  • If you want to see the numerical approximation to a fraction or irrational number, use the function N. For example, to find the decimal represenation of pi, write N[Pi].
  • Use E^x or Exp[x] to represent the function ex.
  • To take the derivative of a function, use D and specify the derivative with respect to which variable. For instance D[x^2 + 3x, x].
  • To take the integral of a function, use Integrate and specify the integral with respect to which variable. For instance Integrate[x^2 + 3x, x].
  • To solve for the roots of ax2+bx+c=0 symbolically, use Solve[a x^2 + b x + c == 0, x].
    Notice the double equals sign. (Mathematica is searching for when the expression is True.)
  • Coefficient[(1 + x)^10, x^3] gives the coefficient of x3 in the expansion of (1 + x)10.

Lists

Mastering lists in Mathematica is key—they are the method by which Mathematica stores and references data. In addition, it is often the case that functions require as part of their input a list.
  • A list looks like {1,2,3}.
  • Entries in a list may be anything, even lists. For example, one might define A={{1,2},{2,3},{4,10}}.
  • The most important list command: Table. Use Table to create lists based on rules. The input is two things, first, a function of a variable, and second, a list containing (1) the variable that is changing and (2) the range of values for the variable. Here are some examples.
    • Table[n^2,{n,10}] gives a list of the squares of integers from 1 (default) to 10.
      (Output: {1,4,9,16,25,36,49,64,81,100})
    • Table[n^2,{n,0,10}] gives a list of the squares of integers from 0 to 10.
      (Output: {0,1,4,9,16,25,36,49,64,81,100})
    • Table[n^2,{n,0,10,2}] gives a list of the squares of numbers from 0 to 10, incrementing by 2 each time.
      (Output: {0,4,16,36,64,100})
    • Table[n^2,{n,{3,20,100}}] gives a list of the squares of numbers 3, 20, and 100.
      (Output: {9, 400, 10000})
  • In order to reference elements in a list, use DOUBLE square brackets.
    Example: To determine the second entry of the list A, evaluate A[[2]].
  • To count the number of elements in a list, use Length[].

Plotting

  • To plot a function f(x) for x between -10 and 10: p1=Plot[x^2+3x,{x,-10,10}]
  • To plot a list of points: p2=ListPlot[{{1,2},{2,3},{3,10}}]
  • To plot multiple plots at the same time, use Show[p1,p2]].
  • There are MANY MANY options that you can use to modify these plots to your liking.
    Use ? Plot and ? PlotStyle to learn more.