Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,35 @@
|
|||
print_roots = (f, begin, end, step) ->
|
||||
# Print approximate roots of f between x=begin and x=end,
|
||||
# using sign changes as an indicator that a root has been
|
||||
# encountered.
|
||||
x = begin
|
||||
y = f(x)
|
||||
last_y = y
|
||||
|
||||
cross_x_axis = ->
|
||||
(last_y < 0 and y > 0) or (last_y > 0 and y < 0)
|
||||
|
||||
console.log '-----'
|
||||
while x <= end
|
||||
y = f(x)
|
||||
if y == 0
|
||||
console.log "Root found at", x
|
||||
else if cross_x_axis()
|
||||
console.log "Root found near", x
|
||||
x += step
|
||||
last_y = y
|
||||
|
||||
do ->
|
||||
# Smaller steps produce more accurate/precise results in general,
|
||||
# but for many functions we'll never get exact roots, either due
|
||||
# to imperfect binary representation or irrational roots.
|
||||
step = 1 / 256
|
||||
|
||||
f1 = (x) -> x*x*x - 3*x*x + 2*x
|
||||
print_roots f1, -1, 5, step
|
||||
f2 = (x) -> x*x - 4*x + 3
|
||||
print_roots f2, -1, 5, step
|
||||
f3 = (x) -> x - 1.5
|
||||
print_roots f3, 0, 4, step
|
||||
f4 = (x) -> x*x - 2
|
||||
print_roots f4, -2, 2, step
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
> coffee roots.coffee
|
||||
-----
|
||||
Root found at 0
|
||||
Root found at 1
|
||||
Root found at 2
|
||||
-----
|
||||
Root found at 1
|
||||
Root found at 3
|
||||
-----
|
||||
Root found at 1.5
|
||||
-----
|
||||
Root found near -1.4140625
|
||||
Root found near 1.41796875
|
||||
Loading…
Add table
Add a link
Reference in a new issue