Departments of Mechanical Engineering and Mathematics
Mathematica Session 1

Mathematica: A System for Doing Mathematics by Computer

Engineers were some of the first professionals to recognise the benefits of 
using computers to perform calculations. Over the years, as hardware and 
software have become immensely more powerful and computers more and more 
widely available to individuals, the main thing that's changed is what we 
mean by "calculations". Once computers were reserved for processing large 
amounts of numerical data (what sometimes gets called "number-crunching").  
While that's still one of the things they're good at, the story doesn't end 
there any more.

Mathematica is just one of the mathematical software tools available to the modern 
professional engineer: one of the more powerful, flexible and expensive ones, 
as it happens! It's described by its manufacturers as "A System for Doing  
Mathematics by Computer". In this introductory session, you'll explore what 
that means in practice.

The price you pay for all this power and flexibility is that Mathematica takes some learning. Unlike some word processing packages, for example, 
or even some of the more restricted mathematical software you sometimes see, 
its use isn't self-explanatory. It has a syntax all its own, and it can be 
very pedantic about errors in that syntax. Using it often feels more like programming a computer than using a package, and actually, that's no accident. Stick 
with it, though: it's worth it in the end.

1. Arithmetical calculations

To start with, it may be helpful to think of Mathematica as a kind of calculator. For example, try typing

2 + 2

To get Mathematica to perform this calculation, hold down the "shift" key and press "return" ("return" without "shift" just gives you a line break.) With any luck, you should get the 
 output

We say that the expression 2 + 2 has been "evaluated".

This calculation may have taken a long time. That's not because Mathematica finds it difficult, but because the part of the program that actually 
does the calculating doesn't get loaded until the first time it's needed; 
it's this loading process that takes the time. All subsequent calculations 
should be much quicker, some very fast indeed. Here are a few:

6 + 2

6 - 2

6*2

6 2

6/2

6^2

100!

1000!

Sin[Pi/3]

Sqrt[9]

Sqrt[50]

2^(1 + 4)

Notice the use of the symbols *, / and ^. Notice, too, the way Mathematica interprets the space between the 6 and the 2 in the fourth line.

Exercises and explorations 1

1.	The last few examples all involve the use of brackets of some kind. But sometimes square brackets have been used, and sometimes 
round ones (parentheses). Does this matter? What are the rules governing what kind of brackets to 
 use when?

2. 	Would it have mattered if we'd typed

sin[pi/3]

instead? What are Mathematica's rules about capital and lower-case letters?

3. 	Was the output from

Sqrt[50]

what you'd expected? What's happening here? What other examples can you 
find?

The Basic Input palette

If you look to the right-hand side of the screen, you may see a tall 
rectangular window, the top portion of which looks like this:

If not, you can open this window (which is known as the "Basic Input" 
palette) by finding Palettes in the File menu and selecting BasicInput.

This facility allows you to create Mathematica commands that look like typeset mathematics. Try, for example:

(6^2)

Sin[(π/3)]

(Sqrt[50])

Exercises and explorations 2

1.	What happens if you type 6, followed by "ctrl-6" (hold down the 
"Ctrl" key and hit 6), followed by 2? What does "ctrl-6" do? How about 
"ctrl-/"?

2.	 Try typing "esc, p, i, esc" (hit the escape key, then p, then i, then 
escape again). What happens? What if you leave out the "i"? What other 
symbols can you create in this way?

Exact or approximate?

All the commands above are examples of exact arithmetic: calculations with whole numbers, surds, fractions and named constants 
such as π. This is something a calculator doesn't do: it works with numbers in the form of decimals, or in standard 
scientific form. Mathematica can also do this type of arithmetic. Try the following pairs of commands, 
 taking note of any differences in the output:

(2/6)(2.0/6.0)

Cos[(π/6)]Cos[(3.1415927/6)]

Sin[2]Sin[2.0]

(Sqrt[50])(Sqrt[50.0])

Exercises and explorations 3

1.	Experiment with the Mathematica command N, as in

N[π]

N[π, 100]

N[(2/6)]

How would you describe what this command does? How could you see it 
being used?

2.	Investigate the Mathematica commands Floor, Ceiling and Round, as in

Floor[3.77]

Round[25]

2. Symbolic calculation

