Primer on Mathematica

advertisement
Primer on Mathematica
1. General Procedures
Mathematica calculations are done in windows called notebooks, which are saved as files with
the .nb extension. Notebook files can be saved, opened, moved, or copied like other files.
Notebooks are divided into cells. Calculations you want to perform are typed into input cells.
When you open a new, blank notebook and begin typing, an input cell is automatically created.
When you finish typing in an input cell, use the <shift><enter> key combination to evaluate the
calculation. Mathematica then displays the answer in an output cell immediately below the input
cell.
For the next calculation, click below the last output cell and start typing to create a new input
cell.
You can copy and paste strings of text from other cells into input cells.
The results of the current calculation may depend on the results of prior calculations. For
example, you may have defined several variables in prior calculations and they will keep their
meanings in any new calculations unless you redefine them, or remove their definitions using the
Remove command.
When you close the Mathematica program, all variable definitions are removed.
When you start Mathematica and open a saved notebook, none of the input is evaluated. To see
the results of the saved calculations, you must re-evaluate the input cells in the notebook. The
“evaluate notebook” option in the Evaluation menu allows you to evaluate all input cells in the
order in which they appear in the notebook. If you evaluate the input cells one-by-one in a
different order, you may get different results.
For that reason, if you are following this lesson and entering and calculating results as you go,
make sure you do the calculations in the order in which they appear.
2. Arithmetic Calculations
Basic calculations are done with operator symbols similar to those in other computer languages
and programs.
When using whole numbers or fractions in input cells, Mathematica provides results as whole
numbers or fractions.
1
Addition:
In:
Out:
3+2
5
Subtraction:
In:
Out:
2-7
-5
Multiplication, use * or a space between numbers or symbols:
In:
Out:
-3*5
-15
In:
Out:
-3 5
-15
Division:
In:
Out:
4/2
2
In:
4/3
4
3
Out:
Exponentiation, use ^ symbol:
In:
Out:
2^5
32
Calculations with  use the symbol “Pi”:
In:
Out:
Pi*8^2
64 
In:
(4/3)*Pi*8^2
2048 
3
Out:
Calculations with i (for imaginary numbers) use the symbol “I”:
In:
Out:
(3+4*I)+(5-2*I)
8 + 2 i
2
Numerical Calculations
Mathematica is not as convenient as MATLAB for most numerical calculations but it can do
them.
Mathematica calculates output as decimal numbers if
(a) numbers are entered with decimal points
(b) decimal calculations are specified by using the built-in function N.
Mathematica’s built-in functions all begin with capital letters, and use square brackets to delimit
their input. Avoid defining your own variables and symbols with capital letters.
Examples of numerical calculations with decimal output:
In:
Out:
3.0+2.0
5.
In:
Out:
N[3.0+2.0]
5.
In:
Out:
2-7.567
-5.567
In:
Out:
4.0/2.0
2.
In:
Out:
N[4/2]
2.
In:
Out:
N[4/3]
1.33333
In:
Out:
4.0/3
1.33333
In:
Out:
N[(4/3)*Pi*8^3]
2144.66
3
Symbolic Calculations
The real value of Mathematica lies in its capability to do symbolic calculations following the
rules of algebra, calculus, etc.
Mathematica does symbolic calculations when algebraic quantities appear in the input, with or
without numbers. Symbolic input uses the same symbols for addition, subtraction, multiplication,
etc. as are used above for arithmetic and numerical calculations.
Symbols may be a single letter, such as with common algebraic variables (x, y, z, u, v, w, …), or
strings of characters (alpha, beta, lambda, …). Greek letters may also be used in symbolic input
by using the “special characters” palette, which can be opened from the Palettes menu.
Because Mathematica’s built-in functions begin with capital letters, it is a good idea to use only
lower case letters or Greek letters for symbolic input.
When constructing complex input you can use spaces and line breaks where needed to make the
expression easier to read. To use a line break, hit the <enter> key within an input cell and you
will start a new line (remember, only the <shift><enter> key combination will cause the input to
be evaluated). Spaces may be inserted where needed to organize the appearance of an expression,
with the caution that a single space between two symbols is interpreted as multiplication. For this
reason, it is advisable to get in the habit of using * to indicate multiplication, and to be careful
when using spaces to organize complex expressions.
Assignment Expressions
The equals sign is used to assign a variable name to complex expressions when doing symbolic
calculations.
In:
Out:
y = a*x^2 + b*x + c
c + b x + a x2
In:
Out:
z = x-2
-2 + x
Once a variable is assigned, you can evaluate it to see its current value:
In:
Out:
y
c + b x + a x2
In:
Out:
z
-2 + x
4
You can create new expressions with variables that have been defined:
In:
Out:
y*z
(-2 + x) (c + b x + a x2)
In:
Out:
x*z
(-2 + x) x
The Expand function multiplies out factors in expressions like these:
In:
Out:
Expand[y*z]
-2 c – 2 b x + c x -2 a x2 + b x2 + a x3
In:
Out:
Expand[x*z]
-2 x + x2
The Simplify function can often put complex expressions in simpler terms, by factoring them
and performing other algebraic operations.
In:
Out:
Simplify[-2*c–2*b*x+c*x-2*a*x^2+b*x^2+a*x^3]
(-2 + x) (c + x (b + a x))
When expressions involve ratios or fractions, using the Together function puts them over a
common denominator, and combining Simplify and Together can be very helpful. Compare the
output from these three calculations:
In:
a*x/(a*x+b*z) + b*y/(a*x+b*y)
In:
Together[a*x/(a*x+b*z) + b*y/(a*x+b*y)]
In:
Simplify[Together[a*x/(a*x+b*z) + b*y/(a*x+b*y)]]
Special Functions
A number of mathematical functions such as trigonometric functions, exponentials, and
logarithms are built-in to Mathematica. Here are some examples. Do you see how the following
calculations make sense when done in the order indicated? Note that the Log function calculates
natural logarithms (base e).
In:
x = Sin[2*Pi*w*t-]
In:
y = Cos[2*Pi*w*t-]
In:
x^2 + y^2
5
In:
Simplify[x^2 + y^2]
In:
x/y
In:
z = Log[u^2 + 6]
In:
Exp[z]
In:
Exp[2*t]/Exp[3*t]
Differentiation
When an expression contains a symbol, you can differentiate with respect to the variable
represented by that symbol. The D function is used, with the expression and the variable entered
as input in that order, separated by a comma. If the expression contains several variables, the D
function calculates the partial derivative with respect to the specified variable.
Examples:
In:
D[t^2 + 3*t +7,t]
In:
D[Cos[2*Pi*w*t-], t]
In:
D[y,t]
In:
D[Exp[r*t],t]
Substitution
The symbol combinations “/.” and “->” are used to make algebraic substitutions into symbolic
expressions. If you are following along with this document and doing calculations in
Mathematica, use the following input to remove the previous definition for the variable x.
In:
Remove[x]
Or you may close the program and restart it with a new, blank notebook.
When making substitutions, type the expression involved followed by the “/.” symbol
combination, and then specify the symbol to be replaced by substitution, the “->” key
combination, and the value to substitute.
Examples of substituting x = 0 into a quadratic equation for x:
6
In:
Out:
a*x^2 + b*x + c /. x -> 0
c
Other examples:
In:
Out:
a*x^2 + b*x + c /. x -> x - x0
c + b (x – x0) + a (x – x0)2
In:
Out:
Exp[r*t]/.t->0
1
Lists
In Mathematica, a list is a set, usually a set of symbols or symbolic expressions, enclosed by
curly brackets.
{a, x, u} = a list of variables a, x, and u.
{c, bx + c, ax2 + bx + c} = a list of symbolic expressions.
You can use a list to make multiple substitutions in an expression.
In:
Out:
a*e*n*p – d*p /. {n -> k, p -> 0}
0
And to make multiple substitutions into multiple expressions.
In:
Out:
{a*e*n*p – d*p, r*n*(1-n/k) – a*n*p}
/. {n -> k, p -> 0}
{0,0}
Solution of Equations
One equation for one variable – The Solve function is used with two inputs separated by a
comma, first an expression specifying the equation, then the variable to solve for.
In:
Out:
Solve[a*x + b == 0,x]
b
{{x →  }}
a
7
In:
Out:
Solve[a*x/(b+x) - c == 0,x]
bc
{{x →
}}
a  c
When equations have multiple solutions, they all appear in a list:
In:
Solve[a*x^2 + b*x + c == 0,x]
Out:
{{x →
b 
b2  4 a c
2a
},{ x →
b 
b2  4 a c
2a
}}
Mathematica reports imaginary and complex solutions when they occur:
In:
Solve[a*x^2 + c == 0,x]
Out:
{{x → 
i c
i c
},{ x →
}}
2 a
2 a
Equations to be solved can include special functions. Mathematica will not be able to solve some
of them, and for others it will not find all possible solutions, and it will give a warning in such
cases.
In:
Solve[n0*Exp[r*t] == 2*n0,t]
Solve::ifun: Inverse functions are being used by _Solve_, so some solutions may not be found; use Reduce for complete
solution information. 
Log[2]
}}
r
Out:
{{x →
In:
Solve[Cos[2*Pi*w*t - ] == 0,t]
Solve::ifun: Inverse functions are being used by _Solve_, so some solutions may not be found; use Reduce for complete
solution information. 



 
2
},{ x → 2
}}
2 w
2 w
 
