Created
October 3, 2018 00:44
-
-
Save glmdev/3677459165dab0c4f677bfc03033359d to your computer and use it in GitHub Desktop.
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
const newton = (x0, fn, derivative) => { | |
const estimates = [x0] | |
let next, last | |
while ( estimates.slice(-1).pop() > 0.09 ){ | |
last = estimates.slice(-1)[0] | |
next = last - (fn(last)/derivative(last)) | |
estimates.push(next) | |
} | |
return estimates.slice(-1)[0] | |
} | |
const f = (x) => { | |
return Math.pow( Math.E, ( -1/( Math.pow(x,2) ) ) ) | |
} | |
const df = (x) => { | |
return ( 1/Math.pow(x,3) )*( 2*( Math.pow( Math.E, ( -1/Math.pow(x,2) ) ) ) ) | |
} | |
console.log(newton(0.1, f, df)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment