Lecture Series : Tools for Physicists / Werkzeuge für Physiker 2023

Organiser: Dr. Peter-Bernd Otte

Short Tour of Mathematica

Dr.Michael Distler, KPH

Mainz, 7 June 2023

Content :

Short Tour (60min)

Fitting with Gnuplot and Mathematica (30 min)

Linear Model Fits (30 min)

B - Splines (30 min)

Interactive Usage

You can use Mathematica just like a calculator: you type in questions, and Mathematica prints back answers.

Ask Mathematica what 3 + 5 is; it prints back 8. Mathematica adds the In and Out labels; you do not type them. You end each line with ⇧|⌤.

3+5

The ^ stands for “to the power of”.

57.1^100

(57.1^100)

Basic Math Assistant and other palettes (Keyboard Entry)
and Assistant Basic Entry Keyboard Math other palettes

Manipulation with simple calculations

Manipulate[Grid[Table[{i,i^m},{i,1,n}],Alignment->Left,Frame->All],{n,1,20,1},{m,1,100,1}]

This asks Mathematica to work out the inverse of a 2 x 2 matrix.

Inverse[{{1,2},{3,4}}]//MatrixForm

Postfix operator //

Mathematica can handle formulas as well as numbers.

This asks Mathematica to integrate a simple function.

Integrate[Sqrt[x]*Sqrt[x + 1], x]Integrate[Sqrt[x] Sqrt[x + 1], x]

This asks Mathematica to solve a quadratic equation. The result is a list of rules for x convenient for use in other calculations.

Solve[x^2+x==a,x]

Built - in Functions

The Wolfram Language has nearly 6,000 built - in functions. All have names in which each word starts with a capital letter :

Range[20]

NestList[f,x,5]

Mathematica can also create two and three-dimensional graphics.

This creates a 2D plot of a simple function.

Plot[Sin[x]+Sin[1.6x],{x,0,40}]

This creates a simple Manipulation of a 2 D plot.

Manipulate[Plot[Sin[a x]+Sin[b x],{x,0,40}],
{{a,1},0,10},
{{b,1.6},0,5}]

Here is a 3D plot.

Plot3D[Sin[x y],{x,0,4},{y,0,4}]

You can access many of the calculator features of Mathematica just by pushing buttons in standard palettes.

Power Computing with Mathematica

Even though you can use it as easily as a calculator, Mathematica gives you access to immense computational power.

Mathematica can handle numbers of any size.

100!

This works out a numerical 100-digit approximation to pi.

N[Pi,100]

The Wolfram Language by default does exact computation whenever it can :

3/7+2/11

Use N to get (potentially faster) numerical results :

N[3/7+2/11]

Mathematica can work with formulas of any length—solving problems that would have taken years by hand.

This asks Mathematica to factor a polynomial.

Factor[x^99+y^99]

Mathematica calls on sophisticated algorithms to simplify formulas.

Simplify[%]

Use the Out operator (%, %%, %%%, ...) with care. 
  It is usually fine to use % (the last result generated) within one cell.
  But using % and %% across cells should be avoided :

M={{1,2},{3,4}};
%//MatrixForm
M=.

Accessing Algorithms in Mathematica

Whenever you use Mathematica you are accessing the world’s largest collection of computational algorithms.

In each case, Mathematica automatically chooses the best algorithm to use.

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

NIntegrate[Log[x+Sin[x]],{x,0,2}]

NSolve[x^5-6x^3+8x+1==0,x]

NMinimize[{Cos[x y]+x,x^2+y^2≤10},{x,y}]

This generates a two-dimensional table corresponding to a matrix.

m=Table[2^i+x^j,{i,3},{j,4}]

This displays the table in matrix form.

MatrixForm[m]

Clear the assignment of a variable as soon as it is not needed any longer.
  Especially the letters that are used in symbolic expression should not be
used as global variables.

m=.

Mathematical Knowledge in Mathematica

Mathematica incorporates the knowledge from the world’s mathematical handbooks—and uses its own revolutionary algorithms to go much further.

Mathematica knows about all the hundreds of special functions in pure and applied mathematics.

LegendreQ[3,x]

Mathematica can evaluate special functions with any parameters to any precision.

N[MathieuC[1+I,2I,3],50]

Mathematica is now able to do vastly more integrals than were ever before possible for either humans or computers.

Integrate[Sqrt[x]ArcTan[x],x]

Here is a definite integral. The results often require special functions.

Integrate[Log[x] Exp[-x^3],{x,0,Infinity}]

Here is a symbolic sum.

Sum[1/(k+1)^6,{k,0,n}]

Mathematica can solve a wide range of ordinary and partial differential equations.

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

This finds the billionth prime number.

Prime[10^9]

Visualization with Mathematica

Mathematica makes it easy to create stunning visual images.

Plots in 2D

Generate a 2 D plot of a polynomial function : (The interval notation of {x, min, max} defines the domain.)

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

Or plot a 2 D region for a set of inequalities : (&& is the symbol for And.)

RegionPlot[Reduce[{x^2+y<2&&x+y<1}],{x,-3,3},{y,-3,3}]

There are lots of useful options to customize visualizations, like adding legends :

Plot[{x^3,x^2,x},{x,-2,2},PlotLegends->"Expressions"]

Or filling a plot to visualize the area under a curve :

Plot[1/x,{x,-3,3},Filling->Axis]

Combine different plot types with Show :

Show[{Plot[x^2+2,{x,-3,3}],RegionPlot[2 x>y-3,{x,-3,3},{y,0,9}]}]

More Plots in 2 D

Use ParametricPlot to plot a set of parametric equations :

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

Make a ContourPlot of a function in multiple variables :

