go-hep.org/x/hep@v0.38.1/fit/curve_nd.go (about)

     1  // Copyright ©2020 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fit
     6  
     7  import (
     8  	"gonum.org/v1/gonum/optimize"
     9  )
    10  
    11  // CurveND returns the result of a non-linear least squares to fit
    12  // a function f to the underlying data with method m, where there
    13  // is more than one independent variable.
    14  func CurveND(f FuncND, settings *optimize.Settings, m optimize.Method) (*optimize.Result, error) {
    15  	f.init()
    16  
    17  	p := optimize.Problem{
    18  		Func: f.fct,
    19  		Grad: f.grad,
    20  		Hess: f.hess,
    21  	}
    22  
    23  	if m == nil {
    24  		m = &optimize.NelderMead{}
    25  	}
    26  
    27  	p0 := make([]float64, len(f.Ps))
    28  	copy(p0, f.Ps)
    29  	return optimize.Minimize(p, p0, settings, m)
    30  }