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

differential-solver

v1.0.1

Published

Euler's Method Differential Equations Solver

Downloads

71

Readme

Differential Equation Solver for TypeScript

A simple Typescript Library for Approximating Solutions to Ordinary Differential Equations using Euler's method.

This project is very simple, but is a proving ground for my future ideas for Mathematics in JS/TS.

Installation

Installation is easy using NPM:

  • Install it directly from NPM

      npm i differential-solver
  • From GitHub

      npm i github.com/cooperw824/differential-solver
  • From Source

      git clone github.com/cooperw824/differential-solver
    
      cd path/to/project
    
      npm i path/to/differential-solver

Usage

To start, create a new DifferentialEquation object:

new DifferentialEquation(
differential:(x: number, y:number) => number,
independentVarInitialCondition:number,
dependentVarInitialCondition:number )

The DifferentialEquation class takes three parameters:

  • The Differential Equation to Evaluate (Required)

    • A function with two number inputs that outputs a number
    (x: number, y: number) => number;
  • The Independent Variable's (x-value) Initial Condition (Required)

    • A number that indicates the initial value for X
  • The Dependent Variable's (y-value) Initial Condition (Required)

    • A number that indicates the initial value for y

In typescript:

import {DifferentialEquation} from 'differential-solver';

let differential  = new DifferentialEquation((x + y) => x+y, 0, 1);

In JavaScript:

import {DifferentialEquation} from 'differential-solver'; // ES Modules Import

// const { DifferentialEquation } = require('differential-solver'); // CommonJS Import

let differential  = new DifferentialEquation((x + y) => x+y, 0, 1);

Approximating Solutions to Differential Equations

eulersMethod(
targetIndependentVariable: number,
steps: number,
options?: EulersMethodOptions)

To approximate the solution of Differential Equation at certain independent variable, make a call to the Euler's Method function

The Euler's Method function has two different modes: recursive and non recursive, they both return the same answer, but if your desired number of steps is too large you may want to use the non-recursive version.

  • Parameters
    • targetIndependentVariable: number (Required)
      • The x-value to approximate a solution for
      • f(targetIndependentVariable) ≈ output
    • steps: number (Required)
    • options: EulersMethodOptions (Optional)
      • recursive: Boolean (Optional)
        • Should the recursive or non recursive version of the algorithm be used? Defaults to false.
      • rounding: Number (Optional)
        • Round to this many decimals. Defaults to 3.

Non-recursive version

By default the non-recursive function runs:

import {DifferentialEquation} from 'differential-solver';

let differential  = new DifferentialEquation((x + y) => x+y, 0, 1);

// approximates f(1) in 3 steps, when dy/dx = x + y and f(0) = 1 
let approximation = differential.eulersMethod(1, 3)

console.log(approximation)
// prints: 2.741

To add more decimals after the decimal point:

import {DifferentialEquation} from 'differential-solver';

let differential  = new DifferentialEquation((x + y) => x+y, 0, 1);

// approximates f(1) in 3 steps, when dy/dx = x + y and f(0) = 1 
let approximation = differential.eulersMethod(1, 3, {rounding: 10})

console.log(approximation)
// prints: 2.7407407407

Recursive Version

Using the recursive version is as easy as setting the recursive option to true:

import {DifferentialEquation} from 'differential-solver';

let differential  = new DifferentialEquation((x + y) => x+y, 0, 1);

// approximates f(1) in 3 steps, when dy/dx = x + y and f(0) = 1 
let approximation = differential.eulersMethod(1, 3, {recursive: true})

console.log(approximation)
// prints: 2.741

To add more decimals after the decimal point:

import {DifferentialEquation} from 'differential-solver';

let differential  = new DifferentialEquation((x + y) => x+y, 0, 1);

// approximates f(1) in 3 steps, when dy/dx = x + y and f(0) = 1 
let approximation = differential.eulersMethod(1, 3, {rounding: 10, recursive: true})

console.log(approximation)
// prints: 2.7407407407

Evaluating a Derivative at a Point

evaluateDifferential(independentVar?: number,
dependentVar?: number,
rounding?: number)

Returns the value of the derivative at the given point

  • Parameters:
    • independentVar: number (Optional)
      • The x value to evaluate the derivative at. Defaults to the independent variable's initial condition
    • dependentVar: number (Optional)
      • The y value to evaluate the derivative at. Defaults to the dependent variable's initial condition
    • rounding: number (Optional)
      • How many numbers after the decimal point to round to. Defaults to not rounding.
  • Returns
    • differential(independentVar, dependentVar)
    • The derivative at the given point x,y

Using initial condition:

import {DifferentialEquation} from 'differential-solver';

let differential  = new DifferentialEquation((x + y) => x+y, 0, 1);

let derivative = differential.evaluateDifferential()

console.log(derivative)
// Prints 1

Using given values:

import {DifferentialEquation} from 'differential-solver';

let differential  = new DifferentialEquation((x + y) => x+y, 0, 1);

let derivative = differential.evaluateDifferential(3.14159, 2.718)

console.log(derivative)
// Prints 5.85959

Using given values, and given rounding:

import {DifferentialEquation} from 'differential-solver';

let differential  = new DifferentialEquation((x + y) => x+y, 0, 1);

let derivative = differential.evaluateDifferential(3.14159, 2.718, 3)

console.log(derivative)
// Prints 5.86
// 5.85959 rounds to 5.86

Building From Source

A build is packaged with the code in the dist folder, but to build the program on windows you can just run

    npm run build

for other operating systems you may need to adjust the fixup script and copy command.

Contributing

This is a very small library, but acts as my proving ground for building future JS / TS modules. I have a few ideas for expanding this, but if you have any issues or ideas please open a GitHub issue.

If you have a feature you would to add, fork the repository, implement your feature, add / verify tests, and then open your pull request.