Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including ml-levenberg-marquardt with all npm packages installed. Try it out:

'use strict'; var levenbergMarquardt = require('ml-levenberg-marquardt'); function sinFunction([a, b]) { return (t) => (a * Math.sin(b * t)); } var len = 20; var data = { x: new Array(len), y: new Array(len) }; var sampleFunction = sinFunction(2, 2); for (var i = 0; i < len; i++) { data.x[i] = i; data.y[i] = sampleFunction(i); } const options = { damping: 0.001, initialValues: [3, 3] }; var ans = levenbergMarquardt(data, sinFunction, options); console.log(ans);

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

ml-levenberg-marquardt v1.0.1

Curve fitting method in javascript

levenberg-marquardt

NPM version build status Test coverage David deps npm download

Curve fitting method in javascript

Installation

$ npm install ml-levenberg-marquardt

API Documentation

This algorithm is based on the article Brown, Kenneth M., and J. E. Dennis. "Derivative free analogues of the Levenberg-Marquardt and Gauss algorithms for nonlinear least squares approximation." Numerische Mathematik 18.4 (1971): 289-297.

In order to get a general idea of the problem you could also check the Wikipedia article.

Example

// import library
const LM = require('ml-levenberg-marquardt');

// function that receives the parameters and returns
// a function with the independent variable as a parameter
function sinFunction ([a, b]) {
  return (t) => (a * Math.sin(b * t));
}

// array of points to fit
let data = {
    x: [ /* x1, x2, ... */ ],
    y: [ /* y1, y2, ... */ ]
};

// array of initial parameter values
let initialValues = [ /* a, b, c, ... */ ];

const options = {
  damping: 1.5,
  initialValues: initialValues,
  gradientDifference: 10e-2,
  maxIterations: 100,
  errorTolerance: 10e-3
};

let fittedParams = LM(data, sinFunction, options);

Or test it in Runkit

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free