1. Introduction to Mathematica

Calculator Features of Mathematica

We often ask Mathematica to do simple calculations for us, and so we need to know how it reads basic input.

To start learning how to use Mathematica, we need to know how it reads basic calculations.

Mathematica  can be used as a simple calculator.  This is a bit like opening a peanut with a sledge hammer but none-the-less this is a very good place to start learning how to use Mathematica. 
     If you wish to have Mathematica add 2 and 7, then just type 2 + 7 .  Then select the cell bracket and choose Evaluate Cells from the Mathematica Evaluation menu.  As a result you obtain from Mathematica

2 + 7

An alternate way of having Mathematica do a computation is to hit the ENTER key on a Mac or the SHIFT key together with the RETURN key on a Windows machine and this also works on a Mac.

Each calculation performed is a sort of dialogue with Mathematica.  What you tell Mathematica  to do Your instructions to Mathematica are placed in the input cell labeled In[1]:=  .  The result Mathematica  obtains is reported in the second or output cell which is labeled Out[1]= . The number in square brackets (the number 1 in this case) means this is the 1st dialogue with Mathematica  which has occured since starting this session. By the way, when you do the first calculation (of a session) with Mathematica it always takes longer then subsequent calculations as it is necessary to start the Mathematica kernel (the computational engine) and this takes time.  The Mathematica "front end" is how you, the user, interact with the kernel.

WHAT IS FRONT END? DO WE NEED TO SAY?

CAN WE COMBINE SUBTRACTION WITH ADDITION ABOVE?

I GUESS IT'S OK, B/C  IT FOCUSES ON DIFFERENT ASPECTS OF INPUTTING.

Subtraction is also easily performed with Mathematica ; for example, subtraction of 7 from 2 is obtained thus

2 - 7

The "spaces" in   2 - 7 can be removed and  2-7 works equally.  (You should try this and all other examples given in the introduction of each module.)

MAYBE DON'T MENTION THIS IN THE ABOVE PARAGRAPH
     At the start of a session, the first calculation takes longer than subsequent calculations since Mathematica  takes time to start its computational engine, the "kernel".  Each time you begin a session, have Mathematica  perform a simple arithmetic calculation so you can verify the kernel is working.   If Mathematica  refuses to perform the simple calculation, the problem may be fixed with some hints at the end of this module.  

The multiplication of 6 and 17 is done either with a "star" (used in many computer languages)

6*17

or a multiplication × symbol which can be found in the middle on the BASIC MATH ASSISTANT palette indicated below (see three rows down)

Inputing the multiplication yields, as before

DO WE NEED TO MENTION THAT "X" DOES NOT WORK?

6×17

Quite often the BASIC MATH ASSISTANT Palette will appear automatically on your screen at Startup of Mathematica.   If it doesn't, the BASIC MATH ASSISTANT palette can be accessed from the menu item PALETTES at the top of the Mathematic application window and then select BASIC MATH ASSISTANT as indicated below.

A "space" for the multiplication operation works equally well

6 17

Combinations of operations may be grouped using pairs of parenthesis

(3+8)*4

The calculation inside the parenthesis is done first before the other calculation. If you try the calculation without parenthesis, namely 3+8/4  then you get a different answer.  Unless parenthesis are used, Mathematica does (got rid of passive voice) all multiplications and divisions first and then additions and subtractions.    Pairs of parenthesis must be used or Mathematica  gives an error message: Syntax::sntxi: Incomplete expression.  

      Division with Mathematica  is different from division on a usual hand calculator since Mathematica  keeps rational numbers as exact--without decimal approximation--unless specifically asked to make a decimal approximation.  For example, ask Mathematica  to divide 2 by 5

2/5

Another way of inputing fraction is to use the BASIC MATH ASSISTANT pallete which was displayed previously.  The division symbol is close to the multiplication symbol.  Select the icon for division and thus obtain the template appears

(\[Placeholder]/\[Placeholder])

which you can fill in.  Place the mouse arrow over a box and "click".  Then insert the number you wish.  For, the example before, one obtains

