Introduction to Mathematica


Getting started

Open a Mathematica window.
Try typing 2+3 and hitting "Enter" - nothing happens? Mathematica allows more than one line of input, so it preserves "Enter" for getting to the next line. So instead, to process your input, you have to press Shift and Enter (simultaneously). Now you should see (wait a few moments if it's slow at first) something like:
In[1]:= 2+3
Out[1]= 5

Arithmetic calculations

Use +, -, *, / for addition, subtraction, multiplication, and division,
x^y denotes "x raised to the power y".

Try 1/3+2/7. The output is a fraction.
If you want the answer to be given as a decimal, type N[1/3+2/7] ("N" stands for "Numerical result").
To compute the answer to 20 decimal places accuracy, type N[1/3+2/7,20].

Exercise: Find the cubic root of 2 accurate to 25 decimal places.

Exercise: Calculate as an ordinary fraction, and also as a decimal:
math1
Note: You have to enter all necessary parentheses! You should get -22/15 and -1.46667.

You can assign values to variables and then evaluate expressions of variables:
In[20]:= x=5
Out[20]= 5
In[21]:= y=6
Out[21]= 6
In[22]:= x*y
Out[22]= 30

To save space, you can define variables on one line:
x=5; y=6;
The ";" will suppress the output.
Also, if you need to evaluate several expressions, you can put them in a list. A list is specified with set braces {...} and the elements of the list are separated by commas.

Exercise: Execute the following: { 2x, y-1, x*y, x y, xy, x^y }.

Notice that xy was not evaluated. That's because Mathematica thinks it is one variable with name xy and not the product of x and y. You do not need to use a space or "*" when multiplying a variable by a number, but you do need a space or "*" when multiplying 2 variables or 2 numbers since xy can be another variable, just as, say, 56 is another number and not a product of 5 and 6.

Note: When you finish working with variables you should "remove" them (from the computer memory). The variables x and y used above are defined and have values. If you use x or y again later on, forgetting about the above, unexpected things can happen, e.g. the software will evaluate them using the above values when you don't mean to work with the above values any more. Alternatively you can remove the symbols just before you use them again.

To find out the value of x, type ?x.

To remove x, type Remove[x].

Functions

Here is a list of the most commonly used functions: Sqrt[x], Abs[x], Exp[x], Log[x], Sin[x], Cos[x], Tan[x], ArcSin[x], ArcCos[x], ArcTan[x], n!.
Notice that all math function names begin with capital letters (sometimes names made from more than one name, like ArcSin have capitals in the middle too) and the arguments in functions are enclosed in square brackets. Use Pi, E, and I for pi=3.1415926..., e=2.7182818..., and the imaginary number i.

Exercise: What is tan of 45 degrees? arccos(1)? arcsin(1/2)? Use Mathematica to check your answers.

Exercise: What happens if you type Sqrt[-4]?

If you need to calculate the values of the same function at several points, you can define the function, and then plug in the points:
f[x_]=x^3-7x+3;
f[1]
f[2]
f[3]
f[4]

Again, to save space, you can use a list:
{f[1], f[2], f[3], f[4]}.

Here are some useful packages for working with polynomial and rational functions: Expand[...], Factor[...], Simplify[...], Together[...] (puts over a common denominator).

Exercise: try the following.
Expand[x(x-5)(x^2+1)]
Factor[x^16-1]
Simplify[(x+2x^2)/(1-x^2)+(1-2x)/(1-x)]
Together[1/(x-1)+1/(x+1)]

Equations

Try Solve[x-3==2x+5,x] and Solve[x^3-x==0,x]. Note the two "=". "x" at the end means that we want to solve the equation for x.

Exercise: Solve the equation 1/(x-2)=x+1. Use Mathematica to check your answer.

Graphics

Plot[f,{x,a,b}] graphs the function f from a to b.

For example, to graph sin(x) from 0 to 2pi, type Plot[Sin[x], {x,0,2*Pi}].

Alternatively, you can define a function f(x) and then plot f(x):
f[x_]=Sin[x];
Plot[f[x],{x,0,2*Pi}]

Exercise: Plot the function math2 on the interval [-4,4].

To plot several functions simultaneously, list them in "{ }":
Plot[{Sin[x],Cos[x]},{x,0,2*Pi}].

Exercise: Above we solved the equation 1/(x-2)=x+1. Now plot 1/(x-2) and x+1 on the same screen for x from 0 to 4. You will see that the two graphs intersect. However, it appears that they intersect again outside of our interval. Increase the range to see both intersection points.

Limits

To evaluate the limit, use Limit[function,variable->value that the variable approaches]. You can define a function f(x) first and then use f[x] inside the limit:

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

or you can write the expression inside the limit:

Limit[(4-x)/(2-Sqrt[x]), x->4]

Exercise: Try the above limits and also plot the graphs of those functions. Check that the answers to the limits agree with the graphs.

You have to be careful when the limit does not exist or when it is infinite. Try the following:
Remove[f];
f[x_]=1/x;
Limit[f[x], x->0]
Now plot the graph of f(x)=1/x. Does the limit as x approaches 0 exist? Try the one-sided limits:
Limit[f[x], x->0, Direction->-1] is the limit as x approaches 0 from the right (notice that when x approaches a number from the right, x is decreasing, so x is changing in the negative direction. That's why we have to use Direction->-1).
Limit[f[x], x->0, Direction->1] is the limit as x approaches 0 from the left (so x is increasing, i.e. x is changing in the positive direction).

To evaluate limits as x approaches infinity or negative infinity, use Limit[f[x], x->Infinity] and Limit[f[x], x->-Infinity].

Exercise: Evaluate the following limits. Also, plot the graphs of the function and see how the answers you are getting agree with the graphs.
math3
Note: As you know, the last limit does not exist, and the limit of ln(x) only exists as x approaches 0 from the right since ln x is only defined for positive x. But try to compute all the limits and see what happens.

Derivatives

As with the limits, one way to compute the derivative of a function is to define the function f(x) and then use f'[x]:
f[x_] = 5x Sin[x];
f'[x] computes the first derivative,
f''[x] is the second derivative, and so on...
f'''''[x] is the fifth derivative...

Note: The "prime" notation will not work with something like y':
y=5x Sin[x];
y'

Another way to compute derivatives is to use D[expression, variable]:
D[5x Sin[x], x] or
D[f[x], x]
This notation is more convenient if you want to compute higher derivatives. The nth derivative is D[expression, {variable,n}]. E.g. to find the fifth derivative of f, use D[f[x], {x,5}].
You can also use y with this notation: define y=5x Sin[x], and then use D[y,x] to find y', and D[y,{x,5}] to find the fifth derivative of y.

Exercise: Compute the derivative of math4 and then simplify it.

Exercise:

  • Plot the graph of
math5 from -8 to 8.
  • Based on the graph of the function, sketch (by hand, on a piece of paper) the graph of its derivative. (Recall that at each point, the derivative is the slope of the graph of the original function.)
  • Use Mathematica to compute the derivative of f(x), and plot the graph of the derivative. Compare your graph and the graph you obtained using Mathematica.
  • Do the same for the second and third derivatives.

Exercise: Let math6 .
Do the following by hand first, and then use Mathematica to check your answers (whenever this is possible):
This page was last revised on April 23, 2015.