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}]


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]


This finds a fit of the form .

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


This gives a table of , pairs.

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


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

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


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]


This does a nonlinear fit.

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


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}


This takes the Fourier transform of the pulse.

In[9]:= Fourier[data]


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.