(2/5)

There are several ways to cause Mathematica  to make a decimal approximation.  Perhaps the easiest way to get a decimal result is to put a decimal point at the end of the 2 in the numerator (or the 5 in the denominator).  A dialogue with Mathematica  then yields

(2./5)

Since 2. is treated as a decimal, Mathematica  treats the division by decimal approximation.

Some Symbols and Functions Contained in Mathematica

The N[ ] function

The "numerical function" N is another method of getting a decimal approximation to a number from Mathematica .  For example,

N[2/5]

The N must be a capital letter in the number function for it to work.   All Mathematica  functions begin with capital letters.  The number function N acts on the fraction 2/5 which is placed inside the square brackets (or argument) of the N function.  Round brackets ( ) or  curly brackets { }  will not work for the argument of N.   These kinds of brackets have other uses in Mathematica .

Exponentials of numbers are obtained using the "hat" symbol (use the shift and 6 key) as commonly used in other programing languages.  Thus "5 squared" (or 5 raised to the 2 power) is obtained thus

5^2

Alternatively, using the BASIC MATH PALLETE can be used to generate the template

(\[Placeholder]^\[Placeholder])

This can be filled in, for the same example as before

(5^2)

This notation is closer to the usual mathematical notation for exponentiation.

Some useful irrational numbers and constants are built into Mathematica.  The ratio of the circumference to the diameter of a circle,  π       is given the symbol  Pi  in Mathematica  (note the capital P and small i).  If one just types Pi, then Mathematica  responds

Pi

since Mathematica treats Pi as exact for purposes of calculation.  The N function can be used to obtain a decimal approximation to  π

N[Pi]

The accuracy of the decimal approximation can be increased by adding a second argument to the N function.  For example, if you wish Pi  to 80  figures then

N[Pi,80]

The backslash ,  \  means the digits are continued on the next line.  The usual hand calculator cannot do calculations involving this level of precision. The level of precision is the number of digits in the decimal appoximation of a result.  Mathematica treats Pi as exact so the level of precision

Precision[Pi]

is infinite.  The "display precision" is set by the user of Mathematica but by default is it 6 digits.  For example,

N[π]

N[π,8]

has six digits of "display precision" (even though internally, Mathematica treats π is having infinite precision).  The default "display precision" can be set by the user with the menu links

Mathematica >> Preferences >> Advanced>>Open Option Inspector >> Global Preference >> Formatting Options >> Expression Formatting >> Display Option >> Print Precision

The greek letter for Pi, namely π is also available (in the more recent versions of Mathematica starting with version 3.0).

The natural number e is also available on the BASIC MATH ASSISTANT palette. Clicking this icon yields e which looks like an e with a vertical bar (to distinguish from the usual e used in English).  The decimal approximation to e  should be familiar

N[E]

Older versions of Mathematica use the capital E instead of e.

The square root function is also available in Mathematica either as Sqrt[ ] or on the BASIC MATH ASSISTANT palette.

Sqrt[12.]

Note the capital "S" in the function "Sqrt" and the argument of the Sqrt is enclosed in square brackets just as the argument of the numerical function, N, discussed before.  Finally, note the decimal point at the end of the number 12.  If instead you use

Sqrt[12]

This is correct but not too useful for situations where you need a decimal approximation.  With Mathematica you can follow one function with another and get a result.  For example,

N[Sqrt[12]]

Clicking on the square root icon yields the template

(Sqrt[\[Placeholder]])

which for appears, when filled in for the previous example, as

(Sqrt[12.])

Note the decimal after the 12 which "forces" Mathematica to make the decimal approximation.

The other usual trigonometric functions are also available in Mathematica.  For example, the sine function applied to an angle π/4  (measured in radians) is

Sin[π/4]

a result which is also exact since π/4 has not been approximated.  The sine of an angle measured in degrees (say 30 deg) is obtained by the conversion

Sin[30*π/180]

