Mathematica

Introduction

Expressions

f[x]   or   x // f

f[x, y, z]

f is the Head, x is the Argument/s

Sin[Pi/3] // N

x^2 == Power[x, 2];  x + y == Plus[x, y]; 
    x y ==  Times[x, y];

Data

{5,  3/4,  5. ,  4 + I,  4.  + I 1. }

{Sqrt[5],  Sqrt[5. ],  Sin[Pi/4],  Sin[Pi/4. ]}

100!

100. !

{Pi,  I,  E}

% // N

{x,  "this is a string",  {1, a, 2 3,  a + a}}

Table[n^2, {n, 0, 100}]

Import["sq.dat", "Table"]

picture = Import[".jpg"]

SetDirectory["A:"]

Export["file", squares, "Table"]

Assignments

x = 5;  2 + x;  y = 2 + x; ?x; ?y

z := 2 + x;  ?z; z; x=3.; z; y;

z := 2 + x

(-b + Sqrt[b^2 - 4  a c])/(2  a)  /. {a -> 1, 
        b -> 2, c -> 3}

x =.

f[x_] := x^2; g[x_, y_] := x (y^2);

Manipulating Output

D[(x^2 - y x^3)/(x + y^2), x]

% // FullSimplify

%42 // TraditionalForm

Also Expand,  Collect,  Together,  
    Apart (see Help)

f1 = 
        Table[{{x, y}, {x^2, Sin[x y]}},  {x, 0, Pi, Pi/20}, {y, 0, Pi, 
            Pi/10}];

% // Short

Errors :  brackets,  syntax,  
      variable already defined before? Remove["x"] ... ;  
      Taking too long?  Abort; 
      To restart click on Kernel > Quit Kernel > Local

Calculus

∫\[Placeholder] \[DifferentialD]\[Placeholder];  (∫_\[Placeholder]^\[Placeholder]) \[Placeholder] \[DifferentialD]\[Placeholder];  (∑_(\[Placeholder] = \[Placeholder])^\[Placeholder]) \[Placeholder]; NIntegrate

∫Log[x] \[DifferentialD]x

(∫_0^∞) E^(-x^2) \[DifferentialD]x // 
      N

(∂_x) x

(∑_(n = 1)^∞) 1/n^2

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

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

Equations

Solve[{x + 2  y == y^2, x - y == 2}, {x, y}]

NSolve[{x + 2  y == y^2, x - y == 2}, {x, y}]

Eliminate[{x + 2  y == y^2, x - y == 2}, y]

Solve[E^x == 5  Sin[x], x]

NSolve[E^x == 5  Sin[x], x]

FindRoot[E^x == 5  Sin[x], {x, 1}]

Plotting

f : R -> R

Plot[LegendreP[4, x], {x, -4, 4}]

Plot[LegendreP[5, x], {x, -4, 4}, 
      PlotRange -> {{-2, 2}, {-2, 2}}]

Options[Plot]

Plot[{Sin[x], x - x^3/3! + x^5/5!}, {x, 0, Pi}, 
      PlotStyle -> {Hue[0], {Hue[ .7], Dashing[{0.03}]}}]

PlotRange -> {{x0, x1}, {y0, y1}}

