Beginning Mathematica

Niels Walet (Niels.Walet@manchester.ac.uk)
(based on work of D.C.M. Burbulla and C.T.J. Dodson)

Introduction

This notebook is intended to illustrate some basic operations in Mathematica and can be personalized for saving in your home directory for reference purposes. 
If you wish to obtain information about any function or other command, the whole of the Mathematica book is available from the help menu (top right). You can find information about almost anything from there! The most important is the Help... menu, which opens a special browser. 
Before starting our little tour of Mathematica,a few basic rules:
1- Mathematical (and also Mathematica) functions are written as you think they are, but they are spelled starting with a capital letter, i.e., the sine function is spelled Sin .
2- The argument of a function is given in square brackets ([]), i.e., the mathematical notation cos(x) is written as Cos[x].
3- The product of two numbers (or two symbols!) is written as 10 10, or x y.
4- A  Mathematica notebook, such as the one you are looking at right now, consists of objects called cells (as indicated by the brackets on the right-hand side). These cells can contain cells that contain cells, etc. There are three important classes of cells you need to know about:
a- input cells: where you type your mathematical input. Click your mouse in an empty area of the notebook, and type any text. This will appear in a bold typewriter font, and allows you to type commands to Mathematica. End your input by holding the Control and Return keys, which will tell Mathematica to execute the commands you have just typed.
b- output cells: the output from your command, or some error messages. Same font, but not bold.
c- group cells, that contain other cells. You will see quite a few of them in this notebook, but initially most of them are closed, which is indicated by the small triangle at the bottom of the correpsonding bracket. Double click on the bracket, and the cell will open, showing all its information.
5- You can cut and paste output into new input!
6- Special characters, symbols, super (and sub) scripts, can be generated by using the  input pallettes (accessible from under the Palettes Menu). Choose the basic typesetting and basic input ones!

Elementary algebra

We shall first look at the Expand function. Use the help menu to find  out more about this function first (help->find in help browser->Go To: Expand). Then click on the input cell below, and type control return to execute. Can you explain the answer?

Expand[((((x^2)-3 x))^3)]

We often need to solve equations. Look up the Solve function, and execute the cell below. Note the == sig: A single equals sign is used for becomes, i.e., a=8 assigns 8 to a, a double equals sign doesn't assign, but compares. I.e., 0==0 is true, but 0=0 is illegal

Solve[(x^2)+6 x+8==0,x]

The answer is given as a set of rules, (read "goes to" for the ->). The way these rules are used is rather complex, and requires quite some understanding of the inner workings of Mathematica. The easiest way to use it is to substitute these rules for x, (/. means substitute once)

x/.{{x->-4},{x->-2}}

Solve gives the exact solution to an equation, look at

Solve[(x^2)+99 x+10==0,x]

Often we cannot solve equations exactly in algebraic form, but a numerical solution exists. Mathematica has the function NSolve for this case

NSolve[(x^4)+(x^3)-1==0,x]

Functions needn't have abstract arguments, we can evaluate the sin(0.1) (i.e. 1/10th radian)

Sin[0.1]

The solve function can also be used for coupled sets of (linear) equations

Solve[{x+y+z==10,10 x+9 y+8 z==6,-x+y-x==2},{x,y,z}]

exercise

Understanding functions

A plot of a function is as simple as the command below, where the three simbols in curly brackets tell Mathematica that we treat the function as depending on x, from -π to π. There are many options to the plot command, but just to understand a function the plain command suffices.

Plot[Sin[x],{x,-π,π}]

The problem with this is that many functions are more complicated. The way plot works, fortunately

Plot[(x^3)+3 (x^2)-5 x+1,{x,-3,3}]

The notation give above is very cumbersome. Often we wish to define our own function. The way this is written is by denoting the argument on the right hand side with an additional underscore (_), to denote its special role. Also note the := rather than =. [This means the right-hand side will only be evaluated when the function is called.]

f[x_]:=(x^4)+3 (x^2)-5 x+1

Let's test this!

f[1]

This is true since

Factor[f[x]]

Our newly defined function can be used in a Plot command as well (the ; suppresses some unwanted output)

Plot[f[x],{x,-3,3}]

We could also tabulate the function. The notation {x,f[x]} means that we wish to see that pair in the table. The fourth number in the string {x,-3,3,0.5} indicates the step taken.

t=Table[{x,f[x]},{x,-3,3,0.5}];TableForm[t]

exercises

3-D Plots

Three-dimensional plots are very similar to 2D ones

