@@ -16,42 +16,43 @@ function convert(f: ((x: number) => number) | string, x: number): number
1616 * Things that may cause this to be unsuccessfull/error:
1717 * #1: Root is not within given range.
1818 * #2: Function changes signs more than once within given range.
19- * #3: The given minimum is greater than the given maximum.
20- * #4: There are x within given range where f(x) is undefined.
19+ * #3: There are x within given range where f(x) is undefined.
2120 *
2221 * @param {(x: number) => any } f - function you want to find the root of.
2322 * @param {number } min - min of given range.
2423 * @param {number } max - max of given range.
2524 * @param {bool } annoyingConsoleLogs - (optional) really annoying console log ever iteration.
26- * @param {number } maxIter - (optional) max number of iterations
25+ * @param {number } maxIter - (optional) max number of iterations.
26+ * @param {number } epsilon - (optional) tolerance for finding root.
2727 */
28- export function bisection ( f : ( ( x : number ) => number ) | string , min : number , max : number , maxIter = 100 , annoyingConsoleLogs = false ) : number | [ number , number ] | 'ERROR'
28+ export function bisection ( f : ( ( x : number ) => number ) | string , min : number , max : number , maxIter = 100 , annoyingConsoleLogs = false , epsilon = 0 ) : number | [ number , number ] | 'ERROR'
2929{
3030 console . log ( "---------------------------------------------------------------" ) ;
3131
3232 if ( min > max )
3333 {
34- console . log ( "You seem to have mixed the min and max..." ) ;
35- return 'ERROR' ;
34+ let tmp = max ;
35+ max = min
36+ min = tmp ;
3637 }
37- else if ( min == max && convert ( f , min ) == 0 )
38+ if ( min == max && convert ( f , min ) == 0 )
3839 {
39- console . log ( "Wow, the given min and max were the same and were the root. Kinda seems like this was on purpose.. ." ) ;
40+ console . log ( "Min= max= root." ) ;
4041 return min ;
4142 }
4243 else if ( min == max )
4344 {
44- console . log ( "Wow, the given min and max were the same but were not the root. Kinda seems like this was on purpose.. ." ) ;
45+ console . log ( "Min= max≠ root." ) ;
4546 return 'ERROR' ;
4647 }
4748 else if ( convert ( f , min ) == 0 )
4849 {
49- console . log ( "Wow, the lower bound of the given range was the root. Kinda seems like this was on purpose.. ." ) ;
50+ console . log ( "Min= root." ) ;
5051 return min ;
5152 }
5253 else if ( convert ( f , max ) == 0 )
5354 {
54- console . log ( "Wow, the upper bound of the given range was the root. Kinda seems like this was on purpose.. ." ) ;
55+ console . log ( "Max= root." ) ;
5556 return max ;
5657 }
5758
@@ -84,9 +85,9 @@ export function bisection(f: ((x: number) => number) | string, min: number, max:
8485 }
8586 max = guess ;
8687 }
87- else
88+ else if ( Math . abs ( convert ( f , guess ) - 0 ) <= epsilon )
8889 {
89- console . log ( `Root: ${ guess } .\nIterations it took: ${ iter } .` ) ;
90+ console . log ( `Root: ${ guess } .\nIterations it took: ${ iter } .` ) ;
9091 return guess ;
9192 }
9293 if ( annoyingConsoleLogs )
0 commit comments