ContourPlot[Sqrt[x^2+y^2],{x,-1,1},{y,-1,1}]

Use DensityPlot for a continuous version :

DensityPlot[Sqrt[x^2+y^2],{x,-1,1},{y,-1,1}]

Plots in 3 D

Plot3D will plot a 3 D Cartesian curve or surface :

Plot3D[x^2-y^2,{x,-3,3},{y,-3,3}]

Use ParametricPlot3D to plot a 3 D space curve :

ParametricPlot3D[{Sin[u],Cos[u],u/10},{u,0,20}]

For plotting in spherical coordinates, use SphericalPlot3D :

SphericalPlot3D[Sin[θ],{θ,0,Pi},{ϕ,0,2 Pi}]

RevolutionPlot3D constructs the surface formed by revolving an expression around an axis :

RevolutionPlot3D[x^4-x^2,{x,-1,1}]

This creates a 3D parametric plot with automatic choices for most options.

ParametricPlot3D[{u Cos[u] (4+Cos[v+u]),u Sin[u] (4+Cos[v+u]),u Sin[v+u]},{u,0,4 Pi},{v,0,2 Pi},PlotPoints->{60,12}]

This shows how Manipulate can be used to visualize a 3D plot

Manipulate[
ParametricPlot3D[{u Cos[u] (4+Cos[v+u]),u Sin[u] (4+Cos[v+u]),u Sin[v+u]},{u,0,umax},{v,0,vmax},PlotPoints->{60,12}],
{{umax,4π},0,10π},
{{vmax,2π},0,4π}]

Mathematical Notation

Mathematica notebooks fully support standard mathematical notation—for both output and input.

Mathematica combines the compactness of mathematical notation with the precision of a computer language.

Here is an integral entered using only ordinary keyboard characters.

Integrate[Log[1+x]/Sqrt[x],x]

Here is the same integral entered in 2D form with special characters. You can enter this form using a palette or directly from the keyboard.

∫((Log[1+ξ])/(Sqrt[ξ]))\[DifferentialD]ξ

This shows the keys you need to type to get the input above. The symbol  stands for the Esc key.

∫((Log[1+ξ])/(Sqrt[ξ]))\[DifferentialD]ξ

Mathematica’s StandardForm is precise and unambiguous. TraditionalForm requires heuristics for interpretation.

Mathematica can generate output in traditional textbook form.

TraditionalForm[%]

Mathematica produces top-quality output for formulas of any size or complexity.

(∑_(μ=0)^∞)(((φ^μ)Exp[((π μ)/4)])/(((μ!)^2)((μ^2)+κ)((μ^2)-λ)))//TraditionalForm

Formulas can be converted to TEX input:

%//TeXForm

Lists and Functional Programming

Functional Programming is an important programming paradigm since it is almost trivial to parallelize a program written in functional form.

Since in Mathematica everything is a list, so functional programming comes very naturally.

{3,4,5,7/8,x,y,x^2+3y^3,{a,b,c},}

Parts of lists are indexed starting at 1, and can be extracted using[[ ...]]

{a,b,c,d}[[3]]

Negative indices count from the end :

{a,b,c,d,e,f}[[-3]]

Many operations immediately "thread" over lists :

{1,2,3}+2

{a,b,c}+{x,y,z}

Refer to "spans" in lists using ;;

{a,b,c,d,e,f}[[2;;4]]

Iterators

Make a table of the first 10 squares :

Table[x^2,{x,10}]

Many functions in the Wolfram Language use the standard "iterator specification" : min, max, step :

Table[f[x],{x,4,20,2}]

An alternative iterator specification just gives an explicit list of values :

Table[f[x],{x,{5,10,20,10,5}}]

This makes a nested table :

Table[i/j,{i,4},{j,2}]//MatrixForm

Assignments

Values can be assigned using =

x=7

This is "immediate" assignment.

An alternative is delayed assignment, where the value is recomputed every
time it is needed :

t:=Now

t

t

Clear the assignments :

x=.
t=.

Use Module to localize variables :

Module[{a=1},a+8]

a

Function Definitions

In the Wolfram Language, function definitions are just assignments that give transformation rules for patterns.

Define a function of two arguments named x and y :

f[x_,y_]:=x+y

Use the definition :

f[4,a]

Clear the definition :

Clear[f]

Pure Functions

The Wolfram Language allows what it calls pure functions, indicated by ending with &
Their first argument is indicated by #

(These are also known as anonymous functions, lambda expressions, etc.)

Make a pure function for adding 1 :

(#+1)&

If a pure function is given as the head of an expression, the function is applied to the arguments :

(#+1)&[50]

Here is a function of several arguments :

{#2,1+#1,#1+#2}&[a,b]

This is an alternative way to specify the function :

Function[{x,y},{y,1+x,x+y}][a,b]

Lots of built - in functions commonly use pure functions :

Select[{1,4,6,8,10,15},#>7&]

Applying Functions

It' s very common to want to "map" a function over multiple expressions :

Map[f,{a,b,c,d}]

/@ ("slash at") is a short notation for Map :

f/@{a,b,c,d}

This uses a pure function :

{#,#}&/@{a,b,c,d}

Apply applies a function to multiple arguments :

Apply[f,{a,b,c,d}]

Expressions have "levels"—corresponding to the number of indices needed to extract a part.Functions like Map can operate at specific levels.

Map defaults to operate at level 1 :

Map[f,{{a,b},{c,d}}]

This operates only at level 2 :

Map[f,{{a,b},{c,d}},{2}]

@@ is equivalent to Apply, operating by default at level 0 :

f@@{{a,b},{c,d}}

@@@ means "apply at level 1" :

f@@@{{a,b},{c,d}}

@means ordinary function application :

f@x