Last active
September 16, 2015 01:36
-
-
Save gregtatum/d9b5fbd9c8dd9f67f056 to your computer and use it in GitHub Desktop.
Trig Functions Maclaurin Series
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| These are unoptimized recreations of the built-in trigonometric | |
| functions as defined by Maclaurin / Taylor series. | |
| */ | |
| function factorial( number ) { | |
| var product = Math.max(1,number) | |
| while( number-- > 1 ) { | |
| product *= number | |
| } | |
| return product | |
| } | |
| function sin( x, iterations ) { | |
| var sum = 0 | |
| for( var n=0; n < iterations; n++ ) { | |
| var numerator = Math.pow(-1, n) * Math.pow(x, 2 * n + 1 ) | |
| var denominator = factorial( 2 * n + 1 ) | |
| sum += numerator / denominator | |
| } | |
| return sum | |
| } | |
| function cos( x, iterations ) { | |
| var sum = 0 | |
| for( var n=0; n < iterations; n++ ) { | |
| var numerator = Math.pow(-1, n) * Math.pow(x, 2 * n) | |
| var denominator = factorial( 2 * n ) | |
| sum += numerator / denominator | |
| } | |
| return sum | |
| } | |
| function tan( x, iterations ) { | |
| return sin( x, iterations ) / cos( x, iterations ) | |
| } | |
| function sec( x, iterations ) { | |
| return 1 / cos( x, iterations ) | |
| } | |
| function cot( x, iterations ) { | |
| return cos( x, iterations ) / sin( x, iterations ) | |
| } | |
| function csc( x, iterations ) { | |
| return 1 / sin( x, iterations ) | |
| } | |
| function eRaisedTo( x, iterations ) { | |
| var sum = 1 | |
| for( var n=1; n < iterations; n++ ) { | |
| sum += Math.pow(x, n) / factorial( n ) | |
| } | |
| return sum | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment