Surface fitting and more | Extreme Optimization Numerical Libraries for .NET Professional |

So far, we have been dealing with nonlinear functions of one variable. In this section, we will show how to fit nonlinear functions of any number of variables of arbitrary types.

When defining what nonlinear fitting means for arbitrary inputs, the key observation is that in the definition of a nonlinear curve,

f(x) = f(a_{1}, a_{2}, ...,
a_{n}; x)
,

the variable x can be of any type. The actual fitting algorithm only does calculations in terms of the curve parameters and in terms of functions defined by the user.

The NonlinearCurveFitter

The nonlinear function is defined by a delegate that maps a
T and a
Vector

The data is supplied as Vector

By default, the fit is unweighted. All weights are set equal to 1. Weights can be specified in one of two ways. The first way is to set the WeightVector property to a vector with as many components as there are data points. Each component of the vector specifies the weight of the corresponding data point.

If error estimates for the Y values are available, then the
GetWeightVectorFromErrors of the
WeightFunctions
class can be used to construct a vector with the weights
that correspond to the errors. Its only argument is a
Vector

The Optimizer property gives access to the Levenberg-Marquardt optimizer that is used to compute the least squares solution. You can set tolerances and control convergence as set out in the chapter on optimization.

The Fit method performs the actual least
squares calculation, and adjusts the parameters of the Curve to correspond to the least squares fit. The
BestFitParameters property returns a
Vector

The Optimizer property also gives access to details of the calculation. It is useful to inspect its Status property to make sure the algorithm ended normally. A value other than Converged indicates some kind of problem with the algorithm.

We will use a very simple example: fitting a plane through a set of points.

Vector<Point> xValues = Vector.Create( new Point(-1, 1), new Point(1, 1), new Point(1, -1), new Point(-1, -1)); Vector<double> yValues = Vector.Create(0.0, 1.0, 2.0, 3.0); NonlinearCurveFitter<Point> fitter = new NonlinearCurveFitter<Point>(); fitter.XValues = xValues; fitter.YValues = yValues; fitter.Function = (x, p) => p[0] + p[1] * x.X + p[2] * x.Y; fitter.InitialGuess = Vector.Create<double>(3); fitter.Fit();

The data points are defined here to be Point structures. The function is a simple linear function of the parameters.

Copyright Â© 2004-2018,
Extreme Optimization. All rights reserved.

*Extreme Optimization,* *Complexity made simple*, *M#*, and *M
Sharp* are trademarks of ExoAnalytics Inc.

*Microsoft*, *Visual C#, Visual Basic, Visual Studio*, *Visual
Studio.NET*, and the *Optimized for Visual Studio* logo

are
registered trademarks of Microsoft Corporation.