Notice a calculation can be performed in the argument of the sine function and also note the answer is exact, that is the fraction 1/2.  A decimal approximation to this result can be obtained by applying the N function after applying the sine function

N[Sin[30*π/180]]

The ability of Mathematica  to take a function of another function is quite useful and we use it often.  Reversing the order of the Sin and N functions results in the same numerical answer as before as you can easily verify by trying Sin[N[30*π/180]].  Also, the conversion factor π/180 from radians to degrees can also be obtained using the symbol ° for degrees in the middle of the Basic Input Palette so that

Sin[30.°]

(making sure there is a space between 30. and  the symbol ° ) and also the following works

Sin[30.Degree]

and again make sure you use a space between 30. and Degree.

Assignment of a Numerical Value to a Symbol:

Numerical values can be assigned to variables by the simple procedure

b = 6

Now b (inside Mathematica ) has been assigned the numerical value 6.  You may wish to suppress the Out response of Mathematica, in which case place a semi-colon at the end of the assignment

b = 6;

This suppression is useful in a program where some kinds of output may be confusing.  
     You may at times be confused about what value Mathematica  has assigned a variable so when in doubt, just question Mathematica  using a ? (question mark) before the variable

?b

Mathematica  answers that b is defined globally (i.e. throughout Mathematica ) and that b has the value 6.   You can easily change the value assigned by Mathematica  to a variable.  For example,

b = 5

If you use ?b, then Mathematica  indicates it now assigns the value 5 to b.  Alternatively,

Print[b]

Sometimes it is useful to indicate some information about the variable printed using

Print["b = ", b]

This print statement has two arguments "b = " and the variable b separated by a comma.  The information to be printed by Mathematica  about b is enclosed in quotes (the quotes themselves are not printed).

Decimal and Integer Numbers

Mostly you will want Mathematica  to make decimal approximations.  In these cases, it is important you include a decimal when you define a symbol (e.g, x) for the first time.  For example, x=6. instead of x=6.  This rule also applies to the number zero, which should be input as 0.0 or 0. but not just 0.  
There are a few cases where n, an integer is used in some calculations to avoid roundoff errors.  So there are some cases where integer calculations will be used.

Scientific Notation

When dealing with numbers that are either very small or very large, scientific notation is preferable to simple decimal notation.  For example, the number 7.5 109  is assigned to z with (note the star * for multiplication and the caret ^ for the exponential)

z=7.5*(10^9)

Alternatively, you could use the BASIC MATH ASSISTANT palette and write something quite close to the usual mathematical notation

z=7.5 (10^9)

Just make sure to be careful and [DO ONE OR NEITHER] place a space (or alternatively a "star", *) before the (10^9) otherwise Mathematica will not understand properly what you mean.

Another example, assigning y to 3.4 10-7 is

y=3.4*(10^-7)

Do not include comma separators (e.g. 1, 251.) as this will give an error message

1,251.

Syntax::sntxf: "1" cannot be followed by ",251.".

Continuation of a Line

On occasion you will have a calculation continue onto a second line.  For example,

2.35 + 4.86 + 3.95 +
    9.80 + 6.72

Notice the last character on the first line is the addition symbol +  This indicates to Mathematica  something is to follow on the next line which is considered part of the first line.  If you wrote instead

2.35 + 4.86 + 3.95
    + 9.80 + 6.72

then Mathematica  treats the two lines as separate additions with one result 11.16 and the other result 16.52  Generally end a line with punctuation (e.g. a comma + ) if you wish the next line to be a continuation of the first in Mathematica .

Capital and Small Letters

Mathematica  knows the difference between the capital letters and small letters.  For example, make the assignment

y=3.0;

Then ask Mathematica  what it knows about y.  You obtain

?y

However, ask Mathematica  about capital Y and you get

?Y

Also, be careful about typing and using the capital letter O, the small letter o, as well as the number zero 0 because Mathematica  knows the difference!

How to Define a Function in Mathematica

It is often useful to define functions (beyond those built into Mathematica ) yourself.  For example, the (straight line) function f[x] is defined using

