Example 2: Nonlinear least-squares problem
A nonlinear least-squares problem is solved using a user-supplied Jacobian.
using System;
using Imsl.Math;
public class NonlinLeastSquaresEx2 : NonlinLeastSquares.IJacobian
{
public void F(double[] x, double[] f)
{
f[0] = 10.0 * (x[1] - x[0] * x[0]);
f[1] = 1.0 - x[0];
}
public void Jacobian(double[] x, double[,] fjac)
{
fjac[0,0] = - 20.0 * x[0];
fjac[1,0] = 10.0;
fjac[0,1] = - 1.0;
fjac[1,1] = 0.0;
}
public static void Main(String[] args)
{
int m = 2;
int n = 2;
double[] x = new double[n];
NonlinLeastSquares zs = new NonlinLeastSquares(m, n);
zs.SetGuess(new double[]{- 1.2, 1.0});
zs.SetXscale(new double[]{1.0, 1.0});
zs.SetFscale(new double[]{1.0, 1.0});
x = zs.Solve(new NonlinLeastSquaresEx2());
for (int k = 0; k < n; k++)
{
Console.Out.WriteLine("x[" + k + "] = " + x[k]);
}
}
}
Output
x[0] = 1
x[1] = 1
Link to C# source.