Numerical Operations on Data

Index

This index gives a list of keywords that are associated with various cells in this Notebook. If you Command-Double-click one of the keywords below,  Mathematica  will find the cell associated with that keyword, select it, and display it in the center of  your window.

ConstrainedMax  
ConstrainedMin  
FindMinimum  
Fit  
Fitting`curve 
Fourier  
InterpolatingPolynomial  
Interpolation  
InverseFourier  
LinearProgramming

Curve Fitting

Syntax (see page 672)

Fit

Fit[data ,funcs ,vars ]  fits a list of data points using the functions funcs  of variables  vars .

The data can have the form                                        {{x1,y1,...,f1 },{x2,y2,...,f2 },...}, where the number of cordinates  x ,  y , ...  is equal to the number of variables in the list vars.

The data can also be of the form {f1, f2 ,...}, with a single coordinate assumed to take values 1, 2, ....

InterpolatingPolynomial

InterpolatingPolynomial[data ,vars ]   gives a polynomial in the variable vars  which provides an exact fit to the list of data.

The data can have the forms  {{x1, f1 }, {x2, f2 }, ...} or        {f1,  f2, ...}, where in the second case, the xi  are taken to have the values 1, 2, ....

The fi  in the data can be replaced by {fi, dfi, ddfi, ...}, specifying derivatives at the points xi .

Linear and Quadratic Fit

A table of the first 20 primes is used as data. Note that the data points are assumed to correspond to the results of evaluating a function of one variable when the function successively takes on values of 1, 2, ...

fp = Table[Prime[x], {x, 20}]

Fit[fp, {1, x}, x]

Fit Using the Exponential

ft = Table[ N[ 1 + 2.3 Exp[-x/3]], {x, 10}]

Fit[ ft, {1, Exp[ -x/3]}, x]

Quadratic Fit to Pairs of Values

fs = Table[{x, 1.2 + .3 x - 1.4 x^2}, 
                               {x, 0, 1, .1}]

Fit[fs, {1, x, x^2}, x]

Mutidimensional Fit

Note that you need to use Flatten to get this "data" in the right form for Fit.

fq = Flatten[ Table[ {x, y, 1 + 5x - x y},
          {x, 0, 1, .04}, {y, 0, 1, .04}], 1];

Fit[ fq, {1, x, y, x y}, {x, y}]

Chop[%]

Interpolating Polynomial

Note that the polynomial reproduces the fourth data point exactly.

efit =
  InterpolatingPolynomial[{1, 2, 6, 24, 120}, x]

efit /. x -> 4

Interpolating Polynomial with Derivatives Specified

InterpolatingPolynomial[ {{2, {1, 2}},{3, 
                                 {3, 4}}}, x]

{%, D[%, x]} /. x -> 2

{%%, D[%%, x]} /. x -> 3

Approximation Functions and Interpolation

Syntax (see page 676 )

Interpolation

Interpolation[{f1, f2, ...}] constructs an approximate function with values fi  at sucssessive integers.

Interpolation[{{x1, f1 },{x2, f2 }, ...}] constructs an approximate function with values fi  at points xi.

Interpolation[{{x1,{f1, df1, ddf2, ...}}, ...}] constructs an approximate function with specified derivatives at the points xi.

Interpolation[{{x1, y1, ... f1 }, ... }] specifies function values on a grid of points.

Basic Interpolation

Note that the aproximate function reproduces the values of the original table. The last two lines compare the approximate function to the true sine.

Table[{x, Sin[x]}, {x, 0, 2, .25}]

sin = Interpolation[%]

sin[0.25]

sin[0.3]

Sin[0.3]

Using Approximate Functions

Note that the approximate function sin was defined in the previous example.

NIntegrate[ sin[x]^2, {x, 0, Pi/2}]

NIntegrate[ Sin[x]^2, {x, 0, Pi/2}]

A Note on Approximate Functions

Approximate functions are represented in Mathematica  by InterpolatingFunction objects. The InterpolatingFunction object contains a represention of the approximate function based on interpolation. Typically it contains values and possibly derivatives at a sequence of points. As a result, when you ask for the value of a function with a particular argument, the InterpolatingFunction object can interpolate to find an approximation to the value you want. For more information, see page 676.

Multidimensional Approximate Function

Note that you need to use Flatten to get this "data" in the right form for Interpolation.

fq = Flatten[ Table[ {x, y, Sin[ x y ]},
          {x, 0, 1, .04}, {y, 0, 1, .04}], 1];

sin2 = Interpolation[ fq ]

sin2[0.13, 0.34]

Sin[ 0.13 0.34]

Fourier Transforms

Syntax (see page  679 )

Fourier

Fourier[{a0, a1,...}] gives the Fourier transform.

Fourier[{{a00, a01, ...},{a10, a11, ...}, ...}] gives a two- dimensional Fourier transform.

InverseFourier

InverseFourier[{a0, a1, ...}] gives the inverse Fourier transform.

Fourier Transform and Its Inverse

square = {-1., -1., -1., -1., 1., 1., 1., 1.}

?Fourier

Fourier[square]

InverseFourier[%]

Chop[%]

A Note on Fourier Transforms

There are several conventions that can be used in defining Fourier transforms. In Mathematica  the zero frequency term in the transform appears at position 1. Also, the convention used for defining the transform is the one common in the physical sciences. In electrical engineering, the sign of the exponent is typically reversed, leading to a list of values which is reversed. Fourier works with lists of any length; however, it is significantly more efficient when the length of the list of data is a power of 2.

Convolution with a Kernel

The plots show the smoothing effect of the convolution. Chop is used in the last line to remove small imaginary parts that are generated in the Fourier transform.

data = Table[ N[ BesselJ[ 1, 10 n/256] 
      + 0.2 ( Random[ ] - 1/2)], {n, 256} ];

kernel = 
    Table[ N[ Exp[ -200 (n/256)^2]], {n, 256}];

conv = 
InverseFourier[ Fourier[data]  Fourier[kernel]];

ListPlot[ data];

ListPlot[ Chop[ conv ]];

Numerical Optimization

Index

This index gives a list of keywords that are associated with various cells in this Notebook. If you Command-Double-click one of the keywords below,  Mathematica  will find the cell associated with that keyword, select it, and display it in the center of  your window.

ConstrainedMax  
ConstrainedMin  
FindMinimum  
Fit  
Fitting`curve 
Fourier  
InterpolatingPolynomial  
Interpolation  
InverseFourier  
LinearProgramming

