Numerical Functions

Overview and applications

Techniques_5.gif Techniques_6.gif Techniques_7.gif
Techniques_8.gif Techniques_9.gif Techniques_10.gif

Numerical Functions

Overview and applications

Integration

Differential equations

Equation solving

Optimization

Linear algebra and sparse arrays

Numerical Integration: Quadrature

Techniques_11.png does numerical integration of functions (“numerical quadrature”):

Techniques_12.png

Techniques_13.png

Numerical Integration: List Integration

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

Techniques_14.gif

Techniques_15.png

Techniques_16.png

Then use Techniques_17.png:

Techniques_18.png

Numerical Integration: Path Integration

For path (contour) integration, parameterize the path.

Techniques_19.png

Techniques_20.png

Graphics:sin(z)/(z - 2)^3

Path integral:

Techniques_22.png

Techniques_23.png

Numerical Integration: Vector Calculus

Surface integrals, volume integrals, and vector calculus.

Parametric surface:

Techniques_24.png

Techniques_25.png

Surface area integral:

Techniques_26.png

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

Techniques_27.png

Differential Equations

Techniques_28.png solves ordinary and partial differential equations.

Techniques_29.png

The solution is a list of rules:

Techniques_30.png

Techniques_31.png

Differential Equations: Special Kinds of ODEs

Techniques_32.png handles delay and differential-algebraic equations.

Delay differential equation:

Techniques_33.png

Techniques_34.png

Differential Equations: PDEs

Partial differential equation (Schrödinger equation):

Techniques_35.png

Techniques_36.png

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

Techniques_37.png

Differential Equations: Indefinite Integral

Use NDSolve to get a numerical indefinite integral.

Techniques_38.png

Indefinite integral as a differential equation:

Techniques_39.png

Plot the integral and integrand:

Techniques_40.png

Equation Solving

Techniques_41.png searches for a numerical solution of one or more equations.

Techniques_42.gif

Techniques_43.png

Equation Solving

You can use Techniques_44.png to set up an inverse function.

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

Techniques_45.png

Techniques_46.png

Use it like any other function:

Techniques_47.png

Optimization

Local and global optimization with constraints.

FindMinimum looks for a nearby local minimum:

Techniques_48.png

Techniques_49.gif

NMinimize works harder to find the global minimum:

Techniques_50.png

Techniques_51.gif

Optimization

Multidimensional optimization with constraints:

Techniques_52.png

Techniques_53.gif

Linear Algebra: Linear Solving

Techniques_54.png is the workhorse in many numerical algorithms.

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

Techniques_55.png

Machine precision arrays:

Techniques_56.png

Linear Algebra: Operations

All standard linear algebra operations, such as:

Techniques_57.png

Techniques_58.png

Linear Algebra: Sparse Arrays

Fast, efficient representation of sparse linear equations.

Techniques_59.png

Techniques_60.png

Linear Algebra: Sparse Arrays

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

Techniques_61.gif

Techniques_62.gif

Linear equations as a sparse array:

Techniques_63.png

Techniques_64.png

Techniques_65.png

Overall structure of the linear equations:

Techniques_66.png

Heat source:

Techniques_67.png

Techniques_68.png

Solve discretized heat equations:

Techniques_69.png

Techniques_70.png

Numerical Methods

Detailed algorithm control

Techniques_71.gif Techniques_72.gif
Techniques_73.gif Techniques_74.gif

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:

Techniques_75.png

Graphics:x^2 + y^2<1

Specifying Methods

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

Techniques_77.png

Techniques_78.png

Techniques_79.png

Iterative method:

Techniques_80.png

Direct method:

Techniques_81.png

Specifying Methods

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

Techniques_82.png

Techniques_83.png

Advanced Method Options: Submethods and Options

Specify submethods and options. See Advanced Documentation.

Submethods: Overall subdivision strategy and specific integration rule:

Techniques_84.png

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

Techniques_85.png

Techniques_86.gif

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):

Techniques_87.png

Techniques_88.png

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

Techniques_89.png

Techniques_90.png

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

Techniques_92.png

Techniques_93.png

Do the same thing inside a Techniques_94.png:

Techniques_95.png

How to Watch Algorithms: EvaluationMonitor

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

Techniques_97.png

How to Watch Algorithms: EvaluationMonitor

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

Techniques_99.png

Techniques_100.png and Techniques_101.png to get a list of evaluation points:

Techniques_102.png

Another way to visualize the same points:

Techniques_103.png

How to Watch Algorithms: StepMonitor

Techniques_104.png lets you monitor the progress after each algorithm step.

Techniques_105.png

How to Watch Algorithms: StepMonitor

