Skip to content

Commit 8690451

Browse files
authored
Merge pull request #13 from micaelgoms/feature/numericMehotd
feat(NumericalMethods): bisection Method
2 parents 8fb87ee + 22f84ae commit 8690451

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

math/bisection.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--[[
2+
Lua implementation of bissetion method
3+
4+
Author: Micael Machado Gomes (micaellgoms@gmail.com)
5+
--]]
6+
7+
function f(x)
8+
local y = 2*x - math.sin(x) - 4
9+
return y
10+
end
11+
12+
-- defined random range
13+
a, b = 2, 3
14+
-- defined error to stop loop
15+
erro = 0.001
16+
-- defined Xn; slipt range;
17+
x = (a + b)/2
18+
19+
k = 1
20+
while math.abs(f(x)) >= erro and math.abs(b-a) >= erro do
21+
if f(a)*f(x) < 0 then
22+
b = x
23+
else
24+
a = x
25+
end
26+
27+
-- update Xn
28+
x = (a + b)/2
29+
k = k + 1
30+
end

0 commit comments

Comments
 (0)