Plot3D[-(x^2)-(y^2),{x,-1,1},{y,-1,1}]

Plot3D[-(x^2)+(y^2),{x,-1,1},{y,-1,1}]

Plot3D[(x^2)+(y^2),{x,-1,1},{y,-1,1}]

ContourPlot[Sin[x+y] Cos[x],{x,-π,π},{y,-π,π}]

There is a special package for 3D Plots

ParametricPlot3D[{r ,r Cos[ϕ] ,(r+0.5)Sin[ϕ]},{r,0,1},{ϕ,0,2 π}]

Exercise

Differentiation

Derivatives can be indicated in many ways

(∂_x)(x^n)

D[(x^n),x]

For example Mathematica knows that

(∂_x)Sin[x]

This can also be written as

Sin'[x]

In a similar vein

(∂_x)Sin[x y]

The second derivative w.r.t. x can be written as

(∂_({x,2}))Sin[x y]

Exercise

Finding Extrema

Information["f",LongForm->False]

(∂_x)f[x]

f'[x]

Solve[f'[x]==0,x]

N[Solve[f'[x]==0,x]]

Plot[f[x],{x,0.6,0.7}]

Exercise

Integration

Mathematica knows how to do most standard integrals, and a truckload of nonstandard but soluble ones as well. Let us look at a few examples, of both definite and indefinite integrals.

∫(x^2)\[DifferentialD]x

∫Sin[x]\[DifferentialD]x

(∫_0^(π/15))Sin[x]\[DifferentialD]x

∫ArcSin[Cos[x]]\[DifferentialD]x

Exercise

Numerical Integration

If an integral can not be performed in closed form, Mathematica knows how to perform numerical integration.

NIntegrate[Sin[x],{x,0,(π/15)}]

NIntegrate[(E^(-(x^2))),{x,0,1}]

We can also use the result of the integral to define a function,

er[y_]:=NIntegrate[(E^(-(x^2))),{x,0,y}]

Plot[er[y],{y,0,2}]

erdata=Table[{y,er[y]},{y,0,2,0.1}];TableForm[erdata]

Exercise

Multiple Integrals

Multiple integrals (i.e., of integrands with more than one variable) can be done in much the same way

(∫_0^1)(∫_0^(π/2))((E^y)Sin[x])\[DifferentialD]x\[DifferentialD]y

Boundaries can depend on an outre integration

(∫_0^1)(∫_0^y)(Exp[y]Sin[x])\[DifferentialD]x\[DifferentialD]y

We can define a function as the result of an integral

f1[a_]:=(∫_0^a)(∫_0^(π/2))((E^y)Sin[x])\[DifferentialD]x\[DifferentialD]y

and plot it

Plot[f1[a],{a,0,1}]

Or tabulate it (note the N[ ], required to get a numerical answer for some values of a (0)

t=Table[{a,N[f1[a]]},{a,0,1,0.2}]

TableForm[t]

Exercise

Solution of ODE

Before progressing, we first need to remove some lingering definitions (from previous sections). Please execute the cell below [Assigning "." to a variable clears it]

t=.;Remove[v];

We all know the solution to the differential equation y''=a, let's  check whether mathematica knows how to deal with that:

DSolve[{((y'))'[t]==a,y'[0]==v,y[0]==x},y[t],t]

Some complicated equations (really, most differential equations) don't have a closed from solution. Suppose we have to solve the differential  equation y'=1-(y^2), where y depends on x. Mathematica knows how to solve this equation numerically (the ; suppresses the output)

NDSolve[{y'[x]==1-((y[x])^2),y[0]==4},y,{x,0,1}];

We can plot the result (which comes out as a rule, so needs to be substituted for y[x]. The "%" sign contains the output of the previous calculation.)

Plot[Evaluate[y[x]/.%],{x,0,1}]

Exercise

Vector algebra

Mathematica also knows vector algebra. Vectors are written as lists, starting and ending with a curly bracket, e.g.

{1,2,3}

It knows about the inner (dot product)

{1,1,1}.{2,-2,0}

and the outer (cross) product

{1,1,1}\[Cross]{2,-2,0}

It can do all kinds of nice things

{1,1,1}.{1,1,1}\[Cross]{2,-2,0}

2 {1,1,2}

Let us define a function to calculate the norm (length) of a vector

norm[v_]=(Sqrt[v.v])

norm[{2,2,2}]

This equals the inbuild function Norm

Norm[{2,2,2}]

Exercise

Needs["VectorFieldPlots`"]