Numerical Functions

Overview and applications

Numerical Functions

Overview and applications

Integration

Differential equations

Equation solving

Optimization

Linear algebra and sparse arrays

Numerical Integration: Quadrature

does numerical integration of functions (“numerical quadrature”):

Numerical Integration: List Integration

For numerical integration of a list of points, interpolate the points.

Then use :

Numerical Integration: Path Integration

For path (contour) integration, parameterize the path.

Path integral:

Numerical Integration: Vector Calculus

Surface integrals, volume integrals, and vector calculus.

Parametric surface:

Surface area integral:

Compute enclosed volume using the divergence theorem (Gauss’ theorem):

Differential Equations

solves ordinary and partial differential equations.

The solution is a list of rules:

Differential Equations: Special Kinds of ODEs

handles delay and differential-algebraic equations.

Delay differential equation:

Differential Equations: PDEs

Partial differential equation (Schrödinger equation):

It’s a particle rolling back and forth in a periodic well:

Differential Equations: Indefinite Integral

Use NDSolve to get a numerical indefinite integral.

Indefinite integral as a differential equation:

Plot the integral and integrand:

Equation Solving

searches for a numerical solution of one or more equations.

Equation Solving

You can use to set up an inverse function.

Given *y**(**x**)*, find the inverse function inv*(**t**)*:

Use it like any other function:

Optimization

Local and global optimization with constraints.

FindMinimum looks for a nearby local minimum:

NMinimize works harder to find the global minimum:

Optimization

Multidimensional optimization with constraints:

Linear Algebra: Linear Solving

is the workhorse in many numerical algorithms.

Arrays of exact numbers (new faster methods in Mathematica 8):

Machine precision arrays:

Linear Algebra: Operations

All standard linear algebra operations, such as:

Linear Algebra: Sparse Arrays

Fast, efficient representation of sparse linear equations.

Linear Algebra: Sparse Arrays

Example: Discretization of 2D heat equation leads to linear equations:

Linear equations as a sparse array:

Overall structure of the linear equations:

Heat source:

Solve discretized heat equations:

Numerical Methods

Detailed algorithm control

Numerical Methods

Detailed algorithm control

Specifying methods

Using advanced method options

How to watch algorithms

Specifying Methods

For when you want to test or compare methods, or you already know the best method to use.

Explore adaptive Monte-Carlo integration:

Specifying Methods

Compare iterative and direct methods for a large sparse system of equations:

Iterative method:

Direct method:

Specifying Methods

Use a method that switches between algorithms for stiff and non-stiff systems:

Advanced Method Options: Submethods and Options

Specify submethods and options. See Advanced Documentation.

Submethods: Overall subdivision strategy and specific integration rule:

Options: Search for a minimum starting from a large number of points:

Advanced Method Options: Special Methods

Some methods can be used to do additional work or get results in a different form.

Example: Landing point of a projectile launched over terrain with height *h**(**x**)*:

Projectile under drag, launched with speed s at angle θ:

The special “EventLocator” method of can detect the collision with the ground:

Do the same thing inside a :

How to Watch Algorithms: EvaluationMonitor

is a quick and easy way to find out what an algorithm is doing:

How to Watch Algorithms: EvaluationMonitor

Watch converge on the more rapidly varying region of this integrand:

and to get a list of evaluation points:

Another way to visualize the same points:

How to Watch Algorithms: StepMonitor

lets you monitor the progress after each algorithm step.

How to Watch Algorithms: StepMonitor

You can access the solution computed so far inside :

Combine with for a progress indicator:

Numbers

Controlling precision & accuracy

Numbers

Controlling precision & accuracy

Exact and approximate numbers

Precision & accuracy goals in numerics functions

Exact and Approximate Numbers: Three Levels of Precision

Exact and Approximate Numbers: Three Levels of Precision

Built-in operations respect the precision of their inputs.

Exact input → exact output

