Mathematica: Getting Started
PHYS 460/660 @ UDelaware

Reference: Heikki Ruskeepaa, Mathematica Navigator (Academic Press, San Diego, 2004).

To open a section, double click on a cell bracket at the right side of the screen that has a hook on its bottom end like this ⥡

0. Initialization

ClearAll["Global`*"]

1. Introduction

In 1903 at a meeting of the American Mathematical Society, F. N. Cole read a paper entitled “On the Factorization of Large Numbers.” When called upon to speak, Cole walked to the board and, saying nothing, raised two to its sixty-seventh power and subtracted one from the answer. Then he multiplied, longhand, 193,707,721 by 761,838,257,287 and the answers agreed. Without having said a word, Cole sat down to a standing ovation. Afterwards he announced that it had taken him twenty years of Sunday afternoons to factorize the Mersenne number (2^67)-1. — Edward Sexton

2^67-1

FactorInteger[2^67-1]//Timing

FactorInteger[2^227-1]//Timing

MaxMemoryUsed[]

PrimeQ[2^227-1]//Timing

Getting quick HELP within the notebook:

?Prime*

??PrimeQ

2. Arithmetics: Exact versus Numerics

({{Basic Arithmetic}, {({{         a+b             }, {         a-b}, {    a*b or ab
    a/b
    a^b}})}})

1260/1530

1260/1530//N

N[Pi,100]

Important constants: Pi, E, I, Infinity

N[E]

I^2

1/Infinity

({{Naming Variables}, {({{Assign value of a, a=value}, {Show the value of a, a}, {Clear the value of a, a=.}})}})

a=Binomial[6,2]

b=(1/6)^2*(5/6)^4

c=a*b

a=.

a^2

3. Basic Functions

({{Standard Mathematical Functions}, {({{exponential, Exp[x]}, {natural logarithm, Log[x]}, {logarithm to base b, Log[b,x]}, {trigonometric , Sin,Cos,Tan,Sec,Csc,Cot}, {inverse trig , ArcSin,ArcCos,ArcTan,}, { , ArcSec,ArcCsc,ArcCot}, {hyperbolic trig, Sinh,Cosh,Tanh,Sech,Csch,Coth}, {inverse hyperbolic trig, ArcSinh,ArcCosh,ArcTanh,}, { , ArcSech,ArcCsch,ArcCoth
}})}})

Sin[Pi/6]

Sin[30 Degree]

ArcCos[1/Sqrt[2]]

a=Table[Random[Integer,{1,6}],{10}]

Max[a]

User defined functions:

f[x_]:=x/(n+x)

When defining a function, it is important to remember the underscore _ after each argument. The underscore makes the function capable of calculating the value of the function for any value of the argument. The colon ( : ) before the equals sign results in the value of expr not being calculated in the definition; the value is calculated only when using the function.

f[sin[x]]

4. Symbolic Algebra: Solving Equations and Symplifying Expressions

({{Manipulating Expressoions}, {({{exponential, Simplify[expr]}, {natural logarithm, FullSimplify[expr]}, {logarithm to base b, Factor[expr]}, {trigonometri, Expand[expr]}, {inverse trig , Appart[expr]}})}})

a=(1+x)*(2+x)+(1+x)^2

a//Simplify

a//Factor

a//Expand

n!/(n-1)!//Simplify

n!/(n-1)!//FullSimplify

(1+x+x^2-x^3)/(x+2)^2//Apart

expr=Sin[x]*Cos[x]

expr/.x->π/6

x

Clear[a,b,c]

eqn=a* (x^2)+b *x+c==0

({{Symbolic Solutions}, {({{An equation (==can be written as==), expr1==expr2}, {Solve a (polynomial) equation with respect to x, Solve[eqn,x]}, {Solve two (polynomial) equations with respect to x  and y, Solve[{eqn1,eqn2},{x,y}]}})}})

sol=Solve[eqn,x]

x/.sol

eqn/.sol//Simplify

Solve[{2x+5y==4,x-3y==3},{x,y}]

Solve[Cos[x]==0,x]

Use Reduce to get completely general results

Reduce[Cos[x]==0,x]

Reduce[a (x^2)+b x+c==0,x]

Numerical methods for solving algebraic equations:

eqn2=x^5-x^3+x^2-2==0

Solve[eqn2,x]

NSolve[eqn2,x]

Solve[Exp[a*x]==b,x]

f=Exp[-x]-0.5*x

Plot[f,{x,0,3}]

FindRoot[f==0,{x,1}]

f/.%

5. Basic Calculus: Derivatives and Integrals

D[x Sin[x],x]

Integrate[p x/(q+r x),x]

D[%,x]

% // Simplify

Integrate[f[x],x]

Integrate[x^2* Exp[x],{x,0,1}]

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

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

Computer Algebra packages are not omnipotent and sometime require human assistance in most trivial cases:

Integrate[2*x*(x^2+1)^24,x]

Factor[%]

FullSimplify[%]

Factor[%+1/25]

Sums are calculated like integrals :

Sum[1/n^2,{n,1,∞}]

6. Graphics: 2D and 3D Plots

Plot[Exp[-x]*Sin[2x],{x,0,2π}]

Plot[{Sin[x],Cos[x]},{x,0,2π}]

f=Sin[x^2]*Cos[Sqrt[y]]

Plot3D[f,{x,0,3},{y,0,4}]

ContourPlot[f,{x,0,3},{y,0,4},PlotPoints->40]

7. Special Functions of Mathematical Physics

({{Common Special Functions}, {({{Gamma, Gamma[z], Gamma[a,z]}, {error, Erf[z], Erfc[a,z],Erfi[z]}, {Fresnel integrals, FresnelS, FresnelC}, {sine, cos integrals, SinIntegral, CosIntegral}, {Legendre, LegendreP[n,x], LegendreQ}, {spherical harmonics, SphericalHarmonicY[λ,m,θ,ϕ]}, {Bessel, BesselJ[n,z],BesselY}, {modified Bessel, BesselK[n,z],BesselI}, {Laguerre, LaguerreL}, {Hermite, HermiteH[n,x]}, {hypergeometric, HypergeometricPFQ}})}})

Plot[BesselJ[2,x], {x,0,20}]

Table[HermiteH[n,x],{n,0,10}]

LaguerreL

Special functions also appear when doing complicated integrals:

si=Integrate[Sin[Exp[x]],x]

D[si,x]

si/.x->1.

Plot[si,{x,0,3}]