b = 5;
f[x_] = 7*x + b

The bar after the x in the argument of the function is required by Mathematica  for the "type checking" feature of Mathematica  and since we will not use this feature, it will not be discussed here.  The bar is  obtained by holding down the shift key together with the minus key (at the upper right hand corner of your keyboard). Just be advised you must include the "bar" following the x or the function definition will not work with Mathematica.  To see how f[x] works, evaluate the function f[x]  at  x=2.0  and obtain

f[2.0]

You can ask Mathematica  for information it has about f[x]  in the usual manner

?f

If you change the value of b to say b=10, then the function f[x] does NOT change when you evaluated f[x] at the same point x=2.0

b = 10;
f[2.0]

You can also verify that the meaning of f[x] has not changed by doing ?f.

?f

Deleting the Definition of a Function:

You can delete or erase the definition of a function (you previously defined) using

Clear[f]

If you then ask Mathematica  about the function f[x],  Mathematica  reports it no longer has f[x] defined

?f

This is almost always a good thing to do at the beginning of a program for functions and variables you plan  to use.  The only exception is a case where you wish to keep a previous definition or assignment.

Another Way to Define a Function in Mathematica

There is another very useful way to define a function. Suppose you define a function g[x] which is almost the same as the definition of f[x] above

b=10.;
g[x_] := 7*x + b

The only difference is the  : or colon to the left of the equal sign.  The value of g[x] at x=2.0 is (recall b=10 now)

g[2.0]

The new definition is different however,  If you change b to the numerical value b = 15

b=15.

then the value of g[x] at x=2.0 changes as well

g[2.0]

Recall changing the numerical value of  b after f[x] was defined did NOT change the value of f[x] at x=2.0  we will define functions like g[x] with a : colon  more often than not, as we will quite often want the ability to change a parameter like b in the definition of a function.

Most of the time, we will define functions with a : colon, like we defined g[x].  Then we can see how a function's output changes when we change a parameter (like b).

The Meaning of the Equal Sign =

Mathematica  (and other programming languages) use the equal sign for the following operation:  Take the numerical value of what is on the right side (of the equal sign) and assign it to the symbol on the left side (of the equal sign).  This is also how the equal sign is used to define a function.  The arrow key  ->  might be more appropriate in programming languages than = but the arrow keys have another use (i.e. to move the cursor around on the screen).  
     The equal sign as used in programming is different from the meaning of the equal sign in algebra.  The equal sign in algebra is a sort of symbolic pivot on a balance.   What is on the right of the equal sign is the same as what is on the left and what is on the left is the same as what is on the right.  A two headed arrow ↔ would perhaps be a more appropriate symbol for the algebra equal sign in programming.  Instead Mathematica  uses two equal signs together == .  
     If the above discussion of the equal sign seems a bit esoteric, then please ignore it for now.  However, please keep in mind this will be important in some applications and there is a special key for == in the BASIC INPUT palett (see by the arror in the figure below):

Two-dimensional Graphs with Mathematica

It is simple to plot either a built-in function or a user defined function.  For example, a plot of Sin[x] over the range 0 < x <  2π  is done

Plot[Sin[x],{x,0,2 π}]

The Plot function has two arguments separated by a comma.  The first argument is the function f[x] to be plotted while the second argument is the range of values of x. In the future,  if you forget the proper syntex, just ask Mathematica  for help using the function below:

?Plot

Plot is a function, so following Mathematica  convention the first letter is a capital P.  The option GridLines->Automatic  (notice the capitals G and L as well as A) makes the function appear as drawn on graph paper

Plot[Sin[x],{x,0,2 π},GridLines->Automatic]

Having the grid is useful when you wish to estimate numerical values of the graphed function.

A Mathematica  graph may be assigned a label so it may be referred to later.  For example, you can assign the label graph1 to a graph via

graph1=Plot[3*x+7,{x,-3,7},GridLines->Automatic]

