Chapter 2
A Brief Overview of Mathematica

from
Introduction to Programming with Mathematica,
2nd Ed.

by
Richard J. Gaylord
Samuel N. Kamin
Paul R. Wellin

Copyright 1995 TELOS/Springer-Verlag

Initialization (evaluate to turn off spell messages)

Off[General::spell]

Off[General::spell1]

2.1  Numerical and Symbolic Computation

2/3 + 4/7

1111111^2

9348 * 437 - 923874^3 + 378/2346 (1283 - 3764)

2^500

N[2^500]

Sqrt[75]

N[%]

N[E]

N[Pi]

Sqrt[-9]

N[EulerGamma]

N[Pi, 35]

Log[E]

Log[2, 1024]

Cos[Pi/3]

ArcTan[1]

Expand[Sin[x]^2, Trig -> True]

Solve[a x^2 + b x + c == 0, x]

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

FindRoot[Cos[x] == x, {x, 1.0}]

Integrate[1/(1 - x^3), x]

D[%, x]

Simplify[%]

D[Log[x], {x, 2}]

D[f[g[x]], x]

Log'[x]

FullForm[ f'[x] ]

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

Integrate[E^Cos[x], {x, 0, Pi}]

NIntegrate[E^Cos[x], {x, 0, Pi}]

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

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

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

2.2  Functions

2.2.1 Functions of Number Theory

Mod[28, 5]

Divisors[28]

EulerPhi[28]

Expand[(a + b)^2]

Expand[(a + b)^3]

Expand[(a + b)^4]

Binomial[4, 3]

Table[Binomial[4, j], {j, 0, 4}]

TableForm[ Table[Binomial[n, k], {n, 0, 8}, {k, 0, n}] ]

Table[Binomial[n, 2], {n, 1, 10}]

Binomial[n + 1, 2]

2.2.2  Functions of Linear Algebra

vec = {0, 1, 3, 1}

5 vec

vec + {1, 2, 3, 4}

mat = {{a, b}, {c, d}};

MatrixForm[mat]

3 mat

MatrixForm[mat . mat]

matf = {{.324, 1.3}, {.01, 5.8}};

invmatf = Inverse[matf]

matf . invmatf

Chop[%]

M = {{2, 3}, {-1, 5}};

X = {x, y};

B = {8, 1};

Inverse[M] . B

M . % == B

LinearSolve[M, B]

2.2.3  Random Numbers

Random[Real, {0, 5}]

Random[Real]

Random[]

Random[Integer, {0, 100}]

SeedRandom[128]

{Random[], Random[], Random[]}

SeedRandom[128]

{Random[], Random[], Random[]}

2.2.4  Packages

Needs["Miscellaneous`ChemicalElements`"]

Wolfram

Abbreviation[Wolfram]

AtomicWeight[W]

HeatOfVaporization[W]

ElectronConfiguration[W]

?ElectronConfiguration

DeclarePackage["Miscellaneous`ChemicalElements`",
   {"AtomicWeight", "ElectronConfiguration"}]

2.3  Graphics

2.3.1  Two-Dimensional Plots

Plot[x + Sin[x], {x,-5Pi,5Pi}];

Plot[Sin[1/x], {x,-1,1}]

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

2.3.2  Parametric Plots

circ = ParametricPlot[{Cos[t], Sin[t]}, {t,0,2Pi}]

N[1/GoldenRatio]

Show[circ, AspectRatio -> Automatic]

ParametricPlot[{Cos[19t], Sin[20t]}, {t, 0, 2Pi},
   AspectRatio -> Automatic]

2.3.3  Three-Dimensional Plots

z[x_, y_] := Sin[x]/Cos[y]

Plot3D[z[x, y], {x, -2Pi, 2Pi}, {y, -2Pi, 2Pi}]

Plot3D[z[x, y], {x, -2Pi, 2Pi}, {y, -2Pi, 2Pi},
      PlotPoints -> 50]

ContourPlot[z[x, y], {x, -2Pi, 2Pi}, {y, -2Pi, 2Pi}]

DensityPlot[z[x, y], {x, -2Pi, 2Pi}, {y, -2Pi, 2Pi},
           PlotPoints -> 150, Mesh -> False]

x[u_, v_] := (1 + v/2 Cos[u/2]) Cos[u]
y[u_, v_] := (1 + v/2 Cos[u/2]) Sin[u]
z[u_, v_] := v/2 Sin[u/2]

ParametricPlot3D[{x[u, v], y[u, v], z[u, v]},
                {u, 0, 2Pi}, {v, -1, 1}]

a = 2;
f = (a + Cos[u/2]Sin[t] - Sin[u/2]Sin[2t]) Cos[u];
g = (a + Cos[u/2]Sin[t] - Sin[u/2]Sin[2t]) Sin[u];
h =      Sin[u/2]Sin[t] + Cos[u/2]Sin[2t];

ParametricPlot3D[{f, g, h}, {t, 0, 2Pi}, {u, 0, 2Pi},
   Boxed -> False, Axes -> False, PlotPoints -> 30]

2.4  Representation of Data

{{0.061, 6.951}, {0.403, 28.189}, {0.622, 41.1},

  {2.51, 120.8}, {2.96, 147.9}, {3.33, 182.8},
  
  {8.2, 368.8}, {28.2, 981.3}, {57.4, 1303.3},
  
  {72.3, 1512.5}, {340.2, 7100.3}, {711, 10101.1},
  
  {5000., 29895.}} >> dataset.m

ListPlot[<<dataset.m, PlotStyle->PointSize[.02]]

Needs["Graphics`Graphics`"]

LogLogListPlot[<<dataset.m,
   PlotStyle->PointSize[.02]]

loglogdata = Log[<<dataset.m]

Fit[loglogdata, {1, x}, x]

2.5  Programming

2.5.1  Example: Harmonic Numbers

harmonic[n_] := Sum[1/k, {k, 1, n}]

harmonic[2]

{harmonic[10], N[harmonic[10]]}

N[harmonic[10^4]]

harmonicSum[n_Integer]:= PolyGamma[n+1] + EulerGamma

Table[harmonicSum[n], {n, 10}]

Timing[ harmonicSum[10^4]; ]

2.5.2  Example: Perfect Numbers

Divisors[6]

Apply[Plus, {1, 2, 3}]

properDivisors[n_] := Drop[Divisors[n], -1]

Apply[Plus,properDivisors[6]]

perfectQ[n_Integer] := Apply[Plus, properDivisors[n]] == n

perfectQ[6]

perfectQ[16]

select[Range[30],perfectQ]

searchPerfect[n_]:= Select[Range[n], perfectQ]

searchPerfect[10000]