Mathematica Demo #1

Arithmetic

Shift-Enter ->  Calculate (inputs any calculation you would like performed)

"+"  ->  Addition
"-"  ->  Subraction
"*" or " "  ->  Multiplication
"/"  ->  Division
"^"  ->  Exponent

Examples:

2.3 + 5.63

(2.4/8.9)^2

2 3 4

(3 + 4)^2 - 2 (3 + 1)

Order of execution is always by standard convention.  Exponents first, followed by multiplication and division, then lastly addition and subtraction.  If two operations are of the same priority, parenthetical operations are performed first if applicable, or the operation that comes first sequentially.

Scientific Notation

One enters a number in scientific notation in the following way:

2.3*10^70

Exact and Approximate Results

Mathematica will generally default to an exact answer; however, this is not always the most desirable format.  The "//N" or "N[expr]" switch allows the program to approximate the solution to a fixed number of decimal places.  It is specifically defined in the following way:

?N

Note that ?Function gives the help description of any function or command.

Examples:

2^100

N[2^100]

2^100 // N

N[2^100, 15]

Similarly, this switch will transform rational fractions to decimal form, or a square root from its exact symbolic form to a decimal form.

(1/3) + (2/7)

N[(1/3) + (2/7)]

Lastly, note the differences in output depending on how the input is entered.  In particular, note the effect of adding a decimal point to change the argument from an integer to a real number.

Sqrt[2]

N[Sqrt[2]]

Sqrt[2. ]

Common Mathematical Functions

Sqrt[x]  ->  square root
Exp[x]  ->  exponential
Log[x]  ->  natural logarithm
Log[b,x]  ->  logarithm to base b
Sin[x], Cos[x], Tan[x], Csc[x], Sec[x], Cot[x]  ->  trigonometric functions (x in radians)
ArcSin[x], ArcCos[x], ArcTan[x], ArcCsc[x], ArcSec[x], ArcCot[x]  ->  inverse 									trigonometric functions (results in radians)
Sinh[x], Cosh[x], Tanh[x], Csch[x], Sech[x], Coth[x]  ->  hyperbolic functions
ArcSinh[x], ArcCosh[x], ArcTanh[x], ArcCsch[x], ArcSech[x], ArcCoth[x]  ->  inverse 								hyperbolic functions
n!  ->  factorial
Abs[x]  ->  absolute value
Round[x]  ->  closest integer to x
FactorInteger[x]  ->  prime factors of n

Examples:

Sqrt[65]

Sqrt[65. ]

Log[56. ]

Log[10, 1353. ]

Sin[(Pi/2)]

6!

FactorInteger[63]

Note that all Mathematica functions begin with capital letters, and that their arguments are enclosed in square brackets.

Mathematical Constants

Pi  ->  pi (3.14159....)
E  ->  e (2.71828....)
Degree  ->  Pi / 180 (converts degrees to radians)
I  ->  Sqrt[-1] (imaginary number)
Infinity  ->  infinity
EulerGamma  ->  Euler's constant (0.577216....)

Examples:

Pi

N[Pi]

N[Pi, 40]

Complex Numbers

As mentioned in the previous section, complex numbers may be denoted by adding the constant I.

?I

Sqrt[-4]

N[Exp[2 + 9 I]]

Useful Commands for Complex Numbers:

Re[z]  ->  real part of z
Im[z]  ->  imaginary part of z
Conjugate[z]  ->  complex conjugate of z
Abs[z]  ->  absolute value (magnitude) of z
Arg[z]  ->  argument (phase) of z

Examples:

N[Re[Exp[2 + 9 I]]]

N[Im[Exp[2 + 9 I]]]

N[Conjugate[Exp[2 + 9 I]]]

N[Abs[Exp[2 + 9 I]]]

N[Arg[Exp[2 + 9 I]]]

Referencing Previous Results

Oftentimes, it is convenient to build upon previous results in performing calculations.  The following tricks let one do so.

%  ->  the last result generated
%%  ->  the next-to-last result generated
%%...% (k times)  ->  the kth previous result
%n  ->  the result on output line Out[n] (careful with this one!!!)

?%

Examples:

77^2

% + 1

3 % + %^2 + %%

In using the notebook format in Mathematica, you have to be extremely careful using these symbols to reference previous results.  This is because "%" references the last output generated, regardless of whether that result appears immediately above your present position in the notebook.  Thus, the ability to scroll back in the notebook makes the use of this command a risky endeavor.

Defining Variables

Since the previous section points out the dangers of using "%" to reference previous calculations, a better alternative is to define a variable.

