This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / A Practical Introduction to Mathematica / Numerical Mathematics /

1.6.6 Manipulating Numerical Data

When you have numerical data, it is often convenient to find a simple formula that approximates it. For example, you can try to "fit" a line or curve through the points in your data.

Fitting curves to linear combinations of functions.

This generates a table of the numerical values of the exponential function. Table will be discussed in Section 1.8.2.

In[1]:= data = Table[ Exp[x/5.] , {x, 7}]

Out[1]=

This finds a least-squares fit to data of the form . The elements of data are assumed to correspond to values , , of .

In[2]:= Fit[data, {1, x, x^2}, x]

Out[2]=

This finds a fit of the form .

In[3]:= Fit[data, {1, x, x^3, x^5}, x]

Out[3]=

This gives a table of , pairs.

In[4]:= data = Table[ {x, Exp[Sin[x]]} , {x, 0., 1., 0.2}]

Out[4]=

This finds a fit to the new data, of the form .

In[5]:= Fit[%, {1, Sin[x], Sin[2x]}, x]

Out[5]=

Fitting data to general forms.

This finds the best parameters for a linear fit.

In[6]:= FindFit[data, a + b x + c x^2, {a, b, c}, x]

Out[6]=

This does a nonlinear fit.

In[7]:= FindFit[data, a + b^(c + d x), {a, b, c, d}, x]

Out[7]=

One common way of picking out "signals" in numerical data is to find the Fourier transform, or frequency spectrum, of the data.

Fourier transforms.

Here is a simple square pulse.

In[8]:= data = {1, 1, 1, 1, -1, -1, -1, -1}

Out[8]=

This takes the Fourier transform of the pulse.

In[9]:= Fourier[data]

Out[9]=

Note that the Fourier function in Mathematica is defined with the sign convention typically used in the physical sciences—opposite to the one often used in electrical engineering. Section 3.8.4 gives more details.