Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Roots-of-a-function/JavaScript/roots-of-a-function.js
Normal file
30
Task/Roots-of-a-function/JavaScript/roots-of-a-function.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// This function notation is sorta new, but useful here
|
||||
// Part of the EcmaScript 6 Draft
|
||||
// developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope
|
||||
var poly = (x => x*x*x - 3*x*x + 2*x);
|
||||
|
||||
function sign(x) {
|
||||
return (x < 0.0) ? -1 : (x > 0.0) ? 1 : 0;
|
||||
}
|
||||
|
||||
function printRoots(f, lowerBound, upperBound, step) {
|
||||
var x = lowerBound, ox = x,
|
||||
y = f(x), oy = y,
|
||||
s = sign(y), os = s;
|
||||
|
||||
for (; x <= upperBound ; x += step) {
|
||||
s = sign(y = f(x));
|
||||
if (s == 0) {
|
||||
console.log(x);
|
||||
}
|
||||
else if (s != os) {
|
||||
var dx = x - ox;
|
||||
var dy = y - oy;
|
||||
var cx = x - dx * (y / dy);
|
||||
console.log("~" + cx);
|
||||
}
|
||||
ox = x; oy = y; os = s;
|
||||
}
|
||||
}
|
||||
|
||||
printRoots(poly, -1.0, 4, 0.002);
|
||||
Loading…
Add table
Add a link
Reference in a new issue