How does one do Algebra in Lua?
snippet in lua

How does one do Algebra in Lua?

user3466

local function find_zero(f, x_left, x_right, eps)
   eps = eps or 0.0000000001   -- precision
   local f_left, f_right = f(x_left), f(x_right)
   assert(x_left <= x_right and f_left * f_right <= 0, "Wrong diapazone")
   while x_right - x_left > eps do
      local x_middle = (x_left + x_right) / 2
      local f_middle = f(x_middle)
      if f_middle * f_left > 0 then
         x_left, f_left = x_middle, f_middle
      else
         x_right, f_right = x_middle, f_middle
      end
   end
   return (x_left + x_right) / 2
end


local function my_func(x)
   return 200/(x+x^2+x^3+x^4+x^5) - 0.00001001
end

-- Assuming that the root is between 1 and 1000
local x = find_zero(my_func, 1.0, 1000.0)
print(x)       -->  28.643931367544