Where Mathematica starts to differ from most calculators is in its ability to perform symbolic calculations, for example:

Expand[(((x + y))^3)]

Factor[(x^3) + 3  (x^2)*y + 3  x*(y^2) + (y^3)]

Apart[(x/((1 + x) (1 + (x^2))))]

Together[%]

(Notice the use of the percent sign, %, to mean "last output").

D[(x^3), x]

∫3  (x^2) \[DifferentialD]x

Exercises and explorations 4

1.	What do Apart and Together do? How about Cancel and Simplify?

2.	Open up the Algebraic Manipulation palette (under Palettes in the File menu). This palette, unlike Basic Input, has the setting "Evaluate in 
Place". To find out what this means, first type, without evaluating,

∫((2 + 3  x + (x^2))/(2 + 2  x + (x^2))) 

        \[DifferentialD]x

Then select the fraction inside the integral, and click on the Apart[■] button. With the same area selected, click on Together[■]. Try using Expand[■] on the numerator, and so on.

3.	Using the Basic Input palette, type

(∑_(r = 1)^6)((1/(r^2)))

Express this sum as a decimal. Try summing from 1 to n, and from 1 to ∞ (you'll find the infinity symbol on the  
palette).

3. Graphics

Mathematica incorporates a wide range of two- and three-dimensional graphics 
functions. The simplest is Plot, which generates ordinary two-dimensional graphs, as in

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

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

(Notice here the use, for the first time, of curly brackets or braces, to enclose the elements of the x-range or to group collections of functions.)

You can take control of some of the characteristics of the plot by means 
of what are called option settings, for example:

Plot[Sin[x], {x, -π, π}, PlotRange -> {-1.5, 1.5}]

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

      PlotRange -> {{-2 π, 2  π}, {-1.5, 1.5}}]

Plot[Sin[x], {x, -π, π}, AspectRatio -> 1]

Plot[Sin[x], {x, -π, π}, AspectRatio -> Automatic]

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

      PlotStyle -> {RGBColor[1, 0, 0], RGBColor[0, 0, 1]}]

More than one option can be set in this way, for example:

Plot[(E^(-x)), {x, 0, 3}, Background -> Hue[0.5],  

      PlotLabel -> "Decaying exponential function"]

For a complete list of options for Plot, together with their default settings (what Mathematica assumes the setting to be if you don't set the option explicitly), type

Options[Plot]

Exercises and explorations 5

1.	If you have not done so already, look up the entry for Plot in the Help system: choose Help from the Help menu, and type "Plot" in the search box. Find out how the AxesLabel option works, and produce a graph of tan(x) where the x-axis is labelled "x" and the y-axis "tan(x)".

2.	Produce a plot showing the relationship between the graphs of y = (x^2) and y = (Sqrt[x]). Your graphs should be in different colours and the scales should be the 
same on both axes.

Other graphical functions

Plot is the simplest graphical function in Mathematica, and one of the most useful, but there are many others:

ParametricPlot[{t  +  Sin[t],  1 + Cos[t]}, {t, 0, 4  π}]

Plot3D[

      ((x^2) - (y^2)) (E^(-(x^2) - (y^2))), {x, -2, 2}, {y, -2, 

        2}]

ContourPlot[

      ((x^2) - (y^2)) (E^(-(x^2) - (y^2))), {x, -2, 2}, {y, -2, 

        2}]

ParametricPlot3D[{Cos[θ], Sin[θ], θ}, {θ, 

        0, 2  π}]

ParametricPlot3D[{Cos[θ] Sin[ϕ], Sin[θ] Sin[ϕ], 

        Cos[ϕ]}, {θ, 0, 2  π}, {ϕ, 0, π}]

Exercises and explorations 6

1.	By studying the output from the above, and by further investigation if 
necessary, find out how (i) a maximum (that is, a hill), (ii) a minimum (that is, a depression) and (iii) a saddle (that is, a high "pass" between two hills) show up on a contour plot.

Estimate the coordinates of any maxima, minima and saddles of the 
function z = ((x^2) - (y^2)) (e^(-(x^2) - (y^2))).

2.	Produce 3D parametric plots of a helix that spirals outwards as it 
rises, and of a prolate spheroid (rugby ball or American football shape).