Introduction to Computational and Scientific Programming in Mathematica

Original Class based on Maple V  by Joseph Zachary
 Copyright 1993 Joseph L. Zachary.  All rights reserved.

Translated to Mathematica Notebooks 
by Thomas L. Marchioro II

Other Contributing Authors:

J. C. Diaz, University of Tulsa
Frank Hagin, Colorado School of Mines
Eric Eide, University of Utah
Chris Johnson, University of Utah
Ken Parker, University of Utah

Working with ``notebooks''

Before you can read through this notebook,  you need to know several things about ``getting around'' inside the notebook window.  This section shows you how to use the mouse to ``scroll'',  and how to select, change and delete text.

Learning how to ``scroll'' through this notebook

The mouse is the small rectangular box that is attached to the keyboard.  By sliding the mouse on a smooth surface (such as a  pad), you can control the movement of the cursor on the screen.  You move the cursor to point to items you want to manipulate.   There are two "buttons" on the mouse, either of which will  (unless altered)  work for your purposes.

Scrolling by using the arrows

You can use the mouse and the direction arrows at the bottom left (or right, depending on your system) of the window to scroll up or down through the tutorial:
   1.  Point the cursor on the `down' arrow that appears at the extreme lower 
         left corner of  this notebook window,
   2.  Click the mouse button once (the text will move up one line in the 
         window);  or simply
   3.  Press (and hold down) the button until the desired amount of new text 
         appears.
You can similarly scroll up,  or backwards,  by using the `up' arrow.

Scrolling by using the `scroll bar'

There is a faster way to move up and down.  Inside the grey margin strip at the left (right) of the window is a short vertical bar  (the scroll bar).  You can use this bar to scroll quickly down, or up,  the page:
    1.  Point the cursor on the scroll bar, 
    2.  Press the button (hold it down), and 
    3.  Drag the scroll bar down or up to correctly position the text.

More about the mouse and the cursor

You will notice that the cursor changes shape as it moves about the screen.  If you move the cursor from a position inside this window to a border of the page, or off the page,  the cursor will change from a vertical I-beam shape to an arrow.   Or,  if you slowly move down the page the cursor will occasionally change, briefly,  to a horizontal I-beam shape.   (This occurs between "cells"s,  where a cell is a block of text defined by the bracket,  ] ,  to the right---e.g. see the  ]  for this paragraph).   Move the cursor around and see it change.

The cursor has three basic shapes:
   ·  a vertical I-beam, for selecting text in the window; 
   ·  a horizontal I-beam, for opening more work space between paragraphs 
       (or cells);
   ·  the arrow,  for pointing to items bordering,  or off,  the window.
You will have occasion soon to see the implications of these various shapes.

Adding new text

You can change (edit) text by using the mouse.  You can practice adding text right now: 
    1.  Move the cursor to the end of a  nearby paragraph, 
    2.  Click the mouse -- a blinking vertical bar will appear, 
    3.  Add new text there by typing something;  e.g. type:   `Some new stuff' 
          after this sentence.

Deleting text

Here is how to delete the text added above:
   1.  Move the cursor to the beginning of your added text,  just before the "S" 
         in Some , 
   2.  `Select' the text  (i.e.  press and drag the mouse to the right until the  
         `Some new stuff'  is completely shaded in grey), 
   3.  Hit the Delete key (located on the upper right of the main keyboard, with 
         the left arrow).  The shaded text will disappear.

By combining the above features, moving about, adding and deleting text, one can change the text as desired.   You will get more practice below.

Exiting a Mathematica Session

How to continue this Tutorial

Below you will a sections titled  ``What is Mathematica?'' and ``Some Basic Elements of Mathematica'' as well as a subsection titled ``How to exit this tutorial'' which serve as a ``table of contents'' for the rest of this tutorial.  These sections are ``closed up'' for convenience in finding the section of interest.   For example,  if you needed to get off the computer now,  you would want to ``open up'' the sections  ``How to exit this tutorial''.   But,  let's assume that you want to open the next section,  ``What is Mathematica?''  To do this:
  1.  Put the cursor on the small rectangle to the right of the section title;
  2.  "double-click"---that is,  make two short clicks of the mouse.  
       This  (short) section should open up for you.   Try it.
  