Out:
{{t →
In:
Solve[a*Log[(b+c)/(b+c*Exp[-k*w])]-d*w == 0,w]
Solve::nsmet: This system cannot be solved with the methods available to Solve. 
Out:
Solve[-d w + a Log[
b  c
]== 0,w]
b  c ek w
Systems of equations – Mathematica can solve systems of n equations for n unknowns. The
system of equations is specified as input in a first list, and the variables to solve for are specified
in a second list.
In:
Solve[{a11*x1 + a12*x2 + b1 == 0,
a21*x1 + a22*x2 + b2 == 0},{x1,x2}]
8
Out:
{{x1 →
a22 b1  a12 b2
a21 b1  a11 b2
, x2 →
}}
a12 a21  a11 a22
a12 a21  a11 a22
Vectors and Matrices (Advanced Topic)
Mathematica can be cumbersome for many matrix and vector calculations. Especially for
numerical calculations, MATLAB is often better. But Mathematica can be useful when analyzing
matrices symbolically.
A vector is specified as a list of elements.
In:
Out:
xvec = {x1,x2,x3}
{x1,x2,x3}
a 
a
A matrix is a list of lists, where each sub-list is a row of the matrix. The 2×2 matrix  11 12 
 a21 a22 
is specified as
In:
Out:
amat = {{a11,a12},{a21,a22}}
{{a11,a12},{a21,a22}}
Similarly, a 3×3 matrix is specified as
In:
Out:
bmat = {{b11,b12,b13},{b21,b22,b23},{b31,b32,b33}}
{{b11,b12,b13},{b21,b22,b23},{b31,b32,b33}}
The Determinant function calculates the determinant of a matrix
In:
Out:
Det[amat]
-a12 a21 + a11 a22
The Eigenvalue function calculates the eigenvalues of a matrix
In:
Out:
Eigenvalues[amat]
1
{ a11  a22  a112  4 a12 a21  2 a11 a22  a222 ,
2
1
a11  a22  a112  4 a12 a21  2 a11 a22  a222 }
2




An identity matrix of a given dimension ins constructed with the IdentityMatrix function:
In:
IdentityMatrix[2]
9
Out:
{{1,0},{0,1}}
Using this, the characteristic polynomial of a matrix can be calculated.
In:
Det[amat – *IdentityMatrix[2]]
Out:
-a12 a21 + a11 a22 – a11  – a22  + 2
Instead of directly calculating eigenvalues, the characteristic equation can be solved:
In:
Out:
Solve[Det[amat – *IdentityMatrix[2]]==0,]
1
{ a11  a22  a112  4 a12 a21  2 a11 a22  a222 ,
2
1
a11  a22  a112  4 a12 a21  2 a11 a22  a222 }
2




10
Exercises:
1. Calculate the following quantities, and obtain results in whole numbers and fractions, and then
in decimal numbers.
1244 + 356
54
3÷7
12 × 4
2. A circle has radius 12.
Calculate its circumference and its area in whole numbers and fractions.
Calculate its circumference and its area in decimal numbers.
3. Do the same for a circle of radius 120,000,000,000,000.
4. Differentiate the expression

max RN
kR
with respect to R and put the result in simplest terms (you will need to use the Simplify
and Together functions).
Then, evaluate it with the substitution R = S.
5. Differentiate the expression
 N
N 
f1  r1 N1 1  1  12 2 
K1 
 K1
with respect to N1 and put the result in simplest terms.
Then, evaluate it with the substitution N1 = 0 and N2 = K2.
6. Differentiate the expression
f  aNT KN  a( K  NT ) N 2  aN 3
with respect to N and put the result in simplest terms.
Then evaluate it (separately) with each of these substitutions: N = 0, N = K and N = NT.
11
7. Differentiate the expression
g
N
1  N
with respect to N and put the result in simplest terms.
Then evaluate it (separately) with each of these substitutions: N = 0, N =
 1
.

8. Evaluate the expressions
 N
N 
f1  r1 N1 1  1  12 2 
K1 
 K1
and
 N
N 
f 2  r2 N 2 1  2   21 1 
K2 
 K2
with each of the following substitutions
N1 = 0, N2 = 0
N1 = K1, N2 = 0
N1 = 0, N2 = K2
Differentiate f1 with respect to N1 and evaluate the result with the same substitutions.
Differentiate f1 with respect to N2 and evaluate the result with the same substitutions.
Differentiate f2 with respect to N1 and evaluate the result with the same substitutions.
Differentiate f2 with respect to N2 and evaluate the result with the same substitutions.
9. Solve the following equations or systems of equations for the variable(s) indicated.
 N
rN 1    0 , for N
 K
 N
 N
N 
N 
r1 N1 1  1  12 2   0 and r2 N 2 1  2   21 1   0 , for N1 and N2
K1 
K2 
 K1
 K2
aeNP
aNP
 N
 dP  0 , for N and P
rN 1   
 0 and
1  ahN
 K  1  ahN
12
10. Calculate the determinant, eigenvalues, and characteristic polynomial for the matrix
 b11 b12 b13 
b b

 21 22 b23 
b31 b32 b33 
Does the eigenvalues function give the same result as solving the characteristic equation?
(Note that both the Eigenvalues function and the solution of the characteristic polynomial
result in eigenvalues that can be complex numbers, but this result is reported differently
by the two procedures.)
11. Calculate the determinant, eigenvalues, and characteristic polynomial of the matrix
rd

  aeK

 r (aeK  d )
aK

d
 
e

0 

Compare the output of the Eigenvalues function to the solution of the characteristic
equation.
13
Download