3. A Few First Steps

2016-04-10

$Version

3.1	Expressions and Strings

Expressions are symbols or numbers or other mathematical entities,  which are worked on in the kernel by the rules  contained therein.

f = x

h = f^2

Strings are pure text, which is stored as such in most, but not all cases. A string is typed by quotation marks. These no longer appear in common output; they are rendered visible by the command:  FullForm[ ]

g = "f^2"

g

FullForm[g]

3.2	Operators and Symbols

=  definition, assignment   
 
For example:  			
In[1]:= 3
In[2]:= f = 3	

:= delayed assignment (this is explained in a later chapter)

== equal   		used  in equations  and logical operations
!= unequal		used  in logical operations

(* ... *)  		comments  may be inserted  anywhere  in expressions.
			 Longer comments should be included in  Text Cells:  
			 Format -> Style -> Text
				
+  addition

-  subtraction

*  multiplication sign,  may be omitted.		Ex.: 2 x = x + x

   but note:  x2  is the name of the variable  x2, not  2 x  !!!

/  division                           Ex.:  a/b

^  exponentiation                     Ex.:  x^2  = x*x

%  preceeding output (preceeding in time, not in position in the 	  
   notebook).

%% output before preceeding output

%4 output of  Out[4]. Attention !  The numbering gets lost as soon as
   you close the notebook. So it is not reasonable, to use it in a
   notebook you will store and use again later !

;  suppresses printing of output

f = 2x^3

f = π^5;

f

I  = Sqrt[-1] = imaginary unit

Pi = π  =  Ludolf's  number, a symbol!    N[π] = 3.14159 (....)

(  )  round brackets (= parentheses) for grouping terms	
								Ex.: (a + b)(a - b)

[  ]  (square brackets) for argument(s) of functions		
								Ex.: f[x], Sin[x], h[x,y]

{  }  curly brackets (= braces) for lists.   			

								Ex.: {a,b,c}, {x,0,2Pi}

All names (of commands, functions, operators,  options,...) start with 
upper case letters;  this applies even to composed  names:

Ex.: Sin[x], Solve[],  BesselJ[n,x],     FindRoot[], TrigToExp[]

In general, it is recommended to use lower case letters for the user's own expressions and names !

3.3	Some Simple Operations

1/2 + 1/3

%

f = %

f

%11

z = 2^1000

(2/5)^5

2/5^5

0.2^5

N[z]

SetPrecision[z, 20]

N[z, 20]

An operator may also act from behind (postfix operation) by two slashes.

z //N

%

%7 + %8

f = x

f^2 + 2f

f = Pi

1.1 f

N[Pi]

Cos[π/6]

Mathematica knows the symbolic value for some arguments of the trigonometric functions,
but not for all. If Mathematica cannot evaluate an input then it shows the input as output.

Cos[π/8]

Cos[π/12]

N[%,12]

SetPrecision[%%, 25]

Cos[%]

Cos[π/13]

N[%]

Exp[I Pi/4]

N[%]

(1 + I)/Sqrt[2]

N[%]

Clear[a, b]
f = (a + b) (a - b)

g = Expand[f]

Factor[g]

g/(a - b)

Factor[%]

g = (a^3 - b^3)/(a - b)

Simplify[g]

Factor[g]

g = (a^7 - b^7)/(a - b)

Simplify[g]

Factor[g]

h = g /. b -> 3

g  has not been changed by the operation above.

g

x = 3

f = (x + a)^2

Clear[x];  f = (x + a)^2

Expand[f]

Clear[f]   
(*  clears assignments made to f but does not remove f from the Global list. *)

f

Remove[f]  
(* The use of this command is explaned in Chap.6  *)

LegendreP[5,x]

LegendreP[5,Cos[θ]]

3.3.1	Complex Numbers

z = 3 + 4 I

Re[z]

Im[z]

Abs[z]

Conjugate[z]

Abs[z]

Abs[z Exp[I π/4]]

Sin[z]

Abs[%]

N[%]

z = a + b I

Abs[z]

More explanations and applications are treated  in § 4.6.1 .

3.4	Exercises:

3.4.1 	Compute analytically (as far as possible) and numerically the following 	quantities:  
		tg(π/4),  cot(π/3); log(1), log(I), log(I π).

3.4.2  	Divide  (x^9)-(a^9)   by   x-a;  get the quotient as a polynomial.

3.4.3  	Decompose   f=(x^4)+2 (x^3)-13 (x^2)-14 x+24   into factors;
	return to the original polynomial.

