npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fzero

v0.2.4

Published

Find a zero of a univariate function.

Downloads

83

Readme

fzero

Build Status Coverage Status npm version

Find a zero of a univariate function. This is a JavaScript port of Jaroslav Hajek's fzero implementation in Octave.

This is essentially the ACM algorithm 748: Enclosing Zeros of Continuous Functions due to Alefeld, Potra and Shi, ACM Transactions on Mathematical Software, Vol. 21, No. 3, September 1995. Although the workflow should be the same, the structure of the algorithm has been transformed non-trivially; instead of the authors' approach of sequentially calling building blocks subprograms we implement here a FSM version using one interior point determination and one bracketing per iteration, thus reducing the number of temporary variables and simplifying the algorithm structure. Further, this approach reduces the need for external functions and error handling. The algorithm has also been slightly modified.

Usage

$ npm install fzero

To use fzero in Node.js, simply require it:

var fzero = require("fzero");

A minified, browserified file dist/fzero.min.js is included for use in the browser. Including this file attaches a fzero object to window:

<script src="dist/fzero.min.js" type="text/javascript"></script>

fzero is a function that takes 3 required arguments: the function to find the zero of, a lower-bound for the zero, and an upper-bound for the zero. A fourth optional argument can be used to adjust fzero's settings; for example, maxiter: 50 sets the maximum number of iterations to 50, and verbose: true prints details of each iteration. (See tests for details.)

var myFunction = function (x) { return Math.cos(Number(x)).toString(); }
var lowerBound = 0;
var upperBound = 3;
var options = {maxiter: 50};
var zero = fzero(myFunction, [lowerBound, upperBound], options);

If you don't know the exact bounds, you can simply pass fzero an initial guess instead:

var initialGuess = 2;
var zero = fzero(myFunction, initialGuess, options);

fzero returns an object that has the following fields:

  • solution: x such that myFunction(x) = 0
  • fval: The numerical value of myFunction at solution.
  • code: Exit flag which can have one of the following values.
    • 1: The algorithm converged to a solution.
    • 0: Maximum number of iterations or function evaluations has been reached.
    • -1: The algorithm has been terminated from user output function.
    • -5: The algorithm may have converged to a singular point.
  • diagnostic: An object which has the following fields.
    • iterations: The number of iterations completed.
    • functionEvals: Number of times myFunction was evaluated.
    • bracketx: An array [lower, upper] with the ending lower and upper x-bounds.
    • brackety: An array [f(lower), f(upper)] with the ending lower and upper y-bounds.

Note: fzero uses decimal.js for arithmetic, so the input function should accept a string input (rather than a JS number).

Tests

Unit tests are included in test/, and can be run using npm:

$ npm test