x = value  ->  assign a value to the variable x
x = y = value  ->  assign a value to both x and y
x = .  or  Clear[x]  ->  remove any value assigned to x

?=

?=.

Examples:

x = 5

x^2

x = 7 + 4

x =.

x

The value assigned to a variable will remain for your entire Mathematica session unless it is explicitly cleared.  Thus, to avoid confusion, it is wise to remove variables once you are finished using them.

The only restriction in naming variables is that they cannot start with numbers.  They can have numbers in their definition, but the first character must be a letter.  It is also recommended that the first character not be a capital letter, to avoid confusion with existing Mathematica functions.

Examples:

x y  ->  implies x times y
xy  ->  implies the variable named xy
5x  ->  implies 5 times x (since variables cannot begin with a number)
x^2y  ->  implies (x^2)y, not x^(2y)

Making Lists of Objects

Sometimes it is convenient to gather together several objects and treat them as a single entity.  This can be done with lists, which are formed using curly brackets "{}".

?{

Examples:

x = {3, 5, 1}

x^2 + 1

There are some commands for manipulating portions of lists:

Part[list, i] or list[[i]]  ->  the ith element of a list
Part[list,-i] or list[[-i]]  ->  the ith element from the end of a list
Part[list, {n1, n2, ...}] or list[[{n1, n2,...}]]  ->  the list of elements at positions n1, n2,...
Part[list,i] = value  or  list[[i]] = value  ->  reset the ith element of a list

First[list]  ->  the first element in a list
Last[list]  ->  the last element in a list

Take[list, n]  ->  the first n elements in a list
Take[list, -n]  ->  the last n elements in a list
Take[list, {m, n}]  ->  elements m through n (inclusive) in a list

Rest[list]  ->  a list with its first element dropped
Drop[list, n]  ->  a list with its first n elements dropped
Drop[list, -n]  ->  a list with its last n elements dropped
Drop[list, {m, n}]  ->  a list with elements m through n (inclusive) dropped

Sequences of Operations

You will typically perform a series of calculations in Mathematica by typing each on a separate line.  However, this is not always necessary, as shown below.

?;

Examples:

x = 4; y = 6; z = y + 6

The use of a semi-colon has the effect of separating a sequence of calculations and only outputing the result for the final one in this case.  The semi-colon can also be used to avoid an output for a single calculation.

Examples:

x = 67 - 5;

x

x =.

Symbolic Algebraic Computations

Mathematica has the ability to take algebraic expressions or equations and manipulate them symbolically.  This is demonstrated below:

Examples:

3 x - x + 2

There are also a variety of commands for manipulating these expressions:

Expand[expr]  ->  multiply out products and powers
ExpandAll[expr]  ->  apply Expand everywhere
Factor[expr]  ->  write an expression as a product of minimal factors
Together[expr]  ->  put all terms over a common denominator
Apart[expr]  ->  separate into terms with simple denominators
Cancel[expr]  ->  cancel common factors between numerators and denominators
Simplify[expr]  ->  try to find the simplest form of an expression by applying various standard
                                    algebraic transformations                                 
Collect[expr, x]  ->  group together powers of x
FactorTerms[expr, x]  ->  pull out factors that do not depend on x

Examples:

y = (x^2 + 2 x + 1) (x - 2)^2

z = Expand[y]

Factor[z]

y =. ; z =. ;

y = (((x - 1)^2 (2 + x))/((1 + x) (x - 3)^2))

z = Expand[y]

z = ExpandAll[y]

a = Together[z]

Apart[a]

x =. ; y =. ; z =. ; a =. ;

Here are a couple more expressions:

ComplexExpand[expr]  ->  perform expansions assuming that all variables are real
PowerExpand[expr]  ->  transform (xy)^p into x^p y^p, etc.

Examples:

ComplexExpand[Sin[x + I y]]

PowerExpand[Sqrt[x y]]

There are still other commands for manipulating algebraic expressions:

Coefficient[expr, form]  ->  coefficient of form in expr
Exponent[expr, form]  ->  maximum power of form in expr
Numerator[expr]  ->  numerator of expr
Denominator[expr]  ->  denominator of expr

Examples:

z =. ; y =. ; x =. ;
z = Expand[(1 + 3 x + 4 y^2)^2]

Coefficient[z, x]

Exponent[z, y]

z = ((1 + x)/(2 (2 - y)))

Denominator[z]

Values for Symbols

Often when a symbolic expression is created, one wants to know what the result would be if a particular value were substituted in for the variable.  This can be done as follows:

?/.

?->

Examples:

1 + 2 x /. x -> 3

1 + x + x^2 /. x -> 2 - y

(x + y) (x - y)^2 /. {x -> 3, y -> 1 - a}

t = 1 + x^2

t /. x -> 2

t /. x -> 5 a

Symbolic Differentiation

?D

Examples:

D[ArcTan[x], x]

D[x^n, {x, 3}]

Integration

Symbolic

?Integrate

Examples:

Integrate[x^n, x]

Integrate[((2 Exp[-x^2])/Sqrt)[Pi], x]

Integrate[x^2 + y^2, {x, 0, 1}, {y, 0, x}]

Numerical

?NIntegrate

Examples:

Integrate[Sin[Sin[x]], {x, 1, 2}]

NIntegrate[Sin[Sin[x]], {x, 1, 2}]

Sums and Products

Symbolic

?Sum

?Product

Examples:

Sum[x^i/i, {i, 1, 7}]

Sum[x^i/i, {i, 1, 7, 2}]

Product[x + i, {i, 1, 4}]

Numerical

?NSum

?NProduct

Examples:

Sum[1/i^4, {i, 1, Infinity}]

NSum[1/i^4, {i, 1, Infinity}]

Equations

Equations are entered in Mathematica using the "==" notation.

?==

Examples:

2 + 2 == 4

2 + 2 == 5

(x^2) + 2 x - 7 == 0

Other Logical Operators

There are other logical operators that Mathematica uses:

!=  ->  not equal to
>  ->  greater than
>=  ->  greater than or equal to
<  ->  less than
<=  ->  less than or equal to

Examples:

3 < 5 < 6

Solving Equations

Symbolic

?Solve

Examples:

Solve[x^2 + 2 x - 7 == 0, x]

Solve[x^2 + 1 == 0, x]

Numerical

?NSolve

Examples:

Solve[x^5 + x + 1 == 0, x]

NSolve[x^5 + x + 1 == 0, x]

Solving Differential Equations

Symbolic

?DSolve

Examples:

DSolve[y'[x]==a y[x] + 1,y[x],x]

DSolve[{y'[x]==a y[x] + 1,y[0] == 0},y[x],x]

Numerical

?NDSolve

?Plot

?Evaluate

Examples:

z=NDSolve[{y'[x]==y[x],y[0] == 1},y,{x, 0, 2}]

y[1.5] /. z

Plot[Evaluate[y[x] /. z], {x, 0, 2}]

Power Series

?Series

Examples:

Series[Cos[x], {x, 0, 10}]

Limits

?Limit

Examples:

t = Sin[x]/x

Limit[t, x -> 0]

Functions

Functions are defined using the ":=" symbol.  To limit confusion, it is also recommended that you use a lower case character for the first letter of a function.

?:=

The argument of the function is typically defined as any character followed by the underline symbol.  This basically makes the argument of the function a wild card, allowing one to substitute any number or variable into the function.

?_

Examples:

f[x_] := (x^2)

f[a + 1]

f[4]

Clear[f]

f[x_, xmax_] := ((((x - xmax))^2)/xmax)

2 + f[x, 3.5]

?f

Clear[f]

Basic Plotting

?Plot

Examples:

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

Plot[Tan[x], {x, -3, 3}]

Plot[{Sin[x], Sin[2 x], Sin[3 x]}, {x, 0, 2 Pi}]

plt1 = Plot[Sin[x], {x, 0, 2  Pi}] ; 
    plt2 = Plot[Sin[2  x], {x, 0, 2  Pi}] ; 
    plt3 = Plot[Sin[3  x], {x, 0, 2  Pi}] ;

?Show

Show[plt1, plt2, plt3]

There are sometimes better ways to make plots, depending on the circumstances.  Listed below are the various ways plots can be constructed:

Plot[f, {x, xmin, xmax}]  ->  first choose specific numerical values for x, then evaluate f for
				    each value of x.

Plot[Evaluate[f], {x, xmin, xmax}]  ->  first evaluate f, then choose specific numerical
					           values of x.

Plot[Evaluate[Table[f, ... ]], {x, xmin, xmax}]  ->  generate a list of functions, and then plot
							      them.

Plot[Evaluate[y[x] /. solution], {x, xmin, xmax}]  ->  plot a numerical solution to a
							          differential equation obtained from
							          NDSolve.

<< Graphics`PlotField`

?PlotVectorField

f[x_, y_] := ((y Cos[x])/(1 + 2 (y^2)))

plt1 = 
      PlotVectorField[{1, f[x, y]}, {x, -3, 3}, {y, -1, 2}, 
        Frame -> True]

Just for fun!

?ParametricPlot3D

ParametricPlot3D[{t, u, Sin[t*u]}, {t, 0, 3}, {u, 0, 3}]