So graph1 now refers to the graph of a straight line of slope 3 and intercept 7.  (You can give your graph almost any name.)   Recall that the function g[x] defined above is a straight line of slope 7 and intercept b=15  (if you followed example above).  Mathematica  can be used to graph g[x] using

graph2=Plot[g[x],{x,-3,5},PlotStyle->Dashed,GridLines->Automatic]

The graph of the function g[x] is associated with the label graph2.  Also g[x] has been plotted as a dashed line using the PlotStyle->Dashed option.  This usage of Plot has three arguments or optioins, each of which is separated from the others by a comma.  
      Both graph1 and graph2 can be displayed at the same time using the function Show

Show[graph1,graph2]

Important Rule When Using Mathematica

AT THE BEGINNING OF CLASS, QUIT ALL APPLICATIONS RUNNING ON YOUR COMPUTER BEFORE STARTING MATHEMATICA. THIS WAY YOUR COMPUTER WILL BE ABLE TO DEVOTE THE MAXIMUM MEMORY TO MATHEMATICA CALCULATIONS.

Exercises You Can Do with Mathematica

CONSIDER "QUIT AND START KERNEL" INSTEAD OF BELOW

You should quit and restart Mathematica  before beginning these exercises.  This procedure will clear any previous definitions of functions and assignments you have made previously if you have worked through the examples in this text.  If you have a question about how a particular Mathematica  function works, just try a simple example where you know the answer.  You should do this even before consulting Mathematica 's help.

Put the answer in the space just below the exercise.  Your answer should contain a numerical result AND a sentence or two explaining what happened.

1. Use Mathematica  to do the multiplication 25.25 * 89.2   Also have Mathematica do the addition  32.2 + 75.1

2. Calculate Sin[2 π] .  Calculate Cos[2 π] .  Calculate Tan[(π/2)] .

3

3. Calculate the Sine of the angle 35 degrees.  Make sure you convert degrees to radians in the argument of  Sin using either Sin[35.*((π/180))]    or   Sin[35.*Degree]    Do you need  parenthesis?  Does  Sin[((35.*π)/180)] give the same answer?  Try it and see what happens.

4. Obtain the numerical approximation of (Sqrt[15]) using Sqrt[15] and either use  (1) a decimal and/or (2)  do it a second time using the numerical approximation function N.

5. Obtain the six figure numerical approximation of the natural number, e which is available in Mathematica  as the symbol, e or  E.  Obtain   e  to 16 digits of accuracy.  Try the letter e as well and see what if anything happens.

6. Plot the function Tan[x] for the range of values 0 < x < 2 π.   (Do not forget a space or "star" between the 2 and the π.)  Add the option GridLines->Authomatic and graph the function again.  Add the option PlotStyle->Dashing and graph the function again.

7. Plot the natural logarithm function Log[x] for the range of values 
0 < x < 10.  (Mathematica uses "Log" for the natural logarithm instead of the mathematical notation "Ln".  If you want the "Common Logarithm use Log[10, x]  where the base 10 is separated from the number x by a comma.)

8. Plot the function g[x_] := 1/x  for the range of values 0 < x < 5.  What is the name of this function?  Evaluate the function g[x] at x=0 and report what happens.

9. Define a new function f[x_] := 2*Sin[x]. Test your function f[x] to make sure it gives a number when you evaluate say f[3.] for example.   Plot f[x] using a dashed line. Assign the label name trigplot1 to this graph and do not forget to redo the graph so Mathematica knows you have assigned the name trigplot1 to it (Just typing does not enter the information into the Mathematica Kernel or "computational engine" of Mathematica.  You must select the cell bracket and hit "shift" together with "return" before this happens.)  Next plot the Sin[x] function using a solid line and assign this graph the label name trigplot2.  Use Show to plot both of these functions on the same graph axes.

10. Ask Mathematica  for the information it has on the definition of the function f[x]. (Assuming you just did exercise 9.) What did Mathematica say?

© Prentice Hall Publishing and  Rodney L. Varley (1996, 2000-2003, 2008, 2009).