You can access the solution computed so far inside Techniques_106.png:

Techniques_107.png

Techniques_108.png

Combine with Techniques_109.png for a progress indicator:

Techniques_110.png

Numbers

Controlling precision & accuracy

Techniques_111.gif

Numbers

Controlling precision & accuracy

Exact and approximate numbers

Precision & accuracy goals in numerics functions

Exact and Approximate Numbers: Three Levels of Precision

Techniques_112.gif

Exact and Approximate Numbers: Three Levels of Precision

Built-in operations respect the precision of their inputs.

Exact input → exact output

Techniques_113.png

Techniques_114.png

Arbitrary precision input → arbitrary precision output

Techniques_115.png

Techniques_116.png

Machine precision input → machine precision output

Techniques_117.png

Techniques_118.png

Exact and Approximate Numbers: Combining Precisions

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

Techniques_119.png

Techniques_120.png

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

Techniques_121.png

Techniques_122.png

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:

Techniques_123.png

Techniques_124.png

10 digits of precision, high accuracy:

Techniques_125.png

Techniques_126.png

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:

Techniques_127.png

Precision & Accuracy Goals: Relative Error

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

Techniques_128.png

Precision & Accuracy Goals in Numerics Functions: Zeroes

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

Techniques_129.png

Techniques

Programming and performance

Techniques_130.gif Techniques_131.gif

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:

Techniques_132.png

Plot it for different values of a:

Techniques_133.png

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

Techniques_134.png

Roughly plot the arc length as a function of a:

Techniques_135.png

Find the solution with minimum arc length near a0.8:

Techniques_136.png

Using Numerics Functions Together: Interpolation

Create fast, pre-computed functions.

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

Techniques_137.png

Techniques_138.png

Techniques_139.gif

ρ(r) is relatively expensive function:

Techniques_140.png

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

Techniques_141.png

Techniques_142.png

The interpolation is fast enough to use in other functions:

Techniques_143.png

Techniques_144.png

Fast Built-In Functions

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

Techniques_145.png

Manual loop—slow:

Techniques_146.png

Built-in functional operation—fast:

Techniques_147.png

Listable built-in operation—fastest:

Techniques_148.png

Numbers and Arrays: Use Approximate Numbers Early

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

Techniques_149.png

Techniques_150.png

Get the corresponding machine precision number:

Techniques_151.png

Techniques_152.png

You may need the exact expression:

Techniques_153.png

Techniques_154.png

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

Techniques_155.png

Techniques_156.png

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:

Techniques_157.png

Techniques_158.png

Packed arrays use less memory:

Techniques_159.png

Techniques_160.png

Packed arrays are faster:

Techniques_161.png

Techniques_162.png

Numbers and Arrays: Packed Arrays

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

Techniques_163.png

Issue extra messages about packed arrays:

Techniques_164.png

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

Techniques_165.png

Techniques_166.png

With a machine precision zero Mathematica can repack the array:

Techniques_167.png

Techniques_168.png

Switch extra messages back off:

Techniques_169.png

Compilation

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

Techniques_170.png

Techniques_171.png produced virtual machine instructions and a native code dynamic library:

Techniques_172.png

The compiled function is fast enough to visualize immediately:

Techniques_173.png

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

Techniques_174.png

Compilation: Automation

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

Techniques_175.png

Techniques_176.png

Techniques_177.png

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

Techniques_178.png

Techniques_179.png

Techniques_180.png

It can be faster to compile it yourself:

Techniques_181.png

Techniques_182.png

Techniques_183.png

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

Techniques_184.png

C Compiler

Needs[“CCodeGenerator`”]

Special Cells

Techniques_185.png

Techniques_186.png

Techniques_187.png

Techniques_188.png

Utility: CreateSpecialCell

Techniques_189.png

Techniques_190.png

NIntegrate Interpolation

Techniques_191.png

NIntegrate path integral

Techniques_192.png

FindRoot plot

Techniques_193.png

NDSolve numerical indefinite integral

Techniques_194.png

Techniques_195.png

1D Optimization plots

Techniques_196.png

Techniques_197.png

NMinimize constrained multidimensional

Techniques_198.png

MonteCarlo sampling points

Techniques_199.png

Sparse arrays 2D heat equation

Techniques_200.gif

Techniques_201.png

NMinimize “RandomSearch” with large “SearchPoints”

Techniques_202.png

Techniques_203.png

Spherically symmetric point cloud

Techniques_204.png

Utility: CreatePictureRowCell

Techniques_205.png

Numerics functions

Techniques_206.gif

Numerical methods

Techniques_207.gif

Techniques

Techniques_208.gif

Lastly

Techniques_209.png