Mathematica: A short tutorial

1. Preliminaries

1 + 3

N[Pi, 2]

%

2. Variables

x

Head[x]Head[2]

x = 2

Head[x]

x = y + 1

x

Head[x]FullForm[x]

x = x + 1;

x

you can clear a variable using x=. or Clear[x] ;

x =.

x^2 Sqrt[x  +  y]/(z + y)^2

Lists

\(\(
u = {a, b, c, d, e, f}\)\)

u^2

u[[3]]

everything is an expression

2^2;  u - 1; % + 2

Symbolic manipulation

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

Simplify[%]  "or FullSimplify[%]"

substitutions

\(" expr /. {x->xval,y->yval...} "; 


    \((x + y)\)^2\ \  /. \ y -> q\)

(x + y)^2   /.  y -> u

force =  G M m/ r^2

\(acceleration = \ 
      force/m\  /. 
        \ {\ G -> 6.67\ 10^{\(-11\)}\ N\ m^2/\((kg^2)\), 
			
          M -> 6\ 10^24\ kg, r -> 7000\ *\ 10^3\ m}\)

%  /.  N -> 1 kg m/(s^2)

Differentiation, Integration, algebra;

f = x^2 - x/(x + 1)^2

D[f, x]

D[f, {x, 2}]

FullSimplify[%]

Integrate[f, x]

Integrate[f, {x, 0, 2}]

Sum[n, {i, 1, 10}]

Limit[f, x -> 10]

Solving equations

Clear[u]u == 1

u = 2

u == 1

u

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

Expand[(x + 1)^2] == x^2 + 2 x  + 1

3 != 2

x^2  +  2 x  - 7  == 0

eqn = %

Solve[eqn, x]

x  /.  %

%[[2]]

In general Solve[{eqn1,eqn2,..},{x1,x2,..}]

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

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

Differential equations

\(\(	D[y[x], {x, 2}] + y[x]\ \ \ k^2/2\  == \ 0\)\)

diffequation = %;

DSolve[diffequation, y[x], x]

" Packages ";

Numerical Mathematics

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

NIntegrate[1/Sqrt[x^2 + 1 + Cos[x]^2], {x, 0, 1}]

Nonlinear equations, Duffing eqn

D[z[x], {x, 2}] +  D[z[x], x] +  z[x] (1 + z[x]) == 0

duff = %

DSolve[duff, z[x], x]

NDSolve[{duff, z[0] == 0, z'[0] == 1}, z, {x, 0.0, 1.0}]

z[0.1]  /.  %

Plot[Evaluate[z[x]  /.  %%], {x, 0, 1}]

Functions and Programs

Sin[x]Sin[0.1]

\(
g[x] = 1/Sqrt[x^2 + 1]\)\(g[0.1]	\)

g[x_] = 1/Sqrt[x^2 + 1]\(g[0.1]	\)

g[wow]

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

g[x_] := 1/Sqrt[x^2 + 1]

g[wow]

foowave[x_, t_] := Exp[-(x - v t)^2]

procedures

Expand[Product[x + i, {i, 3}]]

exprod[n_] := Expand[Product[x + i, {i, n}]]

cex[n_, i_] := (t = exprod[n];  Coefficient[t, x^i])

cex[5, 3]t

ncex[n_, i_] := 
      Module[{u}, (u = exprod[n];  Coefficient[u, x^i])]

ncex[5, 3]

u

\(\ Do\ loops
	\)\(Do[expr, {i, imax}] ";

" Making\ lists \*"";

Table[Sin[n],{n,0,5}]"\)

"collumn and row vectors"

\(\(	
vec = {1, 1, 0, 3, 5}\)\)

vec vec

\("dot product"; 
	
vec.vec\)

\("square matrices are lists of lists"; 


    matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}\)

Eigenvalues[%]

Part II  Graphs

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

graph = %

Show[graph]

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

Plot[Evaluate[y[x]  /.  %], {x, 0, 4}]