PROGRAMMING IN MATHEMATICA

The easiest way to learn how to program an elasticity problem in Mathematica is to run one of the test programs such as S521.nb or S522.nb. To run these programs, first download them into a file with the .nb extension (e.g. S521.nb). Now start the Mathematica program and click on `file' and then `open'. This will display a list of mathematica documents (typically any files with extension `.nb' or `.m') from which S521.nb can be selected by double clicking.

1. Notice that the commands are grouped into cells by square brackets down the right hand side of the window. You can select one cell by clicking on the bracket. Alternatively, you can select several adjacent cells by clicking and dragging. The commands in the selected cells can then performed by clicking on Evaluation->Evaluate Cells. .

2. The notebook can be also edited in the Mathematica window exactly like a text file.

3. You can write a new command in the same cell by typing a carriage return followed by the new command. If you want to start a new cell, move the cursor below the last command and click.

4. Once you have given a value to an expression (e.g. x=a), Mathematica will assume that value for all subsequent calculations, including new runs of the notebook. If you want to change such an assignment, you will need to re-initialize the notebook. You can do this by clicking on Cell->Delete All Output. If this still doesn't work, try Evaluation->Quit Kernel->Local followed by Evaluation->Start Kernel->Local.

5. The derivative g=df/dx of a function f is obtained by writing

g=D[f,x]

Higher order derivatives can be written as (for example)

d^3f/dxdy^2=D[f,x,y,y]

The indefinite integral of a function f is written

g=Integrate[f,x]

and the definite integral from a to b as

g=Integrate[f,{x,a,b}]

6. The command

g=f/.x->a

defines the function g as being equal to f with x replaced by a. It is very useful for imposing boundary conditions. The variant

g=f/.%

substitutes the result from the preceding line into the function f. Don't use this version in a subroutine (see #12 below).

If you want to make several substitutions at the same time, you enclose them in braces and separate them by commas, as in

g=f/.{x->a,y->b}

7. The operator

Coefficient[f,x^2]

selects the coefficient of x^2 from the function f. Unlike Maple, Mathematica will not accept the operation Coefficient[f,x^0]. If you want to extract the term from a function that is independent of x, you have two choices. One is to use `Coefficient' to find all the other x-varying terms and then subtract them from f. The other is to use the sequence

c0=f-Integrate[D[f,x],x]

By differentiating and then integrating, we recover the original expression f, except that the term that is independent of f is lost in the differentiation. Subtracting this from the original function f therefore exposes the x-independent term.

8. The command

Solve[{f1==0,f2==0},{A,B}]

solves the equations f1=0, f2=0 for A and B. It can accept any number of equations and any number of unknowns. If there are too many unknowns, Mathematica will simply eliminate as many as possible and express the answer in terms of the remaining unknowns. If there are two many equations, it will return a solution if there is one - i.e. if the equations are not linearly independent. This is quite helpful in boundary-value problems, since a straightforward approach to the formulation will often generate redundant conditions.

The command

solution=Solve[{f1==0,f2==0},{A,B}]

has the same efect as Solve, but it stores the results in the location 'solution'. This is useful because we can then substitute the results into another expression using the form

phi1=phi/.solution

9. Mathematica will provide a warning message and a beep if you use two symbols with rather similar spelling. Disregard this warning if you are sure the spelling is correct.

10. Text comments can be embedded in a Mathematica file by starting with (* and ending with *)

11. If you terminate a Mathematica command with the symbol ; the expression will be calculated during evaluation, but will not be displayed on the screen. This is useful in keeping the output down to managable proportions in lengthy calculations.

12. You can call a notebook file as a subroutine from another notebook. To do this, open the subroutine file (e.g.file.nb select the cells you want to be evaluated and then click on Cell->Cell Properties->Initialization Cell. Then click on File->Save As and select Mathematica Package as the file type. This will generate a file called file.m. In the outer file, you then call this subroutine using the command

<< file.m

13. The command TrigReduce will express products and powers of trigonometric functions in terms of trigonometric functions with combined arguments. For example TrigReduce[Sin[x]*Cos[x]] will return Sin[2x]/2

14. Mathematica will sometimes return expressions like Sqrt[a^2] because it is allowing a to be either positive or negative. If a>0 this can be avoided by including the `option' Assumptions->{a>0} in the appropriate operation. For example

f1=Simplify[f0,Assumptions->{a>0}]

15. The web site contains Mathematica files for the tables in Chapters 8,9,19,20 and programs for a wide range of other routine operations in two and three dimensions. For a complete list of these files, with clickable links to the files, go to Catalogue of Mathematica files.

Back to J.R.Barber, Elasticity