Skip to content

Instantly share code, notes, and snippets.

@glmdev
Created October 3, 2018 00:44
Show Gist options
  • Save glmdev/3677459165dab0c4f677bfc03033359d to your computer and use it in GitHub Desktop.
Save glmdev/3677459165dab0c4f677bfc03033359d to your computer and use it in GitHub Desktop.
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