Arbitrary precision input → arbitrary precision output

Machine precision input → machine precision output

Exact and Approximate Numbers: Combining Precisions

When different precisions are combined, a justifiable output precision is determined:

If any part is MachinePrecision, the result will be MachinePrecision too:

Exact and Approximate Numbers: Precision & Accuracy

Precision is the number of significant figures. Accuracy is the number of decimal places.

10 digits of precision, low accuracy:

10 digits of precision, high accuracy:

Precision & Accuracy Goals in Numerics Functions

Functions try to get enough correct digits (PrecisionGoal), or enough correct digits after the decimal point (AccuracyGoal).

Get either 7 correct significant figures or 2 correct decimal places:

Precision & Accuracy Goals: Relative Error

When you require a certain number of significant figures, use only PrecisionGoal.

Precision & Accuracy Goals in Numerics Functions: Zeroes

When the correct answer is zero, a PrecisionGoal can never be satisfied. Include an AccuracyGoal.

Techniques

Programming and performance

Techniques

Programming and performance

Using numerics functions together

Fast built-in functions

Numbers and arrays

Compilation

Using Numerics Functions Together

Often you want to use the result of a numerical function as the input to another function.

Numerically solve a differential equation that depends on a parameter *a*:

Plot it for different values of *a*:

Compute the arc length of the solution as a function of *a*, using numerical integration:

Roughly plot the arc length as a function of *a*:

Find the solution with minimum arc length near *a***0.8:

Using Numerics Functions Together: Interpolation

Create fast, pre-computed functions.

Density *ρ**(**r**)* of a spherically symmetric object recovered from surface density *s**(**r**)*:

*ρ**(**r**)* is relatively expensive function:

Pre-interpolate *ρ**(**r**)* over a range of values:

The interpolation is fast enough to use in other functions:

Fast Built-In Functions

Often the shortest way to code something is also the fastest.

Manual loop—slow:

Built-in functional operation—fast:

Listable built-in operation—fastest:

Numbers and Arrays: Use Approximate Numbers Early

Exact inputs give exact results, approximate inputs give approximate results:

Get the corresponding machine precision number:

You may need the exact expression:

If you only need the final approximate number, use approximate numbers from the start:

Numbers and Arrays: Packed Arrays

Lists and arrays of machine precision numbers (real, complex, integer) may be stored internally as “packed arrays”.

Packed arrays are created automatically by Mathematica:

Packed arrays use less memory:

Packed arrays are faster:

Numbers and Arrays: Packed Arrays

To optimize an algorithm that works with lists of machine numbers, keep everything packed.

Issue extra messages about packed arrays:

A quantity such as an exact 0 causes this machine precision array to be unpacked:

With a machine precision zero Mathematica can repack the array:

Switch extra messages back off:

Compilation

Create a fast version of a function that only works on machine numbers.

produced virtual machine instructions and a native code dynamic library:

The compiled function is fast enough to visualize immediately:

Zoom in (interactive version: see the CompilationTarget examples):

Compilation: Automation

You rarely need to compile because Mathematica does it automatically in many places.

Mathematica can’t detect and compile a “hidden” definition like this:

It can be faster to compile it yourself:

Conclusion

More resources

Functions and methods: Mathematica’s advanced tutorials

Examples: “Applications” in each documentation page; Wolfram Blog

Advanced development & debugging: Wolfram Workbench

Initialization

Color

C Compiler

Needs[“CCodeGenerator`”]

Special Cells

Utility: CreateSpecialCell

NIntegrate Interpolation

NIntegrate path integral

FindRoot plot

NDSolve numerical indefinite integral

1D Optimization plots

NMinimize constrained multidimensional

MonteCarlo sampling points

Sparse arrays 2D heat equation

NMinimize “RandomSearch” with large “SearchPoints”

Spherically symmetric point cloud

Utility: CreatePictureRowCell

Numerics functions

Numerical methods

Techniques

Lastly