If you want to close up that section,  double-click on the  ]  to the right, enclosing the entire section,  including the title.  Try it.

Notice the next section,  ``Some basic elements of Mathematica'', has a larger rectangle following it;  this is because it is a much larger section.  Open this section up when you are ready to start learning Mathematica.

How to exit this tutorial

To exit this tutorial, you move the cursor to the Mathematica menu located at the extreme upper left corner of your screen (On Apple Macintosh systems there will be a pull down menu at the upper left).  Point the cursor and click on the "Quit" rectangle at the bottom of the  menu .   You will be presented with a  "Quit"  box with some options; click the  "Quit Anyway"  box.

What is Mathematica?

Mathematica is a state-of-the-art and remarkably powerful system for doing mathematics by computer.  One can use Mathematica in many different ways, but in your the present course you will use only a small portion of its capabilities.   (You will use more advanced features in your later course work and,  very likely,  in your professional career).  The following tutorial sections will show you how to use to do numerical, symbolic and graphical calculations.   You will get a sense of the graphical power in this tutorial.
 
One of the remarkable features of Mathematica  is its `symbolic' capabiltity;  i.e.  its ability to very quickly do messy algrebraic computations and simplifications.   Such packages can thus save us many tedious hours of (error-prone) work.   Similarly, you will see later this term that Mathematica is also very good at calculus operations  (differentiation and integration), and also can be used as a full fledged programming language.  
   
These time saving features will not take all the work out of a scientific programming course,  rather it will allows us to probe more deeply into central issues of the subject.

In addition, Mathematica comes with several ``front ends'' to the calculational kernel. These front ends are designed to make the use of Mathematica both easy and powerful.  The notebook which you are currently reading is an example of such a ``front end''.  Mathematica notebooks run on NeXT computers, X-windows workstations, Macintoshes, and IBM PCs and compatibles.  The notebook allows you to intertwine graphics, sound, text, and Mathematica  commands in one place.  Later in the class we will be studying programming in the C and Fortran languages; although

Some basic elements of Mathematica

In this section we give you examples of how you can use Mathematica for some familiar and elementary operations. Many of these operations will be mysterious to you at this point, do not let that bother you! As the term wears on you will become familiar with Mathematica's syntax, and you will also learn the basics of scientific programming, and these examples will seem quite elementary.  For now they are just meant to give you a brief introduction to the power of modern symbolic manipulation packages such as Mathematica.

Executing a Mathematica command

In the first example, we illustrate some of the algebraic and graphical capabilities of Mathematica.   Here is an example of an expression that defines  y, a function of  x:

y = x^3 - x^2 - 9 x + 9

The above line is called a command, or "input".  When you enter such an input, Mathematica processes it and returns "output" (or a result). To make things easier for you while you are starting off, all Mathematica input statements  in this chapter will have a box around them. That is not usually done in notebooks and we will do so only in this chapter andthe next one.  For now the boxes should prove helpful. Click anywhere in the box above and hit the ``Enter'' key to execute the Mathematica input line. (see below)

 To obtain the result you must first "execute" the command by following these steps:
   1.  Move the cursor to the end of the command;  in this case, after the 
         second  9.   
   2.  Click the mouse to select the command,
   3.  Hit the Enter key,  located on the extreme lower right of the keyboard.  
        (Note: this is  NOT the same as the Return key that you have been 
         using.)
	
The above command is repeated below.   Follow the directions above to execute it now.  There will be a short delay while Mathematica performs the command. Once the command is completed, you will see an input line labeled  In[1]  that contains the original command, and an output line labeled  Out[1].  Notice that the result in the output line is written differently than the way we typed it;  Mathematica  prefers to write polynomials in increasing powers of  x.

y = x^3 - x^2 - 9 x + 9

Writing a Mathematica command

The input line above is an example of writing the definition of  y  as the cubic polynomial in  x.  There are two items you should notice in the above command that are peculiar to the  Mathematica  program:
   ·  "x cube"and "x square" are typed using the carat (^ ) . 
   ·   product  "9 x" does not need a multiplication symbol.  Two possible 
        forms are:   with a space as in "9 x" or  with an asterisk as in "9*x".

Opening Mathematica work space

Now it's time for you to practice writing a command in Mathematica.  
However, first you must `open' a line to type in as follows:   
   1.  Move the cursor slowly down between two paragraphs  (e.g. between 
         this paragraph and the next one)  until the cursor becomes a  horizontal 
         I-beam.
   2.  Click the mouse;  a grey line will appear across the window.

