Maxima by Example, Chapter 14: Fitting a Model Function to Data

Ted Woollett

Chapter 14 provides examples of the use of a new set of Maxima functions (defined in fit.mac). These new functions not only return the best-fit parameter values, but also the estimated parameter uncertainties and the chi squared probability of the results. These new functions assume one independent variable and one corresponding dependent variable and are called fit_line, fit_slope, fit_y_intercept, lfit, and nlfit. These new functions also allow for the use, while finding best fit model parameters, of the estimated uncertainties of the measured dependent variable.

This chapter leans heavily on the text: Data Reduction and Error Analysis for the Physical Sciences, 3rd ed., by Philip R. Bevington and D. Keith Robinson.

  1. : Jan 15, 2017, zip file contains all following files.
  2. --mbe14fit.pdf : Jan 15, 2017, Maxima 5.36.1, 66 pages
  3. --mbe14fit.tex : Jan 15, 2017, Latex code file
  4. --fit.mac : Maxima code, Jan 15, 2017, Maxima 5.36.1
  5. --qdraw.mac : A Graphics Interface to draw2d: Jan 15, 2017, Maxima 5.36.1
  6. --coffee.dat : coffee cooling data file
  7. --mbe14-fit1.dat : Ex. 1 Data File
  8. --mbe14-fit2.dat : Ex. 2 Data File
  9. --mbe14-fit3.dat : Ex. 3 Data File
  10. --mbe14-fit4.dat : Ex. 4 Data File
  11. --mbe14-fit5.dat : Ex. 5 Data File
  12. --mbe14-fit6.dat : Ex. 6 Data File
  13. --mbe14-fit7.dat : Ex. 7 Data File
  14. --mbe14-fit8.dat : Ex. 8 Data File

Chapter 14 Topics

The most useful functions defined in fit.mac are listed here with the syntax. Mdata is a data matrix. sigL is a list of estimated measurement errors. dof is the number of degrees of freedom (the number of data points minus the number of model parameters being fitted). ymodel is an expression which includes some symbolic unknown parameters (to be found).
  1. fit_line (Mdata, sigL), Two Parameter Straight Line Fit
  2. fit_slope (Mdata, sigL, y-intercept), One Parameter Straight Line Fit, Given the Y-intercept
  3. fit_y_intercept (Mdata, sigL, slope), One Parameter Straight Line Fit, Given the Slope
  4. lfit (Mdata, sigL, ymodel, paramL), General Linear Fit
  5. nlfit (Mdata, sigL, ymodel, paramL, param-initL), General Nonlinear Fit, Levenberg-Marquardt Method
  6. moment (dataL)
  7. y_gaussian_PE (Mdata, dof, ymodel), Probable Data Error if Gaussian
  8. get_chi2 (Mdata, sigL, yfit_expr), ChiSquare Value Based on Fitted Parameters
  9. chi2 prob (chi2, dof), ChiSquare Probability and Reduced ChiSquare
  10. Vsearch (Mdata, sigL, ymodel, paramL, param-valuesL), Visual Search for Parameter Values
  11. grid search(Mdata, sigL,ymodel,paramL,param-initL,stepFactor), Grid Search Method

Nine examples are worked out in detail in the first part of the chapter. Seven of these examples are taken from the text "Data Reduction and Error Analysis for the Physical Sciences," by Philip R. Bevington and D. Keith Robinson, 3rd Ed, 2003, McGraw Hill. The chapter ends with a review of statistical concepts needed and also a derivation of the matrix method used for the general linear fit function lfit. A list of useful references concludes this chapter.

The text files fit.mac and qdraw.mac are free software: you can redistribute it and/or modify it under the terms of the GNU GENERAL PUBLIC LICENSE, Version 2, June 1991, as published by the Free Software Foundation. For more information see the license information at the top of the pdf file.