Numerical Minimization

Syntax (see pages 109 and 703)

FindMinimum

FindMinimum[f, {x, x0 }] searches for a local minimum of the function f,  starting from the point x  = x0.

FindMinimum[f, {x, x0, x1 }] searches for a local minimum using x0  and x1  as the first two values of x  (this form must be used if symbolic derivatives of f  cannot be found).

FindMinimum[f, {x, xstart, xmin, xmax }] searches for a local minimum, stopping if x  ever gets ouside the range xmin  to xmax..

FindMinimum[f, {x, x0 }, {y, y0 }, ...] searches for a local minimum in a function of several variables.

Local Minimum

Note that the local minimum that is found may depend on the starting point.

FindMinimum[ x Cos[x], {x, 1}]

FindMinimum[ x Cos[x], {x, 10}]

Getting Values for the Local Minimum

Note that FindMinimum returns a list consisting of the local minimum followed by a transformation rule.

{minval, rule} = FindMinimum[ x Cos[x], {x, 1}]

minval

x /. rule

Local Minimum of a Function without Symbolic Derivative

FindMinimum[ Re[Zeta[ 1/2 + I t]] , {t, 12, 13}]

Local Minimum in a Specified Range

FindMinimum[ Sin[x] + x/5, {x, -2, -2.5, -1.5}]

Local Minimum of a Function with Two Variables

FindMinimum[ Sin[ x y ], {x, 2}, {y, 2} ]

Linear Programming

Syntax (see pages 109 and 707)

ConstrainedMax

ConstrainedMax[f,{inequalities },{x, y, ...}]  finds the global maximum of the linear function f  in the region specified by the inequalities.

ConstrainedMin

ConstrainedMin[f,{inequalities },{x, y, ...}]  finds the global minimum of the linear function f  in the region specified by the inequalities.

LinearProgramming

LinearProgramming[c, m, b ]  finds the vector x   which minimizes c .x   subject to the constraints m .x  >= b  and          x  >= 0.

Constrained Maximum

ConstrainedMax[ x + y, {x < 1, y < 2}, {x, y} ]

Constrained Minimum

Note that Mathematica  assumes that all variables have non-negative values.

ConstrainedMin[ x + y, {x < 1, y < 2}, {x, y} ]

Constrained Maximum with Approximate Numbers

If the coefficients in your input contain approximate numbers, Mathematica will give an approximate numerical result.

ConstrainedMin[ x + 3.2 y + 7 z, 
{x - 2.8 y < 7.3, 2.1 x + 3 z >= 5.1, x + y + z < 10 },
                               {x, y, z}]

Linear Programming Using Matrices

LinearProgramming[{2, -3}, 
       {{-1, -1}, {1, -1}, {1, 0}}, {-10, 2, 1}]

ConstrainedMin[ 2 x - 3 y, 
{x + y <= 10, x - y >= 2, x >= 1}, {x, y}]