Example 2: Minimum of a multivariate function

The minimum of 100(x_2 - x_1^2)^2 + (1-x_1)^2 is found using function evaluations and a user supplied gradient.
using System;
using Imsl.Math;

public class MinUnconMultiVarEx2 : MinUnconMultiVar.IGradient
{
    public double F(double[] x)
    {
        return 100.0 * ((x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0])) + 
            (1.0 - x[0]) * (1.0 - x[0]);
    }

    public void Gradient(double[] x, double[] gp)
    {
        gp[0] = - 400.0 * (x[1] - x[0] * x[0]) * x[0] - 2.0 * 
            (1.0 - x[0]);
        gp[1] = 200.0 * (x[1] - x[0] * x[0]);
    }
    

    public static void  Main(String[] args)
    {
        MinUnconMultiVar solver = new MinUnconMultiVar(2);
        solver.SetGuess(new double[]{- 1.2, 1.0});

        double[] x = solver.ComputeMin(new MinUnconMultiVarEx2());
        Console.Out.WriteLine
            ("Minimum point is (" + x[0] + ", " + x[1] + ")");
    }
}

Output

Minimum point is (0.999999966882301, 0.999999932254245)

Link to C# source.