Mathematica

Getting Started

If you've never used Mathematica before (or need a refresher!) watch the 10-minute intro to Mathematica. Then start up Mathematice--It's available on all the campus lab computers.

  • Starting up Mathematica: If you're asked for a code to activate Mathematica choose "other options". You want to enter the name/IP num of a licensing server. Try "kms.goshen.edu".
  • I'm not sure that they tell you in there...When you're dealing with Mathematica expressions, you hit <shift><return> to execute the line of code you're currently in. (You don't have to be at the end of the line): This frees you up to use <return> to put your Mathematica expressions on several physical lines.

Now, read about typical Mathematica gotchas.

See also: Learning resources - higher ed

Exercises:

  1. Graph (that is Plot[...] the function $f(x)=\frac{\sin x}{x}$.
  2. Make a manipulatable plot of the function $g(x)=\sin(ax)/x$, where you have a slider that lets you try values of $a$ ranging from 0.01 to 13.0.

Mathematica Functions and Symbols

  • Built in functions always begin with capital letters, and use square brackets to enclose their argument(s) like this: Log[23.78].
  • Use the N[...] function to get a numerical answer. For example N[Sin[Pi/4]] to get a number, instead of the symbolic answer 'Sin[$\pi$/4]'.

Exercises:

  1. Find the decimal number which correspond to $2-\sqrt{2}$. You'll need to look up (or guess) the built in function that calculates square roots.
  2. Display the first 25 digits of Pi (which you can type in as Pi). (Look up the documentation on the N[...] function).
  3. Is the argument to the Sin[...] function in Mathematica assumed to be in degrees or radians? Show this by displaying the $\sin$ of some well known number.
  4. What do you get if you try to get a number for (lower case 's':) sin[45] compared to Sin[45]?
  5. Is the argument to the Log[...] function in Mathematica the base 10 or the base $e$, or base something else? Show this by displaying the Log of some well known number.
  6. Figure out how to change the base of the Log[] function. You know that $5^3=125$, so figure out the syntax to get Mathematica to tell you that $\log_5 125=3$.

Solve and expressions and lists

  • = is the assignment operator, e.g. P=32*x assigns the expression $32*x$ to a variable called $P$.
  • == is the equals sign. So, 32*x==64 is an equation, (the solution of which is 2). (It's *also* an expression!)
  • Use Solve[] to solve equations, for example Solve[32*x==64,x] to solve the equation for $x$.
  • The solution to an equation is a rule, indicated with -> which can be read as 'goes to'. The solution to the equation above is x->2, "$x$ goes to 2".
  • /. is the substitution operator. You use a rule to substitute in to an expression. For example to test the solution to the equation above, you'd like to substitute x->2 in expression $P$ to see if it evaluates to 64: P/.x->2.
  • {...} is a list. Many Mathematica functions can operate on lists instead of single numbers, or single expressions. For example, you know that in general if you have two equations for two unknowns that there is a unique solution for each unknown. See solve.nb to see this in action.

Download solve.nb and add your code at the bottom for the following exercises:

  1. Verify that you get solutions that you expect for $x^2=9$ and $x^2=-9$.
  2. Find solutions to the quadratic equation $14x^2 - 56x + 28=0$.

  3. Find decimal numbers for the width and length of a rectangle that fulfills these two conditions: a.) Perimeter = 30 meters, b.) Area of the rectangle = 40 m^2
  4. Same as above, but additionally, tell Solve[] that the length is greater than the width.

  5. The small angle approximation $\sin x \approx x$ holds for 'small' $x$ values. How far out (in $x$) does this approximation hold? Find $x$ such that $x$ and $\sin x$ differ by 10% (that is, 10% of $f(x)=\sin x$).

    It is probable that Solve will kick an error back at you. Include the inequality x>0 and see if you now get an answer...
  6. [Answer: Download smallangle.nb or click on this pdf.]

Plotting and derivatives

Download plotting.nb and add your code for the following exercises:

  1. Plot the quadratic expression $14x^2 - 56x + 28$ for a 'reasonable' range of $x$-values where you can see the places where the graph crosses the $x$-axis. Do the zeroes you found above match the zeroes on your graph?
  2. The small angle approximation $\sin x \approx x$ holds for 'small' $x$ values. You figured out above where they diverge by 10%.
    Plot $f(x)=x$ and $g(x)=\sin x$ on the same graph to show this. Have the $x$-values of the plot run from -10 to +10, but restrict the $y$-values to only -2 to 2. Doe your '10% difference' value look about right as you examine the graph?
  3. Call $f(x)$ the cubic expression in plot.nb. Find the $x$-values which solve $f'(x)=df/dx=0$. [See Solve above]. Plot $f'(x)$ and $f(x)$ on the same graph. Do these make sense?
  4. Read the documentation, or Google, to figure out how to do a logarithmic plot. Make a log plot of $e^{-0.8x}$ over a range of $x$-values such that the $y$-values range over at least 3 orders of magnitude. Your function should appear as a straight line.
  5. Plot a circle of radius $R=2.5$ by making a parametric plot with the parameter $\theta$: $x(\theta)=R*\cos\theta$ and $y(\theta)=R*\sin\theta$. What should the range of $\theta$ be to get a full circle? [To get a $theta$ symbol in Mathematica, type esc-q-esc.]

Now you're in position to do some more plotting: Read through the mathphys topic [Taylor series] and do exercises 1 and 2.

Defining functions

You can define your own function like this...

Many of you discovered the Series function which gives you a power series (Taylor series). For example, this gives you a power series for the natural logarithm $\ln(x)$ around $x=1$ with 5 terms:

Now, it seems like, to plot this, all you'd need to do is define a function and plot the function, but nooooooo!

It seems like values for $x$ are being substituted systematically before evaluating. But, one more round of massaging works with the Evaluate function...

Notice also how '%' works: it symbolically means 'replace me with the result of the previous command':


Plotting more than one function
on the same graph:

The Normal[] function looked promising, but it's not quite what we need either....

Differential equations and rules

A couple things to note:

  • spell at an equation with ==
  • % refers to the previous command's output
  • /. means substitute using the following rule (or rules). A rule is indicated ->.
  • Notice how derivatives are indicated: f'(x). You can also use D[f[x],x].
  • Here we'll use DSolve to exactly solve a differential equation.
  • The output of DSolve is not an expression, it's rule.
  • We'll use Evaluate with rule substitution to turn it into something we can use to plot.

Paul's experiments so far...DiffEq.nb

For the weekend: Try to use NDSolve[ ] (consult the documentation on NDSolve...) to solve a differential equation that you *know* the solution for. E.g.

$$\ddot{f}(x) = -\omega^2 f(x)$$

or

$$\dot{f}(x)=k\cdot f(x)$$

Documents

You can use Mathematica to compose documents with equations, as well as doing math. Some inroads to how to do it:

  • Check out the NOTEBOOKS AND DOCUMENTS topic in the Mathematica guide
  • Insert a text cell instead of a Mathematica cell and type.
  • Pull up Palettes | Writing Assistant
  • Click below your text, and insert a Math cell
  • Use the options under the Typesetting portion of the Writing Assiastant to build up math expressions.

  • Enter math mode in the middle of text by typing <shift><ctrl>(. And <shift><ctrl>) leaves math mode.
  • Greek letters: <esc>[character]<esc>. You don't have to be in math mode to do this.
    A partial list of some of the characters that lead to greek letters:
    $a\to\alpha$, $b\to\beta$,$o\to\omega$,$q\to\theta$,...