Non Linear Curve Fitting Using Spreadsheets

Embed Size (px)

Citation preview

  • 7/29/2019 Non Linear Curve Fitting Using Spreadsheets

    1/2

    448 Journal of Chemical Education Vol. 74 No. 4 April 1997

    Information Textbooks Media Resources

    computer bulletin boardedited by

    Steven D. GammonUniversity of Idaho

    Moscow, ID 83844

    Nonlinear Curve Fitting Using Spreadsheets

    Juan O. Machuca-Herrera

    Departamento De Qumica Inorgnica, Instituto de Qumica

    Universidade Federal do Rio de Janeiro Cidade UniversitariaIlha do Fundo, 21949-200 Rio de Janeiro, Brasil

    The use of spreadsheets in chemistry is common,mainly in analytical and physical chemistry, where they areused to calculate systems of linear equations, nonlinearequations using the iterative NewtonRaphson method, lin-ear least squares regressions, etc. All these applications arewell described in articles (15) and books (6, 7). Besides cal-culational facilities, commercial spreadsheets also have ad-vanced numerical tools packages that allow sophisticatedcalculations in multivariate statistics, as well as linear andnonlinear optimizations, Fourier transforms, and much

    more. These packages often contain graphical facilities thatallow 2D and 3D graphics of functions and discrete pointsets. However, finding these tools is not easy, because theyare usually not readily apparent from the documentationaccompanying the programs.

    The goal of this article is to emphasize the capabilitiesof spreadsheets in solving specific problems common inchemistry, such as nonlinear curve fitting. Nonlinear leastsquares fitting is used, for example, in deconvolutions ofoverlapping bands in vibrational or electronic spectra andin analysis of chemical kinetics. Frequently, fits of nonlin-ear equations are done using the GaussNewton orMarquardt methods (810). However in this work we usethe quasi-Newton method (11).

    The method for nonlinear curve fitting presented here

    is simple and any undergraduate student, with some knowl-edge of spreadsheets, can easily program any of the ex-amples shown. The principal difference between this workand other articles (5, 10) on this subject is that in previousarticles macro structures are necessary for carrying out thecalculations, whereas here the Solver is used. The ability ofspreadsheets to fit nonlinear equations is shown with sev-eral examples. The results obtained are compared to otherresults available in the literature which use the GaussNewton or Marquardt methods.

    The nonlinear least squares method uses a function,y,to fit the experimental points, which has the form:

    y = bjfj(x)j = 1

    n

    (1)

    where the coefficients bjare adjustable parameters and x isthe vector whose components are the independent variables.The objective function to be minimized is the sum of thesquares, SS,

    SS = wi yi bjfj(x)

    j = 1

    n 2

    i = 1

    m

    (2)

    where wirepresents the weight associated with the experi-mental point (xi,yi). Commonly, the wielements are consid-ered unitary. At the minimum (b*) all of the components ofthe gradient g of SS vanish. Thegi components of the gra-dient vector are given by the derivatives of the function SS

    with respect to the bi parameters:

    g= SS

    b1, ... SS

    bn(3)

    The natural criterion of an optimal estimate of b is azero value of the gradient g. Many methods of a minimumsearch terminate the iterative process when the norm of thegradient:

    g2 = gj2j = 1n (4)is sufficiently small. It is possible to select a critical valueof this norm, for example 105, the limit under which thepoint bi is considered as a local minimum. Often iterationsterminate when too small a change of the estimated param-eter appears. This criterion is valid for derivative as wellas nonderivative methods. Derivative methods are usedwhen the functionf(x) is at least twice differentiable. Thequasi-Newton methods fall into this class of methods.

    All calculations were carried out using Excel 5.0 forWindows on a PC-AT 80486 DX2-66 MHz IBM compatible.All that is necessary to do the calculations is to prepare theworksheets for each example (i.e., any equation that is to

    be used for nonlinear modeling and the experimentalpoints) and then select the Solver option from the principalmenu.

    From the Solver dialog box the options can be chosen.In the Set Target Cell box, type (or indicate) the cell con-taining the SS equation on the worksheet, then choose theMax (maximize) or Min (minimize) option. The original de-fault Solver parameters were used in all examples dis-cussed here. In situations where a strong nonlinearity isevident, these parameters should be modified to improvethe fit and to avoid divergence. Finally, after a few itera-tions (typically 710) the minimal condition is satisfied anda stable solution is reached. The optimized values of theparameters, together with the residuals and the SS, arepasted up into the worksheet. Also, the graphics containingthe adjusted data can be put into the worksheet easily.

    The examples considered here were chosen becausethey are known experimentally and represent classical situ-ations found in first-order chemical reactions. This agreeswith the aim of this article, which seeks to explore charac-teristics present in commercial spreadsheets and frequentlynot used. These powerful tools can be used along withchemometrics.

    The mathematical models used were :

    Model I: A First-Order Chemical Reaction (type one)

    A = A 1 exp (kt)

  • 7/29/2019 Non Linear Curve Fitting Using Spreadsheets

    2/2

    Vol. 74 No. 4 April 1997 Journal of Chemical Education 449

    Information Textbooks Media Resources

    Model II: A First-Order Chemical Reaction (type two)

    P = P 1 exp (kt) + P0 exp (kt)

    Model III: An Exponential Equation withNonseparable Parameters

    Y = b1exp

    b2b3 + x

    A critical point in nonlinear fits is the initial choice ofparameter values. If the initial values are far from the trueones, a poor convergence may be obtained, frequently lead-ing to a lack of fit. In spite of the fact that different initialpoints were tested, the calculations converged to similarparameter values for all examples.

    These results agree well with results obtained usingthe different methods available in the literature (810, 12).Considering, for example, the results obtained from ModelII, our best fit parameters are :P0 = 0.544 (0.544), P = 0.299(0.301), k = 26.4 (26.8) s1. [Values in parentheses were ob-tained by Moore (9)]. The initial SS value of 11,809.0 wasreduced after 5 cycles to 33. The results obtained demon-

    strate that this method is a fast and easy alternative to fitexperimental points to any nonlinear equation. The good-ness-of-fit in all cases is in agreement with that available inthe literature (810, 12).

    Acknowledgment

    I thank Ira M. Brinn for corrections and a critical read-ing of this article and for many valuable suggestions.

    Literature Cited

    1. Rioux, F.J. Chem. Educ. 1991, 68, A282A283.2. Currie, J. O.; Whiteley, R. V., Jr.J. Chem. Educ.1991, 68, 923926.3. David, C. W.J. Chem. Educ. 1989, 66, A237A238.4. Simpson, J. M.J. Chem. Educ. 1994, 71, A88A90.5. OConnor, S; Bailey, D.J. Chem. Educ. 1994,71, A91A92.6. Gaeng, P. EXC EL for Science and Technology; Abacus: Grand

    Rapids, MI, 1993.7. Freiser, H. Concepts & Calculations in Analytical Chemistry: A

    Spreadsheet Approach; CRC: Boca Raton, FL, 1992.8. Copeland, T. G.J. Chem. Educ.1984, 61, 778779.9. Moore, P.J. Chem. Soc. Faraday Trans. I,1972, 18901893.

    10. Poshusta, R. D. Comput. Phys. 1991,5(2), 248252.11. Himmelblau, M. D.Applied Nonlinear Programming; McGraw-Hill:

    New York, 1972.12. Militky, J.; Meloun, M. Talanta1993, 40, 269277.