solution = 
      DSolve[{y'[x] == 1 + y[x]^2, y[0] == 1}, y[x], 
        x]

Plot[y[x] /. solution, {x, 0, 6}]

squares = Table[n^2, {n, 0, 100}];

ListPlot[squares]

solution = 
      RSolve[{x[n] == x[n - 1] - x[n - 2]/2, x[0] == 1, 
          x[1] == 0}, x[n], n]

ListPlot[Table[x[n] /. solution, {n, 0, 40}], 
      PlotStyle -> Hue[0]]

f : R -> (R^2)

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

solution = 
      NDSolve[{x'[t] == x[t] y[t], y'[t] == 1 - x[t]^2, 
          x[0] == 1, y[0] == 2}, {x, y}, {t, 0, 10}]

ParametricPlot[{x[t], y[t]} /. solution, {t, 0, 10}]

f : R -> (R^3)

ParametricPlot3D[{Cos[t], Sin[t], t/5}, {t, 0, 20}]

ParametricPlot3D[{Cos[t], Sin[t], t/5}, {t, 0, 20}, 
      ViewPoint -> {1, 0, 4}, Axes -> False, 
      Boxed -> False]

Implicit Plots

<< Graphics`ImplicitPlot`

ImplicitPlot[
      (x^2) - 2  (y^2) == 1, {x, -2, 2}, {y, -2, 2}]

f : (R^2) -> R

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

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

DensityPlot[f[x, y], {x, 0, 5}, {y, 0, 3}, PlotPoints -> 50, 
      Mesh -> False]

ContourPlot[f[x, y], {x, 0, 5}, {y, 0, 3}]

wave = 
      NDSolve[{D[u[t, x], {t, 2}] == D[u[t, x], {x, 2}], 
          u[0, x] == 1 - x^2, u[t, 0] == 0, u[t, 2] == 0}, 
        u, {t, 0, 10}, {x, 0, 2}]

DensityPlot[u[t, x] /. wave, {t, 0, 10}, {x, 0, 2}]

temperature = 
        Table[E^(-(n^2 + n m + m^2/2)/4), {n, -5, 
            5,  .1}, {m, -5, 5,  .1}];

ListDensityPlot[temperature, Mesh -> False]

f:(R^3)->R

<< Graphics`ContourPlot3D`

ContourPlot3D[
      (x^2) + (y^2) - 2  x z, {x, -5, 5}, {y, -5, 5}, {z, -3, 
        4}]

f : (R^2) -> (R^2)

<< Graphics`PlotField`

PlotVectorField[{x y - (y^2), (y^2) - (x^2)}, {x, -5, 5}, {y, -5, 
        5}]

energy[x_, y_] := -(x^2) + (y^2)

PlotGradientField[
      energy[x, y], {x, -5, 5}, {y, -5, 5}]

ListPlotVectorField

f : (R^3) -> (R^3)

<< Graphics`PlotField3D`

PlotVectorField3D[{(x^2), y, z}, {x, -5, 5}, {y, -5, 
        5}, {z, -5, 5}]

PlotGradientField3D[-(x^2) + (y^2) + (z^2), {x, -5, 
        5}, {y, -5, 5}, {z, -5, 5}]

Movie Plots

<< Graphics`Animation`

MoviePlot[(t - x)^2, {x, 0, 2}, {t, 0, 4}]

MovieParametricPlot,  MovieDensityPlot,  MovieContourPlot

Programming

Several Commands

nthcoefficient[f_, n_] := (a = Series[f, {x, 0, n}]; 
        Coefficient[a, x^n])

nthcoefficient[Sin[x], 5]

nthcoefficient[f_, n_] := 
      Module[{a, x}, a = Series[f, {x, 0, n}]; 
        Coefficient[a, x^n]]

root[x_] := (x + 2/x)/2; x = 3.

Do[x = root[x], {10}]

x

Applying functions to lists

5 + {1, 2, 3,  4,  5,  6,  7}

Sin[{1, 2, 3, 4, 5, 6, 7}]

f /@ {1, 2, 3, 4, 5, 6, 7, {8, 9}}

In general,  
    f /@ g[x, y,  ... ] gives g[f[x], f[y],  ... ]

Sin /@ (a + b + c)

In general,  f @@ g[x, y,  ... ] gives f[x, y,  ... ]

Plus @@ {1, 2, 3, 4, 5, 6, 7}

LegendreP @@@ {{0, x}, {1, x}, {2, x}, {3, x}}

LegendreP /@ {{0, x}, {1, x}, {2, x}, {3, x}}

Nest[f, x, 5]

Nest[root, 3. , 5]

Nest[root, x, 5]

FixedPoint[f, x]

FixedPoint[root, 2. ]

Traditional Programming

If[condition,  then,  else]

If[n > 0, Plot[x, {x, 0, 2}], Plot[x^2, {x, 0, 2}]]

For[n = 1, n < 5, n++, do something]

For[n = 1; a = 0, n < 5, n++, a = a + n]

While[test, do something]