3.4.4	Get the values ot the binomial coefficients: 	(({{12}, {n}}))
	Hint: Compute (((x+y))^12.).

3.4.5  	z = 2 + i π. Compute the real and the imaginary part, the conjugate to z, 
	|z|  and the corresponding numerical values.

3.4.6  	Compute the Legendrepolynomial    (Subscript[P, 7])(x), replace x by cosθ and get the 	numerical value of the polynomial for  θ = π/8.

3.5 	Simple Plots

3.5.1	Graphs in 2 Dimensions

Plot[x^3 - x + 1, {x,-2,2}]

More explanations and applications are treated in  §§ 6.1.1 - 6.1.3  .

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

More explanations and applications are treated in  § 6.1.6, options in   § 6.1.11.1  .

3.5.2	Perspectivic Plots of Surfaces in Three Dimensions

Plot3D[Sin[x y],{x,-2,2},{y,-2,2}]

ParametricPlot3D[{2 Cos[t] Cos[p],3 Cos[t] Sin[p],4 Sin[t]},{t,0,π},{p,0,2 π}]

Show[%, ViewPoint ->  {1,.5, - 3}]

More explanations and applications are treated in  §§ 6.2.3, and options in  6.2.8  .

3.5.3  Exercises

3.5.1    Plot the function   f=(x^2)-5 x+1   with  the  range of  x  chosen  such 
	that  both zeros of   f   are shown.
3.5.2    Plot the curve     x = cos(t),  y = cos(2t + a)    for  a = 0,   π/3,  π/2 .
3.5.3    Plot the hyperbolic paraboloid  z=((((x/2)))^2)-((((y/3)))^2) .
3.5.4    Plot the torus   r = 2 + cos(t),  x =  r cos(p),  y = r sin(p), z = sin(t).

3.6 	Differentiation

Clear[a, x, y]

D[ Sin[a x], x]

D[ Sin[ax], x]

D[ Sin[a x], {x,3}]

D[ Sin[a x] Cosh[b y], {x,3}, {y,5}]

More explanations and applications are treated in  § 10.1 .

3.6.1	Exercises

3.6.1   Derive the function   f=(x^2)-5 x+1   w.r.t.  x .
3.6.2   Derive the functions {(e^(2 a (x^2))) cos(c x),tan((e^(3 x))),sin(a x) cos(b y)} w.r.t.  x .
3.6.3   Derive  ((((x/2)))^2) ((((y/3)))^4)   w.r.t.  x   and  y .

3.7  	Integration

3.7.1	Indefinite integration:

Clear[a]

Integrate[ Exp[a x], x]

Integrate[ Exp[-a x] Cos[b y], x, y]

3.7.2	Definite integral:

3.7.2.1	Analytic definite integration

Integrate[ Sin[a x], {x,0,π/2}]

More explanations and applications are treated in  § 10.4.1  .

3.7.2.2	Numeric integration

NIntegrate[Sin[3x], {x,0,0.45}]

More explanations and applications are treated in  § 10.4.2  .

3.7.3	Exercises

3.7.1  	Compute the following definite integral in several ways and 
	compare the results: (∫_0^1) x20 ex  dx  
	a) Numeric integration.
	b) Analytic integration, go over to numeric values with default 
	     precision  and with 20 decimal places.
	c) Series expansion (s. 3.8, 10 terms) of the integrand and tern by term 	    integration

3.8	  Series Expansions

Series[Sin[x], {x,0,5}]

The bracket {}  contains three arguments: 
	1. the expansion variable (here x),
     	2. the expansion point (Subscript[x, 0]) (here 0),
     	3. The highest power to be included (here 5).
 The remainder term   O[]    denotes the output as part of a series, not a polynomial.

Series[Cot[x], {x,0,5}]

Series[ Sqrt[1/(1 + a x + b x^2)], {x,0,3}] //Simplify

Series[Log[x], {x,1,5}]

f = Series[Cos[x], {x,0,5}]

g = Series[Sin[x], {x,0,5}]

f g

f/g

Normal[f/g]

Normal removes the remainder term. Only then one can work with the series output.

Integrate[%,x]

More explanations and applications are treated in  § 10.2  .

3.8.1	Exercises:

3.8.1	Expand  sin(sin x)   around   x = 0  up to the  7-th term.
3.8.2	Expand   tg(x)  around  x = π/2   up to the third term.