For practice, after opening a line,  type the same Mathematica  command that we worked with above, and execute the command.  (Remember:  to execute a command,  first `select' it by clicking behind the command,  and then hit the Enter key.)

Plotting a function

The command below is a very common plotting format that will soon become familiar to you.  (Note: this plot command assumes the above definition of  y;  if you have not executed a command defining  y,  scroll back and do it now.)

Plot[y, {x,-7,7}, PlotRange -> All]

There are some items to note in this plot command that are peculiar to Mathematica:
   ·   Notice the square brackets,  [  and  ],  around the arguments of the Plot 
        command.  Also note {x,-7,7}   specifies the domain of  x.   It is 
        important to remember when to use the different types of brackets.  
        Mathematica is very `narrow minded' about this.
   ·  "PlotRange -> All" is a plotting option, telling Mathematica that---"yes, 
        we do want to see the entire graph".  (Other plotting options will be 
        introduced as we need them.)   The arrow (->) is created with the 
        "hyphen" followed by the  "greater than" keys on the keyboard.
   ·  Notice the upper case letters in  Plot  and  PlotRange.  This is typical of 
       all Mathematica  functions and options;  so get used to this!  In contrast, 
       if  YOU define a new function, like  y  above,  you are free in the use of 
       upper and lower case letters.

Execute the plot command below to see what the graph of  y  looks like. (Select the command by clicking after it,  and hit Enter).

Plot[y, {x,-7,7}, PlotRange -> All]

You can see that the graph depicts the essential character of function y.

Modifying a command

You can now practice modifying a Mathematica command and, at the same time, get a closer look at the graph.

Follow these steps to change the domain of  x in the plot command below:
   ·  Carefully place the cursor in front of the  -7;
   ·  Press and drag the mouse across the -7,7 (it will be shaded grey);
   ·  Type  -4,4  (the  -7,7  will be replaced).
Now execute the modified plot command. (If the command does not execute, it's always a good idea to check for typographical errors. If there are no errors  and the graph still does not plot, ask for help.)

Plot[y, {x,-7,7}, PlotRange -> All]

You will now see a more detailed graph of y where it crosses the x-axis.

Factoring polynomials

In the above graph it looks to the eye as if the function y is equal to 0 somewhere around x = -3,  x = 1, and  x = 3.  How to find where  y=0 is an important question that we will pursue from several directions this semester.   But in this case, the question is easily answered because Mathematica is good at factoring polynomials.   To see this, execute the following command.

Factor[y]

It is now clear that y = 0 precisely at  x=-3,  x=1, and  x=3.

Zooming in on a graph

Let's return to the graph of y and get into an issue that will be very important throughout our study of calculus.   Again,  execute:

Plot[y, {x,-4,4}, PlotRange -> All]

A recuring theme in calculus can be paraphrased:  "most functions are `almost linear' if you look closely enough".  To illustrate this point, you can "zoom" in on the graph at  x  near  2: 
    1.  Modify the plot command below so that the domain of x is 1.9  to  2.1.   
         (If  necessary, you may scroll back to review the directions  "Modifying 
         a  command.")
    2.  Execute the plot command.

You should see a smaller section of the graph and note that it is starting to straighten out a bit.  To further illustrate the point,  modify the x domain in the plot command below to 1.99 to 2.01 and execute the command, and notice that the graph is almost linear.

Plot[y, {x, 1.99, 2.01}, PlotRange -> All]

Is there anything special about the point  (2, -5)?  No.   You may want to experiment on your own by modifying the command with any value for the domain of  x that you choose, and "zoom in" as we have done.  Remember the fundamental point being made:
     "most functions are `almost linear' if you look closely enough".
We will revisit this notion often in Calculus.