Acknowledgements
Parts of the material in this section was taken from a number of sources, which are listed below
Mathematica 4.0, S. Wolfram. Mathematica User's Guide to the X Front End, Wolfram Research. Mathematical Modelling and Mathematica, J.E. Cruthirds and L.R. Hitt, Department of Mathematics and Statistics, University of South Alabama, material taken from the Web. An introduction to Mathematica, N. Blachman, material taken from the Web.
Introduction and demands for Report
This notebook will give an introduction to Mathematica. It gets you acquainted with a number of Mathematica. functions, and introduces the user interface.
At the end of the notebook you will find a number of exercises.
A Introducing Mathematica
A.1 The Mathematica Front End and Kernel
A.1.1 The Mathematica environment
Mathematica is a powerful desktop computer program capable of doing algebraic calculations, numerical approximations, computer graphics, and much more.
Mathematica consists of two parts, the Kernel, which is the computational engine, and the Front End, which acts as the user interface to the Kernel. The Mathematica Kernel is essentially identical on all computer systems. The Front End may differ from one (operating) system to another. Mathematica can be operated in many ways. The Kernel and Front End can both run at the same computer, or the Kernel may run on a powerful server with the Front End running on your local host. If a computation must be carried out, the Front End contacts the Kernel, e.g. through the network. Although the details of running Mathematica differ from one place to another, the structure of Mathematica calculations is the same in all cases. You enter input, then the Mathematica Kernel processes it, and returns the result.
You will be using the Notebook Front End, which is available on many systems (Macintosh, Windows, Unix, etc.). Section 1.2 will discuss the Notebook Front End in more detail.
During the lab session you will be using Mathematica 3.0, which is the most recent version. In our local WINS environment we have a campus wide license. During the lab work you will have both the Kernel and the Front End running on your local workstation.
A.1.2 The Notebook Structure, Starting and Using the Kernel
The Notebook
Mathematica supports a special Notebook interface on most computers with a graphical user interface. In this section the structure of this Notebook is shortly explained and the Notebook Front End is introduced. Finally the use of the Kernel is explained, by starting the Kernel from the Notebook Front End and telling it that is should calculate 1+1 for us.
Currently you are reading a Mathematica Notebook. A notebook is a file containing text, input to the Kernel, and results of calculations, in textual or graphical form, all organised in a hierarchical way. A Notebook can be viewed as a folded piece of paper, which can be unfolded at will. The folds in the file are called cells. Cells can enclose other cells, and may be opened or closed at will. The cell structure can be controlled by the vertical brackets on the right of this window. The hierarchical structure of a cell group is indicated by the layering of the cell brackets.
Mathematica groups cells automatically on the basis of the cell styles (unless you turn this off, using the Cell Grouping features in the Cell menu). For instance, a subsection is automatically grouped with text cells below the subsection cell, as drawn below. The text cell and Sub Section cell are both enclosed by a bracket, and one level up, again by a next bracket, indicating that both cells are grouped. As you see on the right of this Notebook, there are more than just two brackets, indicating higher levels of grouping of the cells.
![[Graphics:Images/appendix_gr_2.gif]](Images/appendix_gr_2.gif)
Using the concept of cells containing certain types of information and grouping of cells, we can now take a look on folding up, or closing the cells. A closed cell is marked by an arrow at the bottom of the grouping bracket. By double clicking on such bracket, the closed group is opened, i.e. unfolded, and the contents is shown. Below, an example of such a closed cell is shown. Now open this cell and continue reading inside that cell.
![[Graphics:Images/appendix_gr_3.gif]](Images/appendix_gr_3.gif)
a closed cell
This is an example of a closed cell (which off-course you just opened), containing text and some graphical output. Under the Style submenu in the Format menu you can assess the style of each specific cell. If you select the cell that you are currently reading, by clicking inside the cell or selecting the cell bracket (click on it once), and look into the Style submenu you will notice that it has the style Text. As you see, the Front End has a large number of predefined cell styles. If you start using the Notebook for the lab exercises you can experiment with the cell styles to create nicely formatted Notebooks, which can be handed over as lab journals. Also notice the StyleSheet submenu in the Format menu which has a large collection of predifined style sheets. Now find out about the cell styles of the three cells you see below. Also notice the special bracket symbols used for these cells.
![[Graphics:Images/appendix_gr_4.gif]](Images/appendix_gr_4.gif)
![[Graphics:Images/appendix_gr_5.gif]](Images/appendix_gr_5.gif)
It will by now be clear that you can also close cell groups by double clicking on the enclosing brackets. Note that if you do so, only the head cell of the closed group is visible. In the rest of this Notebook all sections and subsections are closed cells. To read them they should be opened. Closing all sections and subsection (you could do that now) results in an overview of the contents of the Notebook. You can use the Notebook to write your own (interactive, as will become clear later on) lab journals.
The contents of (text) cells can be edited as usual (i.e. inserting text, copying, cutting, pasting, etc., see the Edit menu). Complete (grouped) cells can also be copied and pasted in a Notebook. First, select the cell you want to copy by clicking its enclosing bracket, then choose Copy from the Edit menu, move the cursor to the place where you want to paste the cell. This can only be at places where the cursor changes in a horizontal bar, click (a horizontal line appears), and choose Paste from the Edit menu. As an exercise, try to copy this complete cell to another position in the Notebook.
More on Cell Properties
The properties of Cells control the display of material in your notebook, and also tell Mathematica how to treat particular cells when you perform an evaluation. Cell properties are controlled through the FormatType sub menus and the Cell Properties sub menu under the Cell menu. One of the most important properties of a cell is how it is formatted and if you can edit the cell. For instance, if you select this cell, and look under the Display As sub menu you will notice that this cell is displayed as text. We will returrn to the FormatType of cells if we discuss Input and Output in Notebooks in more detail in section 1.4.
The cell below is graphical output from Mathematica. Such output is generated by the Kernel as postscript, and the Front End renders it. You can see the underlying postscript code by selecting the cell, and choosing Show Expression from the Format menu. Actually, just before the postscript code you see some Mathematica functions, which are used by the Front End to display the total cell in a correct way. Actually, the overall notebook file is a plain Ascii file only containing such code. In this way the notebooks are fully portable among different computers. Choosing Show Expression once more brings back the rendered picture.
![[Graphics:Images/appendix_gr_6.gif]](Images/appendix_gr_6.gif)
A cell can either be Evaluatable or not (as is seen in the Cell Properties sub menu). An evaluatable cell is one that can be evaluated by the Mathematica Kernel, and usually contains mathematical expressions. Non-evaluatable cells contain material that you would ordinarily not want to evaluate, such as explanatory text (as in this cell), as well as Mathematica output. Non-evaluatable cells are marked with a horizontal tick mark near the top of their cell brackets, like the bracket of this cell.
Below is an example of an evaluatable cell, which is recognised by the plain bracket with the little triangle in the top. Checking the Style sub menu shows that it has the style Input. Also look at the cell properties. To evaluate the expression in the input cell (in this case 1+1) you select the cell, or click somewhere in the cell. Then, select from the Kernel menu Evaluation and Evaluate Cells, or faster, just press Shift-Return. As this is your first evaluation, the Mathematica Kernel is not running. So, after you pressed Shift-Return the Mathematica Kernel automatically starts up (this may take a while). For next evaluation the Kernel is already running. Next, the expression in the cell is sent to the Kernel, which evaluates it and sends the result back to the Front End. New cells below the input cells will be created automatically to place the results. Ok, just try to do this!
![[Graphics:Images/appendix_gr_7.gif]](Images/appendix_gr_7.gif)
![[Graphics:Images/appendix_gr_8.gif]](Images/appendix_gr_8.gif)
You should notice a few things. First, Mathematica assigns in sequence a number to each of the cells it evaluates, which is useful to address previous results in a longer calculation. Next, the output cell that was created automatically, and the input and output are automatically grouped. Notice again the bracket for the output cell. Now that you know about addition, try to enter, immediately below this cell, another expression (using the symbols *, /, or +) and evaluate it. Just move the cursor down, until it becomes a horizontal bar, click (now you see a line) and start typing your expression. Notice that by default the cell style is an evaluatable input cell. Notice also how Mathematica keeps track of the numbers of evaluations, how the output is organised, and play around with the difference between formatted and unformatted output.
Now you know how to use Mathematica as a calculator. In section 1.3 you will find some useful information about the on-line help facilities in Mathematica. In the following sections you will examine the mathematical and graphical capabilities, and also take a look at programming capabilities of Mathematica.
A.1.3 Getting help in Mathematica
As will become clear Mathematica is a very large system, containing an enormous amount of functions (in the order of 1000). Furthermore, the Front End has many features. All information can of course be found in the user manuals and the Mathematica book (that will be present during lab hours). However, Mathematica has an extensive on-line help facility, which will be of utmost importance to help you through this lab work. A full listing of the syntax and use of all functions and add-on packages is available. Extensive demos and tours through Mathematica are available, and the full Mathematica 3 book is available on-line. However, another very useful help facility is Why the Beep? If something goes wrong, Mathematica will beep. To find out what happened, choose the Why the Beep? command from the Help menu....
The Mathematica Help Browser will become an important companion for you. Choose Help in the Help menu. The help browser now pops up. You can get information from 6 different sources,
Built-in Functions Getting Started/Demos Add-ons Other Information The Mathematica Book Master Index
By clicking the radio buttons you select the appropriate source. The Built-in Functions explains Mathematica functions. Add-ons gives access to all standard Mathematica Packages. The Getting Started/Demos gives a wealth of information beyond what is covered in this Lab Work and is helpful as background reading. Other Information fully explains all Menu Commands of the Front End, and gives some other very useful information in using the Front End efficiently. The Mathematica book is the on-line version of the 1400 pages Mathematica 3 book. All you need to know, and much more, is in there. To look up a specific function, start typing its name in the Go To box, or type the function name in a notebook cell, select the function name (e.g. by doubl -clicking on it), and select Find in Help from the Help menu. The Help Browser is a very useful tool when working with Mathematica.
As an exercise, you want to calculate the logarithm to base 10 of 2.5, but forget the function name in Mathematica. You know that the logarithm is an elementary mathematical function. Use the help browser to find the correct function name and calculate the result (which is 0.39794).
• See also the Mathematica book: Section 1.3.8.
The blue thing is a hyperlink, just click it as you know from Web Browsers. They operate the same.
A.1.4 Input and Output in Notebooks
A.1.4.1 Introduction
In previous versions of Mathematica all input was purely text based and in terms of functions. For instance, to calculate a variable x to the power 2 would be expressed as
Power[x,2]
![[Graphics:Images/appendix_gr_9.gif]](Images/appendix_gr_9.gif)
or in shorthand, as
x^2
![[Graphics:Images/appendix_gr_10.gif]](Images/appendix_gr_10.gif)
The output is shown below.
2 x
The format of both input cells above is InputForm as can be assessed from the Display As sub menu from the Cell menu. The output is in OutputForm, as can be seen in the Convert To sub menu from the Cell menu. Also, more complicated formula can be built up using this InputForm and OutputForm. An example is shown below.
(x^2 + y^6 - Sqrt[z])/(x^3 + z^2)
![[Graphics:Images/appendix_gr_11.gif]](Images/appendix_gr_11.gif)
Of course, for many mathematical functions a standard mathematical notation exists, which is probably easier to use. Furthermore, if you are working with formula manipulations and you want to print or publish the results you also want to have nicely formatted formulas, according to the accepted standards. In Mathematica 3 this is possible, and is the default way of entering mathematical formulas (although the fully text based approach in the InputForm and OutputForm can also still be used).
Below you now see the examples from above in nice mathematical notation.
![[Graphics:Images/appendix_gr_12.gif]](Images/appendix_gr_12.gif)
![[Graphics:Images/appendix_gr_13.gif]](Images/appendix_gr_13.gif)
![[Graphics:Images/appendix_gr_14.gif]](Images/appendix_gr_14.gif)
![[Graphics:Images/appendix_gr_15.gif]](Images/appendix_gr_15.gif)
The format of the input and output cells is now StandardForm. Notice the difference in brackets between the InputForm and OutputForm formats and the StandardForm formats. Look at the different brackets as compared to the InputForm and OutputForm formats. By default, your input and output will be in StandardForm. This can be controlled by the Default sub menus from the Cell menu. However, we advise you not to change these settings.
Besides the differences in look, the use of the StandardForm has another important advantage. Both the input and the output can be edited. In OutputForm, the output cannot be edited. This is important if you want to build up calculations. For instance, just try to edit the output of the last example, by changing to (just select the 2 and change it into a 3). Notice what happens, the original output is automatically copied into a new cell, and your changes are effectuated in that cell. You can now evaluate the new cell.
Finally, Mathematica knows another format, the so-called TradionalForm. This format tries to imitate all aspects of traditional mathematical notation. It's great if you want to produce printing quality Notebooks. However, for normal use we advise to stick to the StandardForm. Below is an example of the previous formula in TraditionalForm.
![[Graphics:Images/appendix_gr_18.gif]](Images/appendix_gr_18.gif)
• See also the Mathematica book: Section 1.10.9.
A.1.4.2 Palettes
The easiest way to organise the input of mathematical formula, Greek letters, matrices, etc. is through the use of Palettes. Under the File menu choose from the Palettes sub menu BasicInput. You now see a palette window containing a number of buttons with predefined templates. By pressing the buttons the template is copied into the cell, and you can further edit the template to the final expression. Below is an example. Try to reproduce this formula in an input cell, and evaluate it.
![[Graphics:Images/appendix_gr_19.gif]](Images/appendix_gr_19.gif)
Notice that the templates work in such a way that if you already entered an expression, and if you select (part of) that expression, it is automatically pasted into the black position of the template. Experiment a little bit with the Palettes. Also take a look at the other Palettes. Finally, shift between the StandForm and the InputForm to see the underlying functions.
• See also the Mathematica book: Section 1.10.1., Section 1.10.2, Section 1.10.3, Section 1.10.4, Section 1.10.5, Section 1.10.6, Section 1.10.7, Section 1.10.8.
A.1.5 Repairing corrupted Notebooks
It has happened to a few students that their report couldn't be read by Mathematica after they had saved their work. Most often the cause of this is that Mathematica doesn't check whether you have passed your disk quotum. So please, always keep an eye on your disk usage. In any case, always make sure that you have a backup of your work, and that you save your work regularly. The safest strategy is to make a backup of your last version before starting Mathematica and making any changes: cp notebook.nb notebook.bak If something goes wrong if you save your work, during which you are overwriting your most recent version, at least you still have your backup. If, despite all your precautions, your file has been damaged, it may be possible to repair it. The following website contains a Mathematica package and an explanation that may help you to repair your work. ![[Graphics:Images/appendix_gr_20.gif]](Images/appendix_gr_20.gif)
A.2 Try this!
As an appetiser, and before entering into a less interesting (but on the other hand important to know) section on notational conventions, a first glimpse at the power of Mathematica is shown. More details and other functions are described in sections 4 and 5. Play around with what you see, find out more about these functions using the help browser, and do not hesitate to experiment with the functions! The motto in this lab work is "learning by doing"! To save (your) time, the input is already typed into input cells. All you need to do is evaluate the cells, see what happens, and learn from that.
Let us start with calculating 100 x 99 x 98 x ... x 2 x 1
![[Graphics:Images/appendix_gr_21.gif]](Images/appendix_gr_21.gif)
![[Graphics:Images/appendix_gr_22.gif]](Images/appendix_gr_22.gif)
Notice that Mathematica returns the exact result, something that your pocket calculator will not do. By the way, the "!" is just (well-known) shorthand for the function Factorial.
![[Graphics:Images/appendix_gr_23.gif]](Images/appendix_gr_23.gif)
![[Graphics:Images/appendix_gr_24.gif]](Images/appendix_gr_24.gif)
Usually it takes a lot of time to do lengthy algebraic calculation by hand, such as e.g. calculating (1+x) to the power 20. How much time would you need for that? Now let us see how Mathematica deals with that.
![[Graphics:Images/appendix_gr_25.gif]](Images/appendix_gr_25.gif)
![[Graphics:Images/appendix_gr_26.gif]](Images/appendix_gr_26.gif)
Feel like checking this result? Maybe you should try to do some simpler examples.
Do you hate doing integrals? Again, Mathematica is good at it. For instance, here is how to calculate the indefinite integral of (1+x)/(1-x)
![[Graphics:Images/appendix_gr_27.gif]](Images/appendix_gr_27.gif)
![[Graphics:Images/appendix_gr_28.gif]](Images/appendix_gr_28.gif)
To convince yourself of the answer, also do the integral by hand. Now differentiate the previous result (note that the % refers to the last obtained result) ...
![[Graphics:Images/appendix_gr_29.gif]](Images/appendix_gr_29.gif)
![[Graphics:Images/appendix_gr_30.gif]](Images/appendix_gr_30.gif)
...and simplify the result...
![[Graphics:Images/appendix_gr_31.gif]](Images/appendix_gr_31.gif)
![[Graphics:Images/appendix_gr_32.gif]](Images/appendix_gr_32.gif)
... resulting in the original expression.
Mathematica has many graphical functions (see more on this in chapter 4 and 5). As a start, look at this one dimensional plot ...
![[Graphics:Images/appendix_gr_33.gif]](Images/appendix_gr_33.gif)
![[Graphics:Images/appendix_gr_34.gif]](Images/appendix_gr_34.gif)
... and an example of three dimensional plotting. Notice the use of options (such as PlotPoints) to control the behaviour of the function Plot3D.
![[Graphics:Images/appendix_gr_35.gif]](Images/appendix_gr_35.gif)
![[Graphics:Images/appendix_gr_36.gif]](Images/appendix_gr_36.gif)
A.3 Notational Conventions
A.3.1 Built-In Names
The names of all functions, variables, options, and constants built into Mathematica start with capital letters, e.g. Integrate, Plot. If a name consists of two or more words, the first letter of each word is capitalised, e.g. PlotPoints.
Most names of objects built into Mathematica are complete words. Unlike Unix, MS-DOS, and other systems, Mathematica rarely uses abbreviations. Abbreviations are used only where they are extremely well-known. The following table contains examples of some of the abbreviations that are built into Mathematica.
Abs gives the absolute value of a number Cos gives the trigonometric function cosine D computes the derivative Det calculated the determinant of a matrix GCD computes the greatest common divisor
There are in the order of 1000 functions built into Mathematica. The names of the functions in general indicate the function's purpose. As an example, look at the following function names.
Eigenvalues gives a list of eigenvalues FindRoot searches for a numerical solution to an equation Integrate evaluates the integral Timing returns the time used in evaluating an expression
A.3.2 Mathematical Notation
Typically Mathematica uses English words. Most people are used to referring to standard mathematical functions with symbols rather than words, i.e. symbols such as +, -, *, /, <, and > rather than the words Plus, Minus, Times, Divide, Less, and Greater. The following table contains some of the mathematical symbols supported in Mathematica (in InputForm).
+ Plus - Minus, Subtract * Times / Divide ^ Power ! Factorial < Less <= LessEqual > Greater >= GreaterEqual
In mathematical notation, the product of a and c is commonly written as a c. Mathematica also understands this notation. The asterisk * denoting multiplication is optional. A blank character or space also denotes multiplication.
A.3.3 Brackets
Be aware that Mathematica has adopted slightly different conventions from C, Pascal, and Fortran. Parentheses are used for grouping, a single set of square brackets is used for calling or specifying functions and braces are used for lists, vectors, and matrices. Here is a summary of the different types of brackets.
bracket Purpose Examples
(term) Grouping (a + b)/(c + d) f[expr] Arguments of function Sin[2 Pi x] {a,b,c} Vectors, matrices, and lists {x, 2 x, 3 x} v[[i]] Indexing m[[3]], m[[1,2]] (* remark *) Commenting (* watch out *)
A.3.4 Using Previous Results
Here is a result.
![[Graphics:Images/appendix_gr_37.gif]](Images/appendix_gr_37.gif)
![[Graphics:Images/appendix_gr_38.gif]](Images/appendix_gr_38.gif)
The percent sign (%) gives the previous result. This input multiplies the previous result by 3.
![[Graphics:Images/appendix_gr_39.gif]](Images/appendix_gr_39.gif)
![[Graphics:Images/appendix_gr_40.gif]](Images/appendix_gr_40.gif)
Out[n] or %n gives output n.
A.4 Using Mathematica
A.4.1 A next look at Graphics in Mathematica
Mathematica has several built-in graphics functions that allow curves and surfaces to be graphed with relative ease.
First, we will do a 3-dimensional example like you might have encountered in your calculus course on functions of several variables.
![[Graphics:Images/appendix_gr_41.gif]](Images/appendix_gr_41.gif)
![[Graphics:Images/appendix_gr_42.gif]](Images/appendix_gr_42.gif)
Many of Mathematica 's commands have options, which can be specified. Here is a slight variation on the previous example using one of the options for Plot3D. Rather than having Mathematica re-computing all of the points on the graph, we can use the Show[%, options] to change the display options of the previous graphic.
![[Graphics:Images/appendix_gr_43.gif]](Images/appendix_gr_43.gif)
![[Graphics:Images/appendix_gr_44.gif]](Images/appendix_gr_44.gif)
Notice how the option AxesLabel effects the graph. Here is the same graph with a label.
![[Graphics:Images/appendix_gr_45.gif]](Images/appendix_gr_45.gif)
![[Graphics:Images/appendix_gr_46.gif]](Images/appendix_gr_46.gif)
For a complete list of the options available for Plot3D use the help browser.
Study the previous images to see how the axes are oriented by default in Plot3D. Can you see where the first octant (the region of 3-space in which each of the variables x, y, and z is positive) is located?
Another function of 2-variables we can try is given below. Note that this example, as well as the previous one, is slightly beyond the usual graphing done by hand in a calculus course.
![[Graphics:Images/appendix_gr_47.gif]](Images/appendix_gr_47.gif)
![[Graphics:Images/appendix_gr_48.gif]](Images/appendix_gr_48.gif)
We can modify the appearance of the above plot using some options.
![[Graphics:Images/appendix_gr_49.gif]](Images/appendix_gr_49.gif)
![[Graphics:Images/appendix_gr_50.gif]](Images/appendix_gr_50.gif)
For plotting 2-dimensional graphs of a function of 1 variable, the Plot command is used.
![[Graphics:Images/appendix_gr_51.gif]](Images/appendix_gr_51.gif)
![[Graphics:Images/appendix_gr_52.gif]](Images/appendix_gr_52.gif)
Can you see the two critical points and the two inflection points in the above graph? To aid in estimating the coordinates of points, we can add options to the above plot.
![[Graphics:Images/appendix_gr_53.gif]](Images/appendix_gr_53.gif)
![[Graphics:Images/appendix_gr_54.gif]](Images/appendix_gr_54.gif)
For another example, let's plot a curve with a skew asymptote.
![[Graphics:Images/appendix_gr_55.gif]](Images/appendix_gr_55.gif)
![[Graphics:Images/appendix_gr_56.gif]](Images/appendix_gr_56.gif)
Let's plot the asymptote in blue, and then superimpose the function and its asymptote.
![[Graphics:Images/appendix_gr_57.gif]](Images/appendix_gr_57.gif)
![[Graphics:Images/appendix_gr_58.gif]](Images/appendix_gr_58.gif)
![[Graphics:Images/appendix_gr_59.gif]](Images/appendix_gr_59.gif)
By the way, notice the use of two evaluations in one cell (the Plot and Show command) and how Mathematica organises the output.
Mathematica can also plot parametric curves in the plane. The following example is a well-known curve. Do you recognise it? Can you see the effect of the AspectRatio option?
![[Graphics:Images/appendix_gr_60.gif]](Images/appendix_gr_60.gif)
![[Graphics:Images/appendix_gr_61.gif]](Images/appendix_gr_61.gif)
We return to surface graphs for a final example. Many surfaces cannot be described as a graph of a function . A hyperboloid of 1 sheet is an example of such a surface. In order to graph these surfaces, you need to describe them using sets of parametric equations. Once you have a surface described as a set of parametric equations in two parameters, you can use the Mathematica function ParametricPlot3D to graph the surface. Cylindrical coordinates can be used to produce an easy parameterization of the hyperboloid described in rectangular coordinates by the equation
![[Graphics:Images/appendix_gr_63.gif]](Images/appendix_gr_63.gif)
![[Graphics:Images/appendix_gr_64.gif]](Images/appendix_gr_64.gif)
This example also showed how you can define your own functions in Mathematica. We will return to this point later.
A.4.2 Algebraic Computations
Mathematica has symbolic computing capabilities including algebra and calculus. Additionally, Mathematica can learn any computational rules you want to teach it.
Expanding and factoring polynomials can be done with
![[Graphics:Images/appendix_gr_65.gif]](Images/appendix_gr_65.gif)
![[Graphics:Images/appendix_gr_66.gif]](Images/appendix_gr_66.gif)
and
![[Graphics:Images/appendix_gr_67.gif]](Images/appendix_gr_67.gif)
![[Graphics:Images/appendix_gr_68.gif]](Images/appendix_gr_68.gif)
Yielding a formula in a much simpler form.
Differentiation and integration are also built-in operations:
![[Graphics:Images/appendix_gr_69.gif]](Images/appendix_gr_69.gif)
![[Graphics:Images/appendix_gr_70.gif]](Images/appendix_gr_70.gif)
![[Graphics:Images/appendix_gr_71.gif]](Images/appendix_gr_71.gif)
![[Graphics:Images/appendix_gr_72.gif]](Images/appendix_gr_72.gif)
![[Graphics:Images/appendix_gr_73.gif]](Images/appendix_gr_73.gif)
![[Graphics:Images/appendix_gr_74.gif]](Images/appendix_gr_74.gif)
![[Graphics:Images/appendix_gr_75.gif]](Images/appendix_gr_75.gif)
![[Graphics:Images/appendix_gr_76.gif]](Images/appendix_gr_76.gif)
Of course, you can't expect Mathematica to handle all indefinite integrals. Can you think of an integral that Mathematica can't evaluate? Try out your ideas.
Mathematica also knows about power series. The next example computes the 20th degree Taylor polynomial about x=0 (sometimes called a Maclaurin polynomial) for the given function.
![[Graphics:Images/appendix_gr_77.gif]](Images/appendix_gr_77.gif)
![[Graphics:Images/appendix_gr_78.gif]](Images/appendix_gr_78.gif)
Series can also handle purely symbolic functions such as f.
![[Graphics:Images/appendix_gr_79.gif]](Images/appendix_gr_79.gif)
![[Graphics:Images/appendix_gr_80.gif]](Images/appendix_gr_80.gif)
Limit calculations are also possible symbolically. For example, Mathematica can calculate the following limit to be e, rather than just some approximation to e .
![[Graphics:Images/appendix_gr_81.gif]](Images/appendix_gr_81.gif)
![[Graphics:Images/appendix_gr_82.gif]](Images/appendix_gr_82.gif)
A.4.3 Numerical Calculations
A first Look
You can do arithmetic with Mathematica, just as you would on a calculator. You have seen some examples in previous chapters. However, you must realise that Mathematica can give you exact results. So, let's calculate the exact result for 3 to power 1000.
![[Graphics:Images/appendix_gr_83.gif]](Images/appendix_gr_83.gif)
![[Graphics:Images/appendix_gr_84.gif]](Images/appendix_gr_84.gif)
You can use the Mathematica function N to get approximate results.
![[Graphics:Images/appendix_gr_85.gif]](Images/appendix_gr_85.gif)
![[Graphics:Images/appendix_gr_86.gif]](Images/appendix_gr_86.gif)
As an exercise, find the square root of 10 to 8 digits precision (the answer is 3.1622777). Remember, the help browser answers all your questions how to do this.
Mathematica can also handle complex numbers. In Mathematica I stands for the imaginary number.
![[Graphics:Images/appendix_gr_87.gif]](Images/appendix_gr_87.gif)
![[Graphics:Images/appendix_gr_88.gif]](Images/appendix_gr_88.gif)
![[Graphics:Images/appendix_gr_89.gif]](Images/appendix_gr_89.gif)
Some Mathematical Functions
The following mathematical functions are given without explanation.
N[] Sqrt[x] Exp[x] Log[x] Log[b,x] Sin[x], Cos[x], Tan[x] ArcSin[x], ArcCos[x], ArcTan[x] Abs[x] Round[x] Mod[n, m] Random[] Max[x, y, ...], Min[x, y, ...] FactorInteger[n] RealDigits[r] IntegerDigits[n]
The following constants are built into Mathematica .
Pi E Degree I Infinity
Here are some examples:
![[Graphics:Images/appendix_gr_90.gif]](Images/appendix_gr_90.gif)
![[Graphics:Images/appendix_gr_91.gif]](Images/appendix_gr_91.gif)
![[Graphics:Images/appendix_gr_92.gif]](Images/appendix_gr_92.gif)
![[Graphics:Images/appendix_gr_93.gif]](Images/appendix_gr_93.gif)
![[Graphics:Images/appendix_gr_94.gif]](Images/appendix_gr_94.gif)
![[Graphics:Images/appendix_gr_95.gif]](Images/appendix_gr_95.gif)
![[Graphics:Images/appendix_gr_96.gif]](Images/appendix_gr_96.gif)
![[Graphics:Images/appendix_gr_97.gif]](Images/appendix_gr_97.gif)
![[Graphics:Images/appendix_gr_98.gif]](Images/appendix_gr_98.gif)
![[Graphics:Images/appendix_gr_99.gif]](Images/appendix_gr_99.gif)
![[Graphics:Images/appendix_gr_100.gif]](Images/appendix_gr_100.gif)
![[Graphics:Images/appendix_gr_101.gif]](Images/appendix_gr_101.gif)
Assigning values to variables
This sets x to be 5
![[Graphics:Images/appendix_gr_102.gif]](Images/appendix_gr_102.gif)
![[Graphics:Images/appendix_gr_103.gif]](Images/appendix_gr_103.gif)
The x may now be used in calculations and will have the value 5.
![[Graphics:Images/appendix_gr_104.gif]](Images/appendix_gr_104.gif)
![[Graphics:Images/appendix_gr_105.gif]](Images/appendix_gr_105.gif)
This redefines the value for x.
![[Graphics:Images/appendix_gr_106.gif]](Images/appendix_gr_106.gif)
![[Graphics:Images/appendix_gr_107.gif]](Images/appendix_gr_107.gif)
Asking for x gives the current value of x.
![[Graphics:Images/appendix_gr_108.gif]](Images/appendix_gr_108.gif)
![[Graphics:Images/appendix_gr_109.gif]](Images/appendix_gr_109.gif)
Clear[x] or x = . clears the value for x.
![[Graphics:Images/appendix_gr_110.gif]](Images/appendix_gr_110.gif)
The result of the input x will now be x.
![[Graphics:Images/appendix_gr_111.gif]](Images/appendix_gr_111.gif)
![[Graphics:Images/appendix_gr_112.gif]](Images/appendix_gr_112.gif)
A common source of problems in using Mathematica is trying to use variables that already have values assigned. Therefore, clear variables directly after you are finished with them or before you use them.
Because Mathematica functions begin with capital letters, you may wish to begin your own function names with lower case letters to prevent confusion.
Suppressing Output
Output can be suppressed by using semicolons after expressions. Semicolons can also be used to separate statements on a line.
![[Graphics:Images/appendix_gr_113.gif]](Images/appendix_gr_113.gif)
![[Graphics:Images/appendix_gr_114.gif]](Images/appendix_gr_114.gif)
![[Graphics:Images/appendix_gr_115.gif]](Images/appendix_gr_115.gif)
More Powerful Numerical Capabilities
Mathematica can evaluate all standard mathematical functions. Here is the value of the Bessel function of zeroth order of 14.5.
![[Graphics:Images/appendix_gr_116.gif]](Images/appendix_gr_116.gif)
![[Graphics:Images/appendix_gr_117.gif]](Images/appendix_gr_117.gif)
You can also find approximations to roots of equations with Mathematica using the FindRoot command. In simple cases, FindRoot uses Newton's method, so you must also specify a starting point. FindRoot then reports the first root it encounters (there may be others). In some cases you need to use FindRoot in conjunction with Plot to determine a good starting point.
![[Graphics:Images/appendix_gr_118.gif]](Images/appendix_gr_118.gif)
![[Graphics:Images/appendix_gr_119.gif]](Images/appendix_gr_119.gif)
And you can factor integers as in
![[Graphics:Images/appendix_gr_120.gif]](Images/appendix_gr_120.gif)
![[Graphics:Images/appendix_gr_121.gif]](Images/appendix_gr_121.gif)
How should the output be interpreted ?
Remember integration from section 2? Try to compute the exact value of the definite integral of
![[Graphics:Images/appendix_gr_122.gif]](Images/appendix_gr_122.gif) on the interval {x, 0, 3π/4}.
Computing the required anti-derivative is somewhat involved. If you are only interested in a numerical answer, it is often faster to use a numerical routine to approximate the value of the definite integral avoiding the symbolic difficulties. This feature is illustrated next.
![[Graphics:Images/appendix_gr_124.gif]](Images/appendix_gr_124.gif)
![[Graphics:Images/appendix_gr_125.gif]](Images/appendix_gr_125.gif)
What about the accuracy of NIntegrate. If you have time left, try to find it experimentally, and compare with the default setting of NIntegrate?
A.4.4 Solving Equations
Here is an algebraic equation in Mathematica
![[Graphics:Images/appendix_gr_126.gif]](Images/appendix_gr_126.gif)
![[Graphics:Images/appendix_gr_127.gif]](Images/appendix_gr_127.gif)
This solves the equation on the previous line and gives the solution in terms of the parameter a.
![[Graphics:Images/appendix_gr_128.gif]](Images/appendix_gr_128.gif)
![[Graphics:Images/appendix_gr_129.gif]](Images/appendix_gr_129.gif)
Here is the solution to a simple set of simultaneous equations.
![[Graphics:Images/appendix_gr_130.gif]](Images/appendix_gr_130.gif)
![[Graphics:Images/appendix_gr_131.gif]](Images/appendix_gr_131.gif)
Mathematica can also solve differential equations. Here is the closed form solution for .
![[Graphics:Images/appendix_gr_133.gif]](Images/appendix_gr_133.gif)
![[Graphics:Images/appendix_gr_134.gif]](Images/appendix_gr_134.gif)
Mathematica also solves equations numerically. If you would try to solve the equation , you would find
![[Graphics:Images/appendix_gr_136.gif]](Images/appendix_gr_136.gif)
which means that Mathematica could not find an explicit algebraic form, and leaves the results in a symbolic form.
Note that the syntax above is further explained in section 1.4.8
Applying N gives the numerical results
![[Graphics:Images/appendix_gr_137.gif]](Images/appendix_gr_137.gif)
![[Graphics:Images/appendix_gr_138.gif]](Images/appendix_gr_138.gif)
![[Graphics:Images/appendix_gr_139.gif]](Images/appendix_gr_139.gif)
![[Graphics:Images/appendix_gr_140.gif]](Images/appendix_gr_140.gif)
If you are only interested in numerical results, it is usually a lot faster to use the function NSolve to ask for a numerical results.
![[Graphics:Images/appendix_gr_141.gif]](Images/appendix_gr_141.gif)
![[Graphics:Images/appendix_gr_142.gif]](Images/appendix_gr_142.gif)
NSolve gives you a general way to find numerical approximations to the solutions of polynomial equations. Finding numerical solutions to more general equations, however, can be much more difficult. FindRoot gives you a way to search for a numerical solution to an arbitrary equation, or set of equations. Below is an example how FindRoot allows you to solve transcendental equation numerically. This gives the solution to a pair of simultaneous equations near .
![[Graphics:Images/appendix_gr_144.gif]](Images/appendix_gr_144.gif)
![[Graphics:Images/appendix_gr_145.gif]](Images/appendix_gr_145.gif)
As you would expect by now, Mathematica can also find numerical solutions to differential equations. Below, NDSolve solves for the function y with x in the range 0 to 20.
![[Graphics:Images/appendix_gr_146.gif]](Images/appendix_gr_146.gif)
![[Graphics:Images/appendix_gr_147.gif]](Images/appendix_gr_147.gif)
The solution is given as an interpolating function, which allows to find values of y(x) for specific values of x. Below we take the solution and plot it as a function of x.
![[Graphics:Images/appendix_gr_148.gif]](Images/appendix_gr_148.gif)
![[Graphics:Images/appendix_gr_149.gif]](Images/appendix_gr_149.gif)
Intermezzo, the /. Construction, Applying Transformation Rules
When transforming an expression like to , Mathematica treats in a purely symbolic manner. One way to replace with a definite value is to apply a transformation rule.
This is how you apply a transformation rule. You can think of this as "in the expression replace all with 3."
![[Graphics:Images/appendix_gr_156.gif]](Images/appendix_gr_156.gif)
![[Graphics:Images/appendix_gr_157.gif]](Images/appendix_gr_157.gif)
You can replace x with an expression.
![[Graphics:Images/appendix_gr_158.gif]](Images/appendix_gr_158.gif)
![[Graphics:Images/appendix_gr_159.gif]](Images/appendix_gr_159.gif)
You can apply lists of rules.
![[Graphics:Images/appendix_gr_160.gif]](Images/appendix_gr_160.gif)
![[Graphics:Images/appendix_gr_161.gif]](Images/appendix_gr_161.gif)
More information on transformation rules is given in section 4.6.
A.4.5 Lists and Matrices
Making Lists of Objects
Lists are a way to make collections of objects in Mathematica. Lists are important structures in Mathematica.
Here is a list of three numbers.
![[Graphics:Images/appendix_gr_162.gif]](Images/appendix_gr_162.gif)
![[Graphics:Images/appendix_gr_163.gif]](Images/appendix_gr_163.gif)
You can perform arithmetic operations on the whole list.
![[Graphics:Images/appendix_gr_164.gif]](Images/appendix_gr_164.gif)
![[Graphics:Images/appendix_gr_165.gif]](Images/appendix_gr_165.gif)
This takes the difference between corresponding elements in the lists. The lists must be of equal length.
![[Graphics:Images/appendix_gr_166.gif]](Images/appendix_gr_166.gif)
![[Graphics:Images/appendix_gr_167.gif]](Images/appendix_gr_167.gif)
You can apply mathematical functions to whole lists.
![[Graphics:Images/appendix_gr_168.gif]](Images/appendix_gr_168.gif)
![[Graphics:Images/appendix_gr_169.gif]](Images/appendix_gr_169.gif)
You can assign the list to a variable.
![[Graphics:Images/appendix_gr_170.gif]](Images/appendix_gr_170.gif)
![[Graphics:Images/appendix_gr_171.gif]](Images/appendix_gr_171.gif)
This variable ll can be used instead of the list in subsequent calculations.
![[Graphics:Images/appendix_gr_172.gif]](Images/appendix_gr_172.gif)
![[Graphics:Images/appendix_gr_173.gif]](Images/appendix_gr_173.gif)
Manipulating Elements of Lists
You can refer to parts of lists by using double brackets or Part.
This takes the first element in the list.
![[Graphics:Images/appendix_gr_174.gif]](Images/appendix_gr_174.gif)
![[Graphics:Images/appendix_gr_175.gif]](Images/appendix_gr_175.gif)
Notice the use of the symbols in the StandardForm, which in InputForm would be just double brackets, as was explained in section A.3.3.
This extracts a list of elements.
![[Graphics:Images/appendix_gr_177.gif]](Images/appendix_gr_177.gif)
![[Graphics:Images/appendix_gr_178.gif]](Images/appendix_gr_178.gif)
You can reset the value of elements in a list. Here is a list assigned to w.
![[Graphics:Images/appendix_gr_179.gif]](Images/appendix_gr_179.gif)
![[Graphics:Images/appendix_gr_180.gif]](Images/appendix_gr_180.gif)
This resets the value of the 5th element in the list to b.
![[Graphics:Images/appendix_gr_181.gif]](Images/appendix_gr_181.gif)
![[Graphics:Images/appendix_gr_182.gif]](Images/appendix_gr_182.gif)
![[Graphics:Images/appendix_gr_183.gif]](Images/appendix_gr_183.gif)
![[Graphics:Images/appendix_gr_184.gif]](Images/appendix_gr_184.gif)
![[Graphics:Images/appendix_gr_185.gif]](Images/appendix_gr_185.gif)
Making Tables of Values
The following command makes a list of the squares of the first twenty positive integers.
![[Graphics:Images/appendix_gr_186.gif]](Images/appendix_gr_186.gif)
![[Graphics:Images/appendix_gr_187.gif]](Images/appendix_gr_187.gif)
Next, we plot the values in the list
![[Graphics:Images/appendix_gr_188.gif]](Images/appendix_gr_188.gif)
![[Graphics:Images/appendix_gr_189.gif]](Images/appendix_gr_189.gif)
You can specify the stepsize:
![[Graphics:Images/appendix_gr_190.gif]](Images/appendix_gr_190.gif)
![[Graphics:Images/appendix_gr_191.gif]](Images/appendix_gr_191.gif)
You can read data from an external file (notice that file example.dat must be accessible).
![[Graphics:Images/appendix_gr_192.gif]](Images/appendix_gr_192.gif)
![[Graphics:Images/appendix_gr_193.gif]](Images/appendix_gr_193.gif)
You can also make lists like this:
![[Graphics:Images/appendix_gr_194.gif]](Images/appendix_gr_194.gif)
![[Graphics:Images/appendix_gr_195.gif]](Images/appendix_gr_195.gif)
The next command generates a 2D matrix whose (i,j)-entry equals the greatest common divisor of i and j.
![[Graphics:Images/appendix_gr_196.gif]](Images/appendix_gr_196.gif)
![[Graphics:Images/appendix_gr_197.gif]](Images/appendix_gr_197.gif)
You can manipulate matrices and lists all at the time
![[Graphics:Images/appendix_gr_198.gif]](Images/appendix_gr_198.gif)
![[Graphics:Images/appendix_gr_199.gif]](Images/appendix_gr_199.gif)
You can access parts of the list using double brackets or Part.
![[Graphics:Images/appendix_gr_200.gif]](Images/appendix_gr_200.gif)
![[Graphics:Images/appendix_gr_201.gif]](Images/appendix_gr_201.gif)
![[Graphics:Images/appendix_gr_202.gif]](Images/appendix_gr_202.gif)
![[Graphics:Images/appendix_gr_203.gif]](Images/appendix_gr_203.gif)
Or write it like this
![[Graphics:Images/appendix_gr_204.gif]](Images/appendix_gr_204.gif)
![[Graphics:Images/appendix_gr_205.gif]](Images/appendix_gr_205.gif)
![[Graphics:Images/appendix_gr_206.gif]](Images/appendix_gr_206.gif)
A range of points
![[Graphics:Images/appendix_gr_207.gif]](Images/appendix_gr_207.gif)
![[Graphics:Images/appendix_gr_208.gif]](Images/appendix_gr_208.gif)
I want only the first elements
![[Graphics:Images/appendix_gr_209.gif]](Images/appendix_gr_209.gif)
![[Graphics:Images/appendix_gr_210.gif]](Images/appendix_gr_210.gif)
Or
![[Graphics:Images/appendix_gr_211.gif]](Images/appendix_gr_211.gif)
![[Graphics:Images/appendix_gr_212.gif]](Images/appendix_gr_212.gif)
Remember the command Table, as it is very useful for future work. If you want the list to look more like a matrix you can use the command
![[Graphics:Images/appendix_gr_213.gif]](Images/appendix_gr_213.gif)
![[Graphics:Images/appendix_gr_214.gif]](Images/appendix_gr_214.gif)
We can compute the determinant of the matrix with
![[Graphics:Images/appendix_gr_215.gif]](Images/appendix_gr_215.gif)
![[Graphics:Images/appendix_gr_216.gif]](Images/appendix_gr_216.gif)
Since the determinant is non-zero, we can calculate the inverse matrix.
![[Graphics:Images/appendix_gr_217.gif]](Images/appendix_gr_217.gif)
![[Graphics:Images/appendix_gr_218.gif]](Images/appendix_gr_218.gif)
Finally, we can verify that the product of the matrix and its inverse is the identity matrix.
![[Graphics:Images/appendix_gr_219.gif]](Images/appendix_gr_219.gif)
![[Graphics:Images/appendix_gr_220.gif]](Images/appendix_gr_220.gif)
Mathematica can also manipulate symbolic matrices. This finds the eigenvectors of a matrix, then simplifies the algebraic result.
![[Graphics:Images/appendix_gr_221.gif]](Images/appendix_gr_221.gif)
![[Graphics:Images/appendix_gr_222.gif]](Images/appendix_gr_222.gif)
Here's a list of other Mathematica functions which work with matrices and lists.
Array[a,n] or Array[a, {n,m}] Range[n] Length[list] ColumnForm[list] Dimensions[list] MatrixForm[list] MatrixPower[m,n] Inverse[m] Det[m] Transpose[m] Eigenvectors[m] Eigenvalues[m]
Getting Parts of Lists
Here are some functions to use for taking parts of lists.
First[list] Last[list] Part[list,n] Take[list,n] Rest[list] Drop[list,n]
Rearranging Lists
Some functions for rearranging lists
Sort[list] Union[list] Reverse[list] RotateLeft[list,n] RotateRight[list,n]
A.4.6 More on Transformation Rules
You have already seen how to use a rule, for instance on a algebraic expression.
![[Graphics:Images/appendix_gr_224.gif]](Images/appendix_gr_224.gif)
![[Graphics:Images/appendix_gr_225.gif]](Images/appendix_gr_225.gif)
You can give transformation rules for any expression. Here's a rule for .
![[Graphics:Images/appendix_gr_227.gif]](Images/appendix_gr_227.gif)
![[Graphics:Images/appendix_gr_228.gif]](Images/appendix_gr_228.gif)
More general, this replace , where anything is named , by .
![[Graphics:Images/appendix_gr_232.gif]](Images/appendix_gr_232.gif)
![[Graphics:Images/appendix_gr_233.gif]](Images/appendix_gr_233.gif)
Here is a Mathematica function definition, specifying that is always to be transformed to .
![[Graphics:Images/appendix_gr_236.gif]](Images/appendix_gr_236.gif)
![[Graphics:Images/appendix_gr_237.gif]](Images/appendix_gr_237.gif)
![[Graphics:Images/appendix_gr_238.gif]](Images/appendix_gr_238.gif)
Using the question mark gives information about the rules you have defined for a function.
![[Graphics:Images/appendix_gr_239.gif]](Images/appendix_gr_239.gif)
Global`f
Actually, the question mark also provides information on build-in functions
![[Graphics:Images/appendix_gr_241.gif]](Images/appendix_gr_241.gif)
![[Graphics:Images/appendix_gr_242.gif]](Images/appendix_gr_242.gif)
Here is a way to get extra information including Attributes and Options.
![[Graphics:Images/appendix_gr_243.gif]](Images/appendix_gr_243.gif)
![[Graphics:Images/appendix_gr_244.gif]](Images/appendix_gr_244.gif)
You can also use wild cards.
![[Graphics:Images/appendix_gr_247.gif]](Images/appendix_gr_247.gif)
Returning to the transformation rules, you can also define recursive rules, e.g. for the factorial function.
![[Graphics:Images/appendix_gr_267.gif]](Images/appendix_gr_267.gif)
Also provide a rule for the end condition of the function.
![[Graphics:Images/appendix_gr_268.gif]](Images/appendix_gr_268.gif)
Here are once more the rules you just defined.
![[Graphics:Images/appendix_gr_269.gif]](Images/appendix_gr_269.gif)
Global`fac
Mathematica can now apply these rules to find the value for factorials.
![[Graphics:Images/appendix_gr_272.gif]](Images/appendix_gr_272.gif)
![[Graphics:Images/appendix_gr_273.gif]](Images/appendix_gr_273.gif)
Compare the result with the build-in factorial function.
"/." is the operator that can be symbol replacements:
![[Graphics:Images/appendix_gr_274.gif]](Images/appendix_gr_274.gif)
![[Graphics:Images/appendix_gr_275.gif]](Images/appendix_gr_275.gif)
or pattern replacements
![[Graphics:Images/appendix_gr_276.gif]](Images/appendix_gr_276.gif)
![[Graphics:Images/appendix_gr_277.gif]](Images/appendix_gr_277.gif)
In a pattern replacement rule, x_ means "anything matches here, and call that x".
Mathematica internally holds a list of replacements.
![[Graphics:Images/appendix_gr_278.gif]](Images/appendix_gr_278.gif)
![[Graphics:Images/appendix_gr_279.gif]](Images/appendix_gr_279.gif)
This means, that each time Mathematica encounters the symbol "x", it will be replaced by "5".
![[Graphics:Images/appendix_gr_280.gif]](Images/appendix_gr_280.gif)
![[Graphics:Images/appendix_gr_281.gif]](Images/appendix_gr_281.gif)
Now, watch the difference between "=" and ":="
![[Graphics:Images/appendix_gr_282.gif]](Images/appendix_gr_282.gif)
![[Graphics:Images/appendix_gr_283.gif]](Images/appendix_gr_283.gif)
![[Graphics:Images/appendix_gr_284.gif]](Images/appendix_gr_284.gif)
![[Graphics:Images/appendix_gr_285.gif]](Images/appendix_gr_285.gif)
![[Graphics:Images/appendix_gr_286.gif]](Images/appendix_gr_286.gif)
![[Graphics:Images/appendix_gr_287.gif]](Images/appendix_gr_287.gif)
![[Graphics:Images/appendix_gr_288.gif]](Images/appendix_gr_288.gif)
![[Graphics:Images/appendix_gr_289.gif]](Images/appendix_gr_289.gif)
![[Graphics:Images/appendix_gr_290.gif]](Images/appendix_gr_290.gif)
![[Graphics:Images/appendix_gr_291.gif]](Images/appendix_gr_291.gif)
![[Graphics:Images/appendix_gr_292.gif]](Images/appendix_gr_292.gif)
![[Graphics:Images/appendix_gr_293.gif]](Images/appendix_gr_293.gif)
![[Graphics:Images/appendix_gr_294.gif]](Images/appendix_gr_294.gif)
":=" is called the delayed assignment. It means, that the right-hand side will not be evaluated before it is assigned to the left-hand side. In this example "x+5" will be evaluated if you ask for the value of "z". Compare y and z in the following example:
![[Graphics:Images/appendix_gr_295.gif]](Images/appendix_gr_295.gif)
![[Graphics:Images/appendix_gr_296.gif]](Images/appendix_gr_296.gif)
![[Graphics:Images/appendix_gr_297.gif]](Images/appendix_gr_297.gif)
![[Graphics:Images/appendix_gr_298.gif]](Images/appendix_gr_298.gif)
![[Graphics:Images/appendix_gr_299.gif]](Images/appendix_gr_299.gif)
This is important for defining functions:
The following defines a global pattern replacement rule f[x_]->Sin[x]
Compare the following two examples, and watch the role of the delayed assignment :=
![[Graphics:Images/appendix_gr_300.gif]](Images/appendix_gr_300.gif)
![[Graphics:Images/appendix_gr_301.gif]](Images/appendix_gr_301.gif)
![[Graphics:Images/appendix_gr_302.gif]](Images/appendix_gr_302.gif)
![[Graphics:Images/appendix_gr_303.gif]](Images/appendix_gr_303.gif)
![[Graphics:Images/appendix_gr_304.gif]](Images/appendix_gr_304.gif)
![[Graphics:Images/appendix_gr_305.gif]](Images/appendix_gr_305.gif)
But
![[Graphics:Images/appendix_gr_306.gif]](Images/appendix_gr_306.gif)
![[Graphics:Images/appendix_gr_307.gif]](Images/appendix_gr_307.gif)
![[Graphics:Images/appendix_gr_308.gif]](Images/appendix_gr_308.gif)
![[Graphics:Images/appendix_gr_309.gif]](Images/appendix_gr_309.gif)
![[Graphics:Images/appendix_gr_310.gif]](Images/appendix_gr_310.gif)
Also, remember to define a pattern replacement rather than a fixed replacement rule. The following is probably not what you want:
![[Graphics:Images/appendix_gr_311.gif]](Images/appendix_gr_311.gif)
![[Graphics:Images/appendix_gr_312.gif]](Images/appendix_gr_312.gif)
![[Graphics:Images/appendix_gr_313.gif]](Images/appendix_gr_313.gif)
![[Graphics:Images/appendix_gr_314.gif]](Images/appendix_gr_314.gif)
You can also define functions of multiple variables
![[Graphics:Images/appendix_gr_315.gif]](Images/appendix_gr_315.gif)
![[Graphics:Images/appendix_gr_316.gif]](Images/appendix_gr_316.gif)
![[Graphics:Images/appendix_gr_317.gif]](Images/appendix_gr_317.gif)
Note that "overloading" of function names is possible
![[Graphics:Images/appendix_gr_318.gif]](Images/appendix_gr_318.gif)
![[Graphics:Images/appendix_gr_319.gif]](Images/appendix_gr_319.gif)
![[Graphics:Images/appendix_gr_320.gif]](Images/appendix_gr_320.gif)
![[Graphics:Images/appendix_gr_321.gif]](Images/appendix_gr_321.gif)
![[Graphics:Images/appendix_gr_322.gif]](Images/appendix_gr_322.gif)
You can also take the derivative like this:
![[Graphics:Images/appendix_gr_323.gif]](Images/appendix_gr_323.gif)
![[Graphics:Images/appendix_gr_324.gif]](Images/appendix_gr_324.gif)
![[Graphics:Images/appendix_gr_325.gif]](Images/appendix_gr_325.gif)
![[Graphics:Images/appendix_gr_326.gif]](Images/appendix_gr_326.gif)
![[Graphics:Images/appendix_gr_327.gif]](Images/appendix_gr_327.gif)
![[Graphics:Images/appendix_gr_328.gif]](Images/appendix_gr_328.gif)
![[Graphics:Images/appendix_gr_329.gif]](Images/appendix_gr_329.gif)
![[Graphics:Images/appendix_gr_330.gif]](Images/appendix_gr_330.gif)
A.4.7 Using Add-On Packages
Mathematica comes with several packages for special applications. Packages must be loaded into a session before they can be used. $Packages gives a list of the contexts corresponding to all packages that have been loaded in your current Mathematica session.
The various packages are located in the Packages sub-folder of the Mathematica folder. Information about them can be found in the book Mathematica Standard Add-On Packages, which is part of the standard Mathematica distribution, and also in the help browser. Also, the help browser helps in getting information on the available packages. The following example illustrates the use of a package.
![[Graphics:Images/appendix_gr_331.gif]](Images/appendix_gr_331.gif)
This command loads the package Polyhedra. It reads in a file, evaluating each expression in it, and returning the last one. Now that the package has been loaded, we can use some of the commands defined in the package.
![[Graphics:Images/appendix_gr_332.gif]](Images/appendix_gr_332.gif)
![[Graphics:Images/appendix_gr_333.gif]](Images/appendix_gr_333.gif)
You can also use Need command which read in the package if the specified context is not already in $Package. Itt should be used if you want to set it up so that a particular package is read in only if it is needed. This loads the package PlotField if it has not already been loaded:
![[Graphics:Images/appendix_gr_334.gif]](Images/appendix_gr_334.gif)
SetDirectory sets the current working directory.
A.4.8 Programming in Mathematica
Examples
Mathematica is also a high-level programming language with capabilities for iteration, looping, user-defined functions, etc.
p?test is a pattern object that stands for any expression which matches p, and on which the application of test gives True.
![[Graphics:Images/appendix_gr_335.gif]](Images/appendix_gr_335.gif)
![[Graphics:Images/appendix_gr_336.gif]](Images/appendix_gr_336.gif)
The definition for p1 applies only when x is manifestly a number.
![[Graphics:Images/appendix_gr_337.gif]](Images/appendix_gr_337.gif)
![[Graphics:Images/appendix_gr_338.gif]](Images/appendix_gr_338.gif)
The definition for p2 is more liberal.
![[Graphics:Images/appendix_gr_339.gif]](Images/appendix_gr_339.gif)
![[Graphics:Images/appendix_gr_340.gif]](Images/appendix_gr_340.gif)
The following code displays the graphs of a function for several different values of the parameter a.
![[Graphics:Images/appendix_gr_341.gif]](Images/appendix_gr_341.gif)
![[Graphics:Images/appendix_gr_342.gif]](Images/appendix_gr_342.gif)
The above example involves a straightforward Do loop which generates a list of graphics. One very useful feature about such a list is that it can be animated. If you select the graphics by clicking the appropriate enclosing cell bracket, choose the Cell menu, then choose Animate Selected Graphics, an animation will begin. You can control various aspects of the animation by clicking the buttons in the lower left of the window. Click anywhere in the middle of the window to stop the animation. If you close the cell group containing all the graphics you only see the first generated graphic. Double-clicking on the this graphic now suffices to start the animation
If you want to print an animation, of course there is a problem. You could print one frame per page to generate a kind of flip book animation. Or, you could just print several frames per page in some type of matrix arrangement. The GraphicsArray command makes the latter possible, as illustrated below.
![[Graphics:Images/appendix_gr_343.gif]](Images/appendix_gr_343.gif)
![[Graphics:Images/appendix_gr_344.gif]](Images/appendix_gr_344.gif)
If you want to perform an operation many times until some condition is true, you can use While loop:
![[Graphics:Images/appendix_gr_345.gif]](Images/appendix_gr_345.gif)
![[Graphics:Images/appendix_gr_346.gif]](Images/appendix_gr_346.gif)
![[Graphics:Images/appendix_gr_347.gif]](Images/appendix_gr_347.gif)
![[Graphics:Images/appendix_gr_348.gif]](Images/appendix_gr_348.gif)
![[Graphics:Images/appendix_gr_349.gif]](Images/appendix_gr_349.gif)
You can often avoid using loops in Mathematica by operating directly on complete lists. The resulting programs are usually more elegant and more efficient. Here are programs to calculate the means and variance of a list. The pattern numlist_List only matches lists.
![[Graphics:Images/appendix_gr_350.gif]](Images/appendix_gr_350.gif)
![[Graphics:Images/appendix_gr_351.gif]](Images/appendix_gr_351.gif)
![[Graphics:Images/appendix_gr_352.gif]](Images/appendix_gr_352.gif)
![[Graphics:Images/appendix_gr_353.gif]](Images/appendix_gr_353.gif)
![[Graphics:Images/appendix_gr_354.gif]](Images/appendix_gr_354.gif)
![[Graphics:Images/appendix_gr_355.gif]](Images/appendix_gr_355.gif)
![[Graphics:Images/appendix_gr_356.gif]](Images/appendix_gr_356.gif)
![[Graphics:Images/appendix_gr_357.gif]](Images/appendix_gr_357.gif)
![[Graphics:Images/appendix_gr_358.gif]](Images/appendix_gr_358.gif)
The final result shows the action of the pattern matching of the numlist_List construction. Mathematica does not have a rule to apply mean to an integer, and therefore returns mean[66]. You could also apply a rule for the case that you give an integer to the function mean.
![[Graphics:Images/appendix_gr_359.gif]](Images/appendix_gr_359.gif)
![[Graphics:Images/appendix_gr_360.gif]](Images/appendix_gr_360.gif)
![[Graphics:Images/appendix_gr_361.gif]](Images/appendix_gr_361.gif)
Here's another example of the use of lists. We will compare the graphs of with those of its first eight Maclaurin polynomials. Rather than inputting the formulas for the polynomials by hand, and rather than use the Mathematica built-in command Series, we will build a table of the polynomials all at once. This is done in a single line of input to Mathematica (spread out over a number of lines of code below for readability). Note that this line of input contains what would amount to a nested double Do loop in many other programming languages.
![[Graphics:Images/appendix_gr_363.gif]](Images/appendix_gr_363.gif)
![[Graphics:Images/appendix_gr_364.gif]](Images/appendix_gr_364.gif)
Finally, here is a program that plots the solutions to a polynomial equation as points in the complex plane. The /; clause at the end checks if is indeed a polynomial if the variable . It fulfills the same function as the pattern matching rules introduced above. Furthermore, notice the use of the /. construction.
![[Graphics:Images/appendix_gr_367.gif]](Images/appendix_gr_367.gif)
![[Graphics:Images/appendix_gr_368.gif]](Images/appendix_gr_368.gif)
![[Graphics:Images/appendix_gr_369.gif]](Images/appendix_gr_369.gif)
And again see the result of check on poly.
![[Graphics:Images/appendix_gr_370.gif]](Images/appendix_gr_370.gif)
![[Graphics:Images/appendix_gr_371.gif]](Images/appendix_gr_371.gif)
![[Graphics:Images/appendix_gr_372.gif]](Images/appendix_gr_372.gif)
Actually, in all these examples you have seen a number of different programming paradigms, supported by Mathematica. In the next parts of this section these different paradigms are listed, and for each an example is presented. This overview, together with the (long) list of programming functions, which you can find in the help browser, gives you a good idea of the very powerful programming capabilities of Mathematica.
Modules
Modules allow you to set up local variables with names that are local to the specific module. The new symbols are created to represent each of its local variables every time it is called.
This defines the global variable t to have the value 17.
![[Graphics:Images/appendix_gr_373.gif]](Images/appendix_gr_373.gif)
![[Graphics:Images/appendix_gr_374.gif]](Images/appendix_gr_374.gif)
The t inside the Module is local, so it can be treated independently of the global t.
![[Graphics:Images/appendix_gr_375.gif]](Images/appendix_gr_375.gif)
![[Graphics:Images/appendix_gr_376.gif]](Images/appendix_gr_376.gif)
The global t still has value 17.
![[Graphics:Images/appendix_gr_377.gif]](Images/appendix_gr_377.gif)
![[Graphics:Images/appendix_gr_378.gif]](Images/appendix_gr_378.gif)
Blocks and Local Values
Modules in Mathematica allow you to treat the names of variables as local. Sometimes, however, you want the names to be global, but values to be local. You can do this in Mathematica using Block.
Here is an expression involving x.
x^2 + 3
![[Graphics:Images/appendix_gr_379.gif]](Images/appendix_gr_379.gif)
This evaluates the previous expression, using a local value for x.
Block[{x = a + 1}, %]
![[Graphics:Images/appendix_gr_380.gif]](Images/appendix_gr_380.gif)
There is no global value for x.
x
![[Graphics:Images/appendix_gr_381.gif]](Images/appendix_gr_381.gif)
As described above, the variable x in a module such as Module[ x , body] is always set up to refer to a unique symbol, different each time the module is used, and distinct from the global symbol x. The x in a block such as Block[ x , body] is, however, taken to be the global symbol x. What the block does is to make the value of x local. The value x had when you entered the block is always restored when you exit the block. And during the execution of the block, x can take on any value.
Procedural Programming
![[Graphics:Images/appendix_gr_386.gif]](Images/appendix_gr_386.gif)
![[Graphics:Images/appendix_gr_387.gif]](Images/appendix_gr_387.gif)
![[Graphics:Images/appendix_gr_388.gif]](Images/appendix_gr_388.gif)
![[Graphics:Images/appendix_gr_389.gif]](Images/appendix_gr_389.gif)
List-based Programming
![[Graphics:Images/appendix_gr_390.gif]](Images/appendix_gr_390.gif)
![[Graphics:Images/appendix_gr_391.gif]](Images/appendix_gr_391.gif)
|