Skip to content

Instantly share code, notes, and snippets.

@ilsubyeega
Created May 1, 2022 16:16
Show Gist options
  • Select an option

  • Save ilsubyeega/9a8419a41a20120050e5380b3e1e05b7 to your computer and use it in GitHub Desktop.

Select an option

Save ilsubyeega/9a8419a41a20120050e5380b3e1e05b7 to your computer and use it in GitHub Desktop.
const original = (x) => (x - 1) * (x - 2) * (x - 3) * (x - 4)
const h = 1e-8
const prime = (f => x => (f(x + h) - f(x)) / h)(original)
let max = 100
let max_x = -1
for (let i = 0; i <= 10; i += 0.00005) {
const res = Math.abs(prime(i))
if (res < max) {
max = res
max_x = i
}
if (res.toFixed(3) === "0.000") console.log(i, res.toFixed(8))
}
// x, f(x), f'(x)
console.log(max_x.toFixed(3), original(max_x).toFixed(3), max.toFixed(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment