 Application Center - Maplesoft

# Classroom Tips and Techniques: Nonlinear Curve Fitting

You can switch back to the summary page by clicking here. Classroom Tips and Techniques: Nonlinear Curve Fitting
Robert J. Lopez
Emeritus Professor of Mathematics and Maple Fellow
Maplesoft

Introduction

I was recently asked for help in fitting a nonlinear curve to data, and then drawing related graphs of the data and the fitting curve.  This month's article summarizes all the advice about nonlinear least squares I generated in response to the user's questions.

Initializations  Problem Statement

Table 1 contains, with simplified notation, the data and fitting function presented to me.  The data points , were presented in the form of two vectors X and Y, and the fitting function was given as , with and as two parameters to be found.  The fitting function is indeed  nonlinear in these parameters. , , Table 1   The data for the nonlinear least squares fit

Graph of the Data Points

Table 2 shows how the data can be entered in a syntax-free manner.

 Control-drag from Table 1     Context Menu: Assign Name  Control-drag from Table 1     Context Menu: Assign Name  Control-drag from Table 1       Context Menu: Assign Function   Table 2   Syntax-free data-entry

Figure 1, a graph of the points and their piecewise linear interpolant, can be obtained with the Curve Fitting Assistant, as per the steps articulated in Table 3.

• Enter the sequence and press the Enter key.

• Context Menu: Curve Fitting?Interactive Curve Fitting? • In the Splines section, select degree-1 and press Plot

• On "Done" return Plot

• Press Done    Table 3   Use of the Curve Fitting Assistant to graph data points and their piecewise linear interpolant

The points in Figure 1 were rendered as solid dots by using the Context Menu for the graph (click on one of the points) and selecting Symbol?Circle (Solid). Figure 1   Data points and piecewise linear interpolant

If the Curve Fitting Assistant is not used, the graph in Figure 1 would have to be constructed by overlaying the separate graphs of the points and the interpolant.

Least Squares

The Fit command in the Statistics package decides if a linear or nonlinear algorithm is called.  The fitting function is obtained with

 >  >

where the numeric value of the option "iterationlimit" was found by trial-and-error.  Additional information about the fitting function and its calculation can be obtained by generating the module

 > >

and interrogating it with

 >          >

The module content is a sequence we have displayed elementwise by use of the for-loop.  The values of the parameters, the minimum value of the sum of squares of deviations, and the individual residuals are amongst the items recovered from the calculation.  In particular, the values of the parameters and can be accessed via

 >  >

That the product in the numerator of the fitting function is ? can be verified by the following evaluation.

 >  >

In Figure 2, we have graphed the least-squares fitting function, and added to it the graph of the data and their piecewise linear interpolant from Figure 1.  The graph of the least-squares function is obtained with the Plot Builder, launched from the Context Menu.  The graph in Figure 1 is added by simply copying and pasting .  Of course, the alternative would be creating two separate plots and joining them with the display command in the plots package. Figure 2   The least-squares fitting function, the data points, and their piecewise linear interpolant.

First Principles

The nonlinear fit can also be obtained by forming and minimizing the sum of squares of deviations

 > >

differentiating to obtain the normal equations

 > >

and solving numerically to obtain

 >  >

The value of at this solution is

 >  >

in agreement with the value found earlier.

Alternatively, can be minimized with the Optimization assistant, as per Figure 3. Figure 3   Minimization of sum of squares by the Optimization Assistant

The Edit button is used to insert the Objective Function, and this can be referenced by its name, .  On the left, select as solver the nonlinear simplex method.  Setting the number of function evaluations to 200 will allow the Solve button to find the minimum.

Functions of the Optimal Parameters

After having provided this nonlinear least-squares solution, I was further asked how another function could make use of the optimal parameters in an expression such as

 >  >

The simplest way to form the desired function is shown in Table 4.  The alternative would involve use of the unapply command.

 Using the evaluation template from the Expression palette, form the equation Context Menu: Assign Function   Table 4   Forming a function containing the optimal parameters

Proof that the desired function has been generated is given by

 >  >

To evaluate this function at the nodes , we could use the new syntax for elementwise operations, as in

 >  >

Alternatively, in the Context Menu for , select "Map Command Onto," and in the pop-up, provide the name Figure 4, a graph of and the points can be constructed from separate plots of the points and of (using the display command from the plots package), or it can be constructed interactively via the Context Menu. Figure 4   Graph of and the points The interactive construction of Figure 4 begins with juxtaposing the two vectors and selecting Plots?Plot Builder from the Context Menu.  The domain for a plot of the six points is .  The expression for can be dropped onto the graph of the points, with the Context Menu for the resulting composite graph used to re-adjust the plot window.  Select Axes?Properties, and in the dialog that appears, uncheck "Use Data Extents" so appropriate Range values can be inserted.

Legal Notice:  Maplesoft, a division of Waterloo Maple Inc. 2009. Maplesoft and Maple are trademarks of Waterloo Maple Inc. This application may contain errors and Maplesoft is not liable for any damages resulting from the use of this material. This application is intended for non-commercial, non-profit use only. Contact Maplesoft for permission if you wish to use this application in for-profit activities.