Last active
September 25, 2018 18:46
-
-
Save anthonykrivonos/2ab477fdf38f353085895b5bf86fe141 to your computer and use it in GitHub Desktop.
Euler Approximation Scheme
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
/* | |
* Euler Approximation Scheme Script | |
* Anthony Krivonos | |
* Prof. Kei Kobayashi | |
* 9/25/2018 | |
*/ | |
/* | |
* Sample Function | |
* - Takes in (t, y) and returns a number. | |
* - Container for a math function to approximate. | |
* @param t Independent variable. | |
* @param y Dependent variable. | |
* @returns A calculation of F(t, y). | |
*/ | |
let sampleFunction = (t, y) => { | |
return t + Math.sin(y); | |
}; | |
/* | |
* Euler Approximate | |
* - Takes in (t, y) and returns a number. | |
* - Container for a math function to approximate. | |
* @param func F(t, y) function. | |
* @param leftBoundT Left closed bound on t's domain. | |
* @param rightBoundT Right closed bound on t's domain. | |
* @param initialY Initial value satisfying y(leftBoundT). | |
* @param delta The step of the approximation. | |
* @returns An Euler approximation of F(t, y) within the provided bounds. | |
*/ | |
let eulerApproximate = (func, leftBoundT, rightBoundT, initialY, delta) => { | |
// Instantiate y(t) as IC. | |
var t = leftBoundT; | |
var y = initialY; | |
// Variable for precise calculation below. | |
const EPSILON = 10000000000; | |
while (t < rightBoundT) { | |
// y(t_(k + 1)) = F(t, y)(delta) + y(t_(k)) | |
y = func(t, y) * delta + y; | |
// Increment t with epsilon precision | |
t = Math.round((t + delta) * EPSILON)/EPSILON; | |
} | |
return y; | |
}; | |
// Tests | |
console.log(eulerApproximate(sampleFunction, 0, 1, Math.PI/2, 0.1)); // Returns 2.8262115870726854 | |
console.log(eulerApproximate(sampleFunction, 0, 1, Math.PI/2, 0.2)); // Returns 2.8143717120214893 | |
console.log(eulerApproximate(sampleFunction, 0, 1, Math.PI/2, 0.0000001)); // Returns 2.835764798002636 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment