Skip to content

Commit

Permalink
Merge branch 'release/0.1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-hanna committed Apr 22, 2021
2 parents a4500af + 62c7aa1 commit f655d89
Show file tree
Hide file tree
Showing 10 changed files with 3,554 additions and 4,999 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.{js,jsx,ts,tsx,py,go}]
charset = utf-8
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules
/lib

*.swp

coverage
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo: false
language: node_js
node_js:
- '8'
cache:
directories:
- node_modules
install:
- yarn install
script:
- yarn run coveralls
46 changes: 40 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![Build Status](https://travis-ci.com/adam-hanna/goal-seek.svg?branch=develop)](https://travis-ci.com/adam-hanna/goal-seek)
[![Coverage Status](https://coveralls.io/repos/github/adam-hanna/goal-seek/badge.svg?branch=develop)](https://coveralls.io/github/adam-hanna/goal-seek?branch=develop)

# goal-seek

goal-seek is a javascript library that can be used to solve for the value of an independent variable: "x"; of a function: "f(x)"; such that f(x) equals some defined goal. In other words: do you know the desired output of a function but not the input to yield such an output? If so, then use this goal seek!
Expand All @@ -17,11 +20,13 @@ The package exports two error types, the function parameter type and one functio
```typescript
export const IsNanError = TypeError('resulted in NaN');
export const FailedToConvergeError = Error('failed to converge');
export const InvalidInputsError = Error('invalid inputs');

export type Params = {
fn: (...inputs: any[]) => number;
fnParams: any[];
percentTolerance: number;
percentTolerance?: number;
customToleranceFn?: (arg0: number) => boolean;
maxIterations: number;
maxStep: number;
goal: number;
Expand All @@ -47,11 +52,12 @@ The `goalSeek` function takes one object argument with the following keys:

1. `fn` - the function, "f(x)" that is being evaluated.
2. `fnParams` - an array of parameters that are to be used as inputs to `fn`.
3. `percentTolerance` - the acceptable error range to the stated goal. For example, if `goal: 100` and `percentTolerance: 1`, then any values in the range [99, 101] will be accepted as correct (± 1% of 100).
4. `maxIterations` - the maximum number of attempts to make.
5. `maxStep` - the maximum magnitude step size to move the independent variable `x` for the next guess.
6. `goal` - the desired output of the `fn`.
7. `independentVariableIdx` - the index position of the independent variable `x` in the `fnParams` array.
3. `percentTolerance` - the acceptable error range to the stated goal. For example, if `goal: 100` and `percentTolerance: 1`, then any values in the range [99, 101] will be accepted as correct (± 1% of 100). If used, `customToleranceFn` should be left undefined.
4. `customToleranceFn` - a custom function that can be used to check the validity of a result. If used, `percentTolerance` should be left undefined.
5. `maxIterations` - the maximum number of attempts to make.
6. `maxStep` - the maximum magnitude step size to move the independent variable `x` for the next guess.
7. `goal` - the desired output of the `fn`.
8. `independentVariableIdx` - the index position of the independent variable `x` in the `fnParams` array.

To use the function, for example, with a simple linear equeation:

Expand Down Expand Up @@ -114,6 +120,34 @@ try {
console.log('error', e);
}

// result: 7
```

```javascript
import goalSeek from 'goal-seek';

const fn = (x,y,z) => x * y * z;
const fnParams = [4,5,6];
const customToleranceFn = (x: number): boolean => {
return x < 1
}

try {
const result = goalSeek({
fn,
fnParams,
customToleranceFn,
maxIterations: 1000,
maxStep: 0.01,
goal: 0.5,
independentVariableIdx: 2
});

console.log(`result: ${result}`);
} catch (e) {
console.log('error', e);
}

// result: 7
```

Expand Down
1 change: 1 addition & 0 deletions dist/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f655d89

Please sign in to comment.