Mathematica Demo

Ken Levasseur
December 7, 2006

Using Built-in Capabilities

System Functions

All of the following can be done by just opening the basic Mathematica system.

Calculus

D[Exp[-(s^2  +  t^2)], t]

Integrate[Sin[x/2] (E^(-3 x)), {x, 0,  Infinity}]

Linear Algebra

A=(({{5, 4, 7, 23, 5}, {-32, 1, 0, 3, 1}, {2, 8, 1, 2, -17}}))

RowReduce[A]

Solve[A . Array[x, {5}] == {1, 1, 2}, Array[x, {5}]]

B = A . Transpose[A]

Eigenvalues[N[B]]

Eigenvectors[N[B]]

Number Theory

p = Prime[10^6]

Mod[2^(p - 1), p]

q = 29341

Mod[2^(q - 1), q]

PrimeQ[q]

Differential Equations

DSolve[x''[t] - 6  x'[t] - 16  x[t] == 0, x[t], 
      t]

DSolve[{x''[t] - 6  x'[t] - 16  x[t] == 0, 
        x[0] == 5, x'[0] == -1}, x[t], t]

DSolve[x''[t] == 6 t x'[t] - 4, x[t], t]

Statistics

data = Table[Random[Real, {0, k}], {k, 1, 20}]

Mean[data]

StandardDeviation[data]

Graphics

p = CharacteristicPolynomial[B, x]

Plot[p, {x, 0, 1500}]

Plot3D[
      Exp[-(s^2  +  t^2)], {s, -2, 2}, {t, -2, 
        2}]

ListPlot[data, PlotJoined -> True]

Packages

Units

<< Miscellaneous`Units`

?Miscellaneous`Units`C*

Convert[1 Gallon, Tablespoon]

Convert[161.0 Pound,  Stone]

Convert[120.  Feet 250 Feet,  Acre]

ContinuousDistributions

<<Statistics`ContinuousDistributions`

?Statistics`ContinuousDistributions`*

?Statistics`Common`*`*

expd = ExponentialDistribution[1.3]

PDF[expd, x]

Mean[expd]

edata = Table[Random[expd], {100}]

Mean[edata]

Learning More Mathematica

Definition and Information functions

?Integrate

?? Integrate

Help Browser

Access from link generated by Definition or Information

or

Open directly from the Help menu

Programming

Given a set of points S, find a point P that minimizes the sum of the distances to the points in S.

S = 
      Table[{Random[Integer, {0, 100}], 
          Random[Integer, {0, 100}]}, {8}]

Show[Graphics[Map[Point, S]]]

SImage = 
      Show[Graphics[{PointSize[0.02], Map[Point, S]}], 
        PlotRange -> {{0, 100}, {0, 100}}, 
        AspectRatio -> Automatic, Background -> Yellow]

distance[A_, B_] := Sqrt[Apply[Plus, (A - B)^2]]

distance[{0, 0}, {1, 1}]

distance[{1, -1, 1}, {4, 5, -6}]

g[{x_, y_}] := 
      Sum[distance[{x, y}, S[[k]]], {k, 1, Length[S]}]

g[{x, y}]

g[{0, 0}]

g[{x_, y_}] := 
      N[Sum[distance[{x, y}, S[[k]]], {k, 1, Length[S]}]]

g[{0, 0}]

DensityPlot[g[{x, y}], {x, 0, 100}, {y, 0, 100}, PlotPoints -> 40, 
      ColorFunction -> Hue, Mesh -> False]

?FindMinimum

solution = FindMinimum[g[{x, y}], {x, 0}, {y, 0}]

P = {x, y} /. solution[[2]]

g[P]

Show[{SImage, Graphics[{PointSize[0.03], Red, Point[P]}]}]

ContourPlot[g[{x, y}], {x, 0, 100}, {y, 0, 100}]

The calculations  that determine the "center" can be encapsulated into a Module, with the intermediate functions being local to the Module.

CenterPoint[
        S_List] := 
Module[{distance, g, 
          solution}, 
  distance[A_, B_] := 
          Sqrt[Apply[Plus, (A - B)^2]]; 
   g[{x_, 
              y_}] := N[
            Sum[distance[{x, y}, S[[k]]], {k, 1, 
                Length[S]}]]; 
solution = 
          FindMinimum[g[{x, y}], {x, Random[]}, {y, Random[]}, 
            AccuracyGoal -> 6]; 
{x, y} /. 
          solution[[2]]]

CenterPoint[S]

CenterPoint[{{0, 0}, {0, 100}, {100, 0} }]

CenterPoint[
      Map[{50 + Cos[#], 50 + Sin[#]} &, Range[Pi/6, 2  Pi, Pi/6]]]