Home

Staff
     
Consulting
               
Mathematica Tools

Customer Quotes
        
Contact

Older Mathematica Items

 

 

 

 

 

Scientific Arts

Linear and Nonlinear Least Squares Fitting to Data



Linear Least Squares Fits: Specific Heats in H20

This data comes from the NIST WebBook:

E.W. Lemmon, M.O. McLinden and D.G. Friend, "Thermophysical Properties of Fluid Systems"  in NIST Chemistry WebBook, NIST Standard Reference Database Number 69, Eds. W.G. Mallard and P.J. Linstrom, November 1998, National Institute of Standards and Technology, Gaithersburg MD, 20899 (http://webbook.nist.gov).

We have placed the information in the file h2odata.dat. First read it into the variable H2OData. (If the file h2odata.dat is not placed in a directory that is contained in Mathematica's $Path variable, you can make it available by executing the function [Graphics:Images/datafitting_gr_2.gif] where path is the path to the needed directory.)

[Graphics:Images/datafitting_gr_3.gif]

The first line in h2odata.dat contains information on the data that is contained in each column (there are 11 columns of data):

[Graphics:Images/datafitting_gr_4.gif]
[Graphics:Images/datafitting_gr_5.gif] [Graphics:Images/datafitting_gr_6.gif]
1 Temperature(K)
2 Pressure(atm)
3 Density(mol/l)
4 Volume(l/mol)
5 InternalEnergy(kJ/mol)
6 Enthalpy(kJ/mol)
7 Entropy(J/mol*K)
8 Cv(J/mol*K)
9 Cp(J/mol*K)
10 SoundSpeed(m/s)
11 Phase

 

Now remove the first (header) line to redefine H2OData.

[Graphics:Images/datafitting_gr_7.gif]

Create a variable [Graphics:Images/datafitting_gr_8.gif] by extracting the specific heat at constant volume versus temperature (this command extracts columns 1 and 8 from H2OData to create a list of [Graphics:Images/datafitting_gr_9.gif] data points).

[Graphics:Images/datafitting_gr_10.gif]
[Graphics:Images/datafitting_gr_11.gif]

Create another variable, [Graphics:Images/datafitting_gr_12.gif], by extracting the specific heat at constant pressure versus temperature.

[Graphics:Images/datafitting_gr_13.gif]
[Graphics:Images/datafitting_gr_14.gif]

Now make plots of both data sets.

[Graphics:Images/datafitting_gr_15.gif]

[Graphics:Images/datafitting_gr_16.gif]

 

[Graphics:Images/datafitting_gr_17.gif]

[Graphics:Images/datafitting_gr_18.gif]

 

Here they are together in one plot.

[Graphics:Images/datafitting_gr_19.gif]

[Graphics:Images/datafitting_gr_20.gif]

Now use Fit to find a least squares fit of the [Graphics:Images/datafitting_gr_21.gif] data to a cubic polynomial of the form [Graphics:Images/datafitting_gr_22.gif].  We also compute a quadratic fit for comparison.

Here are the fits.

[Graphics:Images/datafitting_gr_23.gif]
[Graphics:Images/datafitting_gr_24.gif]
[Graphics:Images/datafitting_gr_25.gif]
[Graphics:Images/datafitting_gr_26.gif]

Now plot all three fits together. The plot is suppressed through the [Graphics:Images/datafitting_gr_27.gif] option, but we give it the name fitPlotCv so that we can use it below.

[Graphics:Images/datafitting_gr_28.gif]

Now show these three plots along with the data.

[Graphics:Images/datafitting_gr_29.gif]

[Graphics:Images/datafitting_gr_30.gif]

 

Now let's do the same procedure with the [Graphics:Images/datafitting_gr_31.gif] data.

[Graphics:Images/datafitting_gr_32.gif]
[Graphics:Images/datafitting_gr_33.gif]
[Graphics:Images/datafitting_gr_34.gif]
[Graphics:Images/datafitting_gr_35.gif]
[Graphics:Images/datafitting_gr_36.gif]
[Graphics:Images/datafitting_gr_37.gif]

[Graphics:Images/datafitting_gr_38.gif]

 

The standard add-on package Statistics`LinearRegression` extends the functionality of Fit by allowing access to a great deal of detail about the statistical properties of the resulting fit.

This loads the package

[Graphics:Images/datafitting_gr_39.gif]

Here is an example.

[Graphics:Images/datafitting_gr_40.gif]
[Graphics:Images/datafitting_gr_41.gif]

Here is a list of the estimated variances for linear, quadratic, and cubic fits. First the [Graphics:Images/datafitting_gr_42.gif] data:

[Graphics:Images/datafitting_gr_43.gif]
[Graphics:Images/datafitting_gr_44.gif]

Now the [Graphics:Images/datafitting_gr_45.gif] data:

[Graphics:Images/datafitting_gr_46.gif]
[Graphics:Images/datafitting_gr_47.gif]

Nonlinear Fits: Fitting to a Gaussian Function

Generate Some Artificial Data

We will first create a data set based on normally distributed random numbers. The standard add-on package Statistics`NormalDistribution` extends the built in function Random[] so that it can generate normally distributed random numbers. The standard add-on package Statistics`ContinuousDistributions` generalizes Random[] to a large variety of other probability distributions (if you load Statistics`ContinuousDistributions`, then Statistics`NormalDistribution` is automatically loaded as well).  

First load the package (we also load the standard add-on package Statistics`DataManipulation` to enable the BinCounts function that allows us to easily bin the data that we will generate).

[Graphics:Images/datafitting_gr_48.gif]

Here is the symbolic expression for the normal probability density function.

[Graphics:Images/datafitting_gr_49.gif]
[Graphics:Images/datafitting_gr_50.gif]

Now generate 1000 random normally distributed numbers with a mean of [Graphics:Images/datafitting_gr_51.gif] and a standard deviation of [Graphics:Images/datafitting_gr_52.gif].

[Graphics:Images/datafitting_gr_53.gif]

Now bin the data using BinCounts. The result is normalized to a total probability of one, and the amount in each bin is associated with the value of the bin's center point.

[Graphics:Images/datafitting_gr_54.gif]

Analysis of Gaussian Data

Here is the plot of the data that we generated above along with the normal curve that it was based upon.

[Graphics:Images/datafitting_gr_55.gif]

[Graphics:Images/datafitting_gr_56.gif]

 

If we did not know that this data had come from a normal distribution but nonetheless wished to fit it to a parameterized Gaussian function, we could make use of the nonlinear fitting functions provided by the standard add-on package Statistics`NonlinearFit` (which, among other methods, implements the Levenberg-Marquardt method). First we load the package.

[Graphics:Images/datafitting_gr_57.gif]

Now we use NonlinearFit, guessing starting values for the parameters based on the graph above.

[Graphics:Images/datafitting_gr_58.gif]
[Graphics:Images/datafitting_gr_59.gif]

Here is the fit function graphically compared to the data and the original Gaussian function.

[Graphics:Images/datafitting_gr_60.gif]

[Graphics:Images/datafitting_gr_61.gif]

 

While NonlinearFit returns the parameterized fitting function, NonlinearRegress returns a list of rules that specify, in addition to the fitting parameters, a variety of statistical quantities associated with the fit. (There is much more information that can be returned from NonlinearRegress [Graphics:Images/datafitting_gr_62.gif] through the RegressionReport option [Graphics:Images/datafitting_gr_63.gif] in addition to the set shown here: see the documentation for Statistics`NonlinearFit` for more detail.)

[Graphics:Images/datafitting_gr_64.gif]
[Graphics:Images/datafitting_gr_65.gif]

For further information on our services send email to info@scientificarts.com .
Contents of this web site Copyright © 1999-2011, Scientific Arts, LLC.

s