First Encounter with Mathematica

The FrontEnd Interface
How to Enter Calculations
Examples

CPSC 215 — L02 — Winter 2002
Christian Jacob

14 January 2002

Numerical Calculations

Enter a mathematical calculation and hit <Shift>-<Return> or the <Enter> key.

2 + 3

3!

30!

300!

3000!

Pi

N[π]

N[π, 500]

Algebra

9 (2 + x) (x + y) + (((x + y))^2)

Expand[%]

\(%%\^3\)

Simplify[%]

Expand[%]

Simplify[%]

FullSimplify[%]

Calculus

int = Integrate[(x^2) Sin[x]^2, x]

int // TraditionalForm

Examples

int = Integrate[(x^2) Sin[x]^2, x]

int // TraditionalForm

deriv = D[%, x]

Simplify[deriv]

Plot[{int, deriv}, {x, -5, 5}, 
        PlotStyle -> {Hue[0.4], Hue[0.9]}];

Plot[{int, deriv}, {x, -5, 5}, 
        PlotStyle -> {Hue[0.4], Hue[0.9]}, 
        PlotRange -> All];

Solving Equations

equation = a (x^2) + b x + c == 0

Examples

equation = a (x^2) + b x + c == 0

Solve[equation, a]

Solve[equation, x]

Solve[a (x^3) + b (x^2) + c x + d == 0, x]

Solve[a (x^4) + b (x^3) + c (x^2) + d x + e == 0, x]

Solve[(∑_(i = 0)^5) (Subscript[a, i]) (x^i) == 0, x]

Solve[(∑_(i = 1)^5) (Subscript[a, i]) (x^i) == 0, x]

Simplify[%]

NSolve[(x^5) + 2  x + 1 == 0, x]

Graphics

Examples

sinPlot = Plot[Sin[x], {x, 0, 2  π}]

\(\(Show[
        sinPlot, 
Frame -> 
          True, 
FrameLabel -> {"Signal", "t"}, 
GridLines -> Automatic];\)\)

graphic = 
        Plot3D[Sin[x + Sin[y]], {x, -3, 3}, {y, -3, 3}];

graphic = 
        Plot3D[Sin[x + Sin[y]], {x, -3, 3}, {y, -3, 3}, 
          PlotPoints -> 50, Mesh -> False];

<< RealTime3D`

Show[graphic];

<< Default3D`

graphic = 
        Plot3D[Sin[x + Sin[y]], {x, -3, 3}, {y, -3, 3}, 
          PlotPoints -> 50, Mesh -> False];

Show[ContourGraphics[graphic]];

Show[DensityGraphics[graphic]];

spiral = 
        ParametricPlot3D[{u Sin[t], u Cos[t], t/3}, 
{t, 
            0, 15}, 
{u, -1, 
            0}, 
Boxed -> False, 
          Axes -> False];

<< RealTime3D`

Show[spiral];

Animations

Examples

<< Default3D`

Table[
        Plot3D[Sin[t x + Sin[y]], {x, -3, 3}, {y, -3, 3}, 
          PlotPoints -> 50, Mesh -> False], {t, 0.1, 1, 
          0.1}];

\(\(Do[
        ParametricPlot3D[{Cos[i\ π\/50]\ Cos[τ] + 
              Sin[i\ π\/50]\ Sin[σ], Sin[τ], 
            Cos[σ]}, {σ, 0, 2\ π}, {τ, 0, 2\ π}, 
          Axes -> None, Boxed -> False, PlotPoints -> 20, 	
          PlotRange -> {{\(-1.5\), 1.5}, {\(-1.1\), 1.1}, {\(-1.1\), 
                1.1}}], {i, 0, 25}];\)\)

List Processing

Examples

Table[n!, {n, 1, 15}]

Log[%]

logs = N[%]

ListPlot[logs];

fits = Fit[logs, {1, x, (x^2)}, x]

Plot[fits, {x, 0, 15}]

Show[{%, ListPlot[logs]}];

pointsArray = 
Table[
Table[
            If[GCD[i, j] == k, 1, 0], 
{i, 
              30}, 
{j, 30}], 
{k, 1, 
            6}
];

Take[pointsArray, 1]

plotsArray = 
Table[
          ListDensityPlot[
            pointsArray[[
              k]], 
DisplayFunction -> Identity], 
{k, 1, 6}];

Show[GraphicsArray[Partition[plotsArray, 3]], 
        DisplayFunction -> $DisplayFunction];

fourierPlotsArray = 
Table[
          ListDensityPlot[
            Abs[Fourier[
                pointsArray[[
                  k]]]], 
DisplayFunction -> Identity], 
{k, 
            1, 6}];

Show[GraphicsArray[Partition[fourierPlotsArray, 3]], 
        DisplayFunction -> $DisplayFunction];

fourierContourPlotsArray = 
Table[
          ListContourPlot[
            Abs[Fourier[
                pointsArray[[
                  k]]]], 
DisplayFunction -> Identity], 
{k, 
            1, 6}];

Show[GraphicsArray[Partition[fourierContourPlotsArray, 3]], 
        DisplayFunction -> $DisplayFunction];