3.9  Matrix Calculations

ma = {  
{12,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
{12, 10, -2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  2},
{ 0, -4,  8, -1,  0,  0,  0, -3,  1,  1,  0,  0,  0,  0},
{ 0,  2,  0, 12,  1,  0,  0,  0, -1, -1,  0,  0,  0,  0}, 
{12,  0,  0,  2,  8, -1, -1,  0, -1, -1,  0,  0,  0,  0},     
{ 0, -2,  1,  0, -2,  6,  1,  0,  0,  2, -1,  0,  0,  0},
{ 0, -2,  1,  0, -2,  1,  6,  0,  2,  0, -1,  0,  0,  0}, 
{ 0,  0, -2,  0,  1,  0,  0,  6,  0,  0,  0,  0,  0,  0},
{ 0, -1,  1, -2, -1,  0,  1,  0,  8,  0,  0,  0,  2, -2},
{ 0, -1,  1, -2, -1,  1,  0,  0,  0,  8,  0,  0,  2, -2},     
{ 0,  2,  0,  2,  0, -1, -1,  0,  0,  0,  6, -2, -4,  0},
{ 0,  0,  0,  0, -2,  0,  0,  0,  0,  0, -2,  4,  0, -2},
{-6,  0,  0,  0,  0,  0,  0,  0,  1,  1, -1,  0,  8,  0}, 
{ 0,  2,  0,  0,  0,  0,  0,  0, -1, -1,  0,  0,  0,  8}};

The notebook  lamat.nb  containing this  matrix can be found on the Mathematica  subwebsite in Mathematica course notebooks.

Det[ma] //Timing

im = Inverse[ma]  //Timing ;

im[[1]]

im[[2,1]]

Eigenvalues[N[ma]] //Timing

3.9.1	Characteristic Polynomial

(*	B = MA  - x I,  f = Det( MA  - x I )        *)

b = ma - x IdentityMatrix[14];
f = Det[b] //Timing

Factor[f[[2]]]

More explanations and applications are treated in  Chap.8  .

3.9.2	Exercise:

3.9.1	Compute the determinant, the inverse, the characteristic  values  and the 
	characteristic  equation of the following matrix:     
	{{1, 0, -4}, {0, 5, 4}, {-4,  4, 3}}. 
	Factorize the characteristic  polynomial.

3.10 Solving Linear Equations

3.10.1	Regular systems

sys = {5 x + 1 y == 3, 4 x - 3 y == 2}

Solve[ sys, {x,y}]

sol = % // Flatten

sys /. sol

Solve[ {5 x + 1 y == 3., 4 x - 3 y == 2}, {x,y}]

ma = {{5,1}, {4, -3}} ; MatrixForm[ma]
b  = {3,2}
mx = {x,y}
ma . mx  == b

ma . mx  == b //Thread

Inverse[ma] . b

3.10.2	Singular systems

Solve[ {x - y == 1, 3 x - 3 y == 3}, {x,y} ]

Solve::"svars" :  "Equations may not give solutions for all "solve" variables " >>

Solve[ {x - y == 1, 3 x - 3 y == 2}, {x,y} ]

More explanations and applications are treated in  Chap.9  .

3.10.3	Exercises

3.10.1	Solve the following system of linear equations:
	x + y + z = 1,   x + 2y + 3z = 4,   x + 3y + 6z = 10.

3.10.2	Solve the following system of linear equations in 2 ways:
	a x + b y   =  e,   c x  +  d y  =  f.

3.11 	Solving Non-Linear Algebraic Equations

Clear[p2, x, a, b, c]
p2 = a x^2 + b x + c

sol =  Solve[ p2 == 0 , x]

p2 /. sol

% //Simplify

Solve[ x^4 - 1 == 0]

Solve[ x^4 + 1 == 0]

Solve[ x^4 + 1 == 0. ]

p3 = x^3 + x + 1

sn = NSolve[p3 == 0]

p3 /. sn

Chop[%]

s3 = Solve[p3 == 0]

Simplify[s3]

N[%]

p3 /. s3

Together[%]

p5 = x^5 - x + 1

s5 = Solve[p5 == 0]

The expression ending with the empercent sign is a “pure function” .
Hashmarks (#,  #1, ...)  represent the variables.

ns5 = N[%]

p5 /. ns5

More explanations and applications are treated in  Chap.9  .

Exercises

3.11.1	Solve the following algebraic equation (analytically and numerically):
	(x^5) +  x  -  1   =  0. Verify by inserting that the solutions fulfil the equation.

3.11.2	Solve the following algebraic equation (analytically and numerically):
	(x^8) -  13  (x^4)  + 7   =  0. Verify by inserting that the solutions fulfil the 	equation.

3.12 	Finding Roots of Algebraic and Transcendental  Equations

Clear[a,x,y,z]

p3 = x^3 + x + 1

s1 = FindRoot[p3, {x,1}]

s2 = FindRoot[p3, {x,I}]

s3 = FindRoot[p3, {x,-I}]

so = {s1, s2, s3}

p3 /. so

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

Message in Message Window:

Solve::ifun: 
Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information.>>

?Reduce

Reduce[Cos[x] == 0,x]

soq =Solve[Sin[x] + Cos[x] == 0,x]

Message in Message Window as above.

FindRoot[hh[x_] = Sin[x] + Cos[x] ,{x,2.1} ]

N[ -(π/4)+2 π Range[-1,1]]

N[ -(π/4)+2 π Range[-1,1,1/2]]

hh[%]

Plot[hh[x],{x,-1,2.5}]

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

ContourPlot[{x^3 - y^2 == 0, x + y == 2},{x,-2,2},{y,0,2}]

FindRoot[{x^3 - y^2, x + y  -2},{x,1},{y,2}]

FindRoot[{x^3 - y^2, x + y  -2},{x,I},{y,2I}]

f = z^2 + Conjugate[z]

FindRoot[f,{z,1}]

FindRoot[f,{z,1, I}]

One may give also two starting vales. In some cases FindRoot[] even asks for this.

More explanations and applications are treated in  Chap.9  .

Exercises

3.12.1  Find the numeric values of two other roots of the function: 
	f = sin(x)-cos(x).

3.12.2  Find all the numeric values x  in the interval  (-3, 3) for which the function
	f = (tg x  -  x)/((x/3)+cos x)  becomes either 0  oder infinite.

3.13   Solution of Differential  Equations

3.13.1  Analytic Solution : General Solution

'    (apostrophe)  denotes the derivative w.r.t. the independent variable

Clear[k,x,y]

DSolve[ y''[x] + k y[x] == 0, y[x], x]

DSolve[ y''[x] + 2 y'[x]/x - l (l + 1)/x^2 y[x] + 
		k^2 y[x] == 0, y[x], x]

3.13.2  Analytic Solution :  Initial Value Problem

Clear[t,x,y,g];
DSolve[ {x''[t] == 0, y''[t] + g == 0, x[0] == 0, 
x'[0] == v0, y[0] == 5, y'[0] == 0}, {x[t], y[t]}, t]

More explanations and applications are treated in  §§ 11.1 - 11.4  .

3.13.3  Numerical Solution

Motion of mass point in gravity field with and without Newtonian friction.

Clear[r,v,b,t,x,y]

Position

r[t_] = {x[t], y[t]}

Velocity

v[t_] = D[r[t],t]

Acceleration

b[t_] = D[v[t],t]

m  b  =  F  =   m g

m = 1; g = 10; a = .3; 
sys = m b[t] == {0, -m g}

sys = sys //Thread

m b  =  F  =  m g -  a v |v|

sysa = m b[t] == 
{0, -m g} - a v[t] Sqrt[x'[t]^2 + y'[t]^2] //Thread

anf = {x[0] == 0, y[0] == 0, x'[0] == 2, y'[0] ==10}

sol = NDSolve[ Join[sys,anf], {x,y}, 
{t,0,4}] //Flatten

sola = NDSolve[ Join[sysa,anf], {x,y}, 
{t,0,2}] //Flatten

p=ParametricPlot[{x[t],y[t]}/.sol,{t,0,2}];
pa=ParametricPlot[{x[t],y[t]}/.sola,{t,0,1.35`},PlotStyle->Dashing[{0.01`}]];

Show[p,pa]

More explanations and applications are treated in  § 11.2  .

Exercise

3.13.1 Solve the 2-dimensional equation of motion for the motion of a projectile 
	experiencing the field of gravity and a linear friction force.

3.14  Vector Algebra and Analysis in Orthogonal Curvilinear Coordinates

In Mathematica versions up to version 8  the vector analysis functionality is contained in an own package named  “VectorAnalysis`”, which must be loaded as shown in the next Mathematica command  just below. Since version 9 onwards the main commands are contained in the kernel. These new commands are more involved than those of the earliner versions. However, the commands of the previous versions can still be used if the package  “VectorAnalysis`” is loaded.
This loading overrides the new functionality and provides almost complete compatibility for programs written in the old verions. Here I shall describe the old version, so I use the package 
“VectorAnalysis`”.
I shall deal with the new version in Chap.14 .

Needs["VectorAnalysis`"]

3.14.1	Vector algebra

Cartesian coordinates   x, y, z

There are three vectors (Overscript[a, →]), (Overscript[b, →]), (Overscript[c, →]) :

Clear[va,vb,vc]

va = {ax, ay, az} ; 
 vb = {bx, by, bz};    
vc  = {cx, cy, cz} ;

Scalar product:

α = va.vb

Vector product:

vcc = Cross[va,vb]

Scalar triple product  (Ge.:  Spatprodukt)

β = vc.Cross[va,vb]

Spherical coordinates    r, θ, ϕ

There are three vectors (Overscript[a, →]), = (Overscript[e, →])r ar + (Overscript[e, →])θ aθ + (Overscript[e, →])ϕ aϕ ;   (Overscript[b, →]), (Overscript[c, →])  defined analogously.

Clear[va,vb,vc]

va = {ar, aθ, aϕ} ; 
 vb = {br, bθ, bϕ} ;    
vc  = {cr, cθ, cϕ} ;

Scalar product:

α = va.vb

Vector product:

vcc = Cross[va,vb]

Scalar triple product  (Ge.:  Spatprodukt)

β = vc.Cross[va,vb]

In all orthogonal curvilinear physical coordinate systems, vectors are set up as linear combinations of the base vectors;  in Mathematica they are written as lists comprising 
the three components.

3.14.2  Coordinate transformations

Transformatons between Cartesian coordinates x, y, z and 
spherical coordinates  r, θ, ϕ

Needs["VectorAnalysis`"]

SetCoordinates[Spherical[r,θ,ϕ]]

CoordinateSystem

CoordinatesToCartesian[{r,θ,ϕ}]

CoordinatesToCartesian[{4.5,π/6.,π/4.}]

CoordinatesFromCartesian[{x,y,z}]

CoordinatesFromCartesian[{1.5909902576697317`,1.5909902576697315`,3.897114317029974`}]

{4.5,π/6.,π/4.}

3.14.3	Vector analysis in orthogonal curvilinear coordinates

Cartesian coordinates

Needs["VectorAnalysis`"]

SetCoordinates[Cartesian[x,y,z]]

CoordinateSystem

Grad[ψ[x,y,z],{x,y,z} ]

Div[ {ax[x,y,z], ay[x,y,z], az[x,y,z]},{x,y,z} ]

Curl[ {ax[x,y,z], ay[x,y,z], az[x,y,z]},{x,y,z} ]

Laplacian[ψ[x,y,z],{x,y,z}]

Spherical coordintes

SetCoordinates[Spherical[r,θ,ϕ]]

Grad[ ψ[r,θ,ϕ],{r,θ,ϕ},"Spherical" ]

Div[ {ar[r,θ,ϕ], ath[r,θ,ϕ], aph[r,θ,ϕ]},{r,θ,ϕ},"Spherical" ] //
Expand

Curl[{ar[r,θ,ϕ], ath[r,θ,ϕ], aph[r,θ,ϕ]},{r,θ,ϕ},"Spherical" ] //
Expand

Laplacian[ ψ[r,θ,ϕ],{r,θ,ϕ} ,"Spherical" ] //Expand

More explanations and applications are treated in  Chap.14  .

Exercises

3.14.1  Go over to circular cylindrical coordinates  r, ϕ, z  
	( SetCoordinates[ Cylindrical[r,ϕ,z]] ).
	Seek the transformations between the Cartesian and the cylindrical coordinates
	of the same point   (x,y,z)  ⇔  (r, ϕ, z) .  For the point  x = 2.3, y = 3.7, z = 1.4 
	find the values of the corresponding cylindrcal coordinates. Transform them back
	to the Cartesian components. 
	Compute the gradient and Laplacian of a scalar function  ψ(r,ϕ,z); divergence and 
	curl of a vector field ( ar(r,ϕ,z), ap(r,ϕ,z), az(r,ϕ,z) ). Do not forget to insert the name 	“Cylindrical” into the operators of vector analysis.
	
3.14.2	Verify in rectangular, spherical and cylindrical coordinates that

Laplacian ψ  =  div grad ψ.