Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Roots-of-a-function/Elixir/roots-of-a-function.elixir
Normal file
27
Task/Roots-of-a-function/Elixir/roots-of-a-function.elixir
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
defmodule RC do
|
||||
def find_roots(f, range, step \\ 0.001) do
|
||||
first .. last = range
|
||||
max = last + step / 2
|
||||
Stream.iterate(first, &(&1 + step))
|
||||
|> Stream.take_while(&(&1 < max))
|
||||
|> Enum.reduce(sign(first), fn x,sn ->
|
||||
value = f.(x)
|
||||
cond do
|
||||
abs(value) < step / 100 ->
|
||||
IO.puts "Root found at #{x}"
|
||||
0
|
||||
sign(value) == -sn ->
|
||||
IO.puts "Root found between #{x-step} and #{x}"
|
||||
-sn
|
||||
true -> sign(value)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp sign(x) when x>0, do: 1
|
||||
defp sign(x) when x<0, do: -1
|
||||
defp sign(0) , do: 0
|
||||
end
|
||||
|
||||
f = fn x -> x*x*x - 3*x*x + 2*x end
|
||||
RC.find_roots(f, -1..3)
|
||||
Loading…
Add table
Add a link
Reference in a new issue