Skip to content

Commit f8dc85c

Browse files
Add an algorithm to find Taylor series approximation of exponential function
1 parent ede60b8 commit f8dc85c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Maths/ExponentialFunction.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let powerOfX = 1
2+
let factorial = 1
3+
/**
4+
* @function exponentialFunction
5+
* @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n
6+
* @param {Integer} power
7+
* @param {Integer} order - 1
8+
* @returns exponentialFunction(18,4) = 18.4
9+
* @url https://en.wikipedia.org/wiki/Exponential_function
10+
*/
11+
function exponentialFunction (power, n) {
12+
if (n === 0) { return 1 }
13+
const recursion = exponentialFunction(power, n - 1)
14+
powerOfX = powerOfX * power
15+
factorial = factorial * n
16+
return recursion + powerOfX / factorial
17+
}
18+
19+
export {
20+
exponentialFunction
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { exponentialFunction } from '../ExponentialFunction'
2+
3+
describe('exponentialFunction', () => {
4+
it('with a power of 5 and order of 21', () => {
5+
const ex = exponentialFunction(5, 20)
6+
expect(ex).toBe(148.4131470673818)
7+
})
8+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy