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

root-finding

v1.0.2

Published

A javascript CLI implementation of root finding using Newton-Raphson's method.

Downloads

9

Readme

Root-Finding

Build Status

A javascript implementation of root finding using Newton-Raphson's method. Users are able to enter their own expressions through the console and they are parsed by the mathjs parser.

Installation

You can install this package through npm:

npm install root-finding

Usage

For the command line version of the Newton-Raphson's algorithm, write this into a file:

const nr = require('root-finding');

nr.newtonRaphson-CLI();

Then run the script with node script.js and enter in the values that you're prompt with.

For the pure function, use:

/**
* @param {number} guess - The initial x value to guess where the root is.
* @param {number} increment - The delta x to be used for the next guess.
* @param {number} iteration - The number of times to perform the algorithm.
* @param {number} eps - The smallest f(x) value to be considered a root.
* @param {Function} f - The expression whose root you want to solve.
*/
nr.newtonRaphson(guess, increment, iteration, eps, f);

// Example

function FofX(x) {
	return x + 2; // Our expression is f(x) = x + 2.
}

nr.newtonRaphson(1, 0.1, 10, 0.001, FofX); // => returns -2.

Description

Newton-Raphson is a fundamental algorithm in computational science. It uses a guess and check strategy that finds the roots to functions that are continuous and well behaved. Root finding is important because it allows us to solve system of equations that involve higher order polynomials.

(x2 + 1) = (4 - x3)   =>   (x3 + x2 - 3) = 0

The algorithm itself derives from analytical geometry. When we're trying to find the root of an expression, we are looking for the x-value where the curve of that equation crosses the x-axis. That gives us an f(x) = 0 value. Newton-Raphson takes a guess, or an arbitrary x-value, that is close to the root and constructs a tangent line to that curve. It then checks if the zero root of that line is also the root of the curve by plugging it into the equation. If that isn't the root, then it uses the value as the next guess, repeating it until f(xi) ~ 0, where i is the iteration.

When we construct the tangent line, we have the equation:

y = mx + b

We see that m, the slope, is just the derivative of the curve. For the y-intercept, it may be difficult to see but suppose you construct a line that crosses (0, -a). You will notice that the slope multipled by some arbitrary value xi will get us a g(xi) value that is shifted a above the original line. If you take the difference between f(x) and g(x), you will get the y-intercept. So:

b = f(xi) - g(xi) = f(xi) - f'(xi)(xi)

Formulating the final equation, we have:

y = f'(xi)(x) + f(xi) - f'(xi)(xi)

Or:

y = f'(xi)(x - xi) + f(xi)

Setting y to zero, we reduce the equation to:

x = xi - f(xi) / f'(xi)

Which is just the root of the tangent line. In this way, we generate new guesses (x) that we will plug into our equation and see if it is approximately equal to the root.

Newton-Raphson, of course, have limitations. It will not be able to handle discontinuous functions and functions that generate an infinite number of new guesses because of a loop.