summaryrefslogtreecommitdiff
path: root/test/bisect.lua
blob: cfb1bbfdfa0144092e3ceda6cd2eedf787a52082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$debug
-- bisection method for solving non-linear equations

function bisect(f,a,b,fa,fb)
print(n.." a="..a.." fa="..fa.." b="..b.." fb="..fb)
 local c=(a+b)/2
 if abs(a-b)<delta then return c end
 n=n+1
 local fc=f(c)
 if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end
end

-- find root of f in the inverval [a,b]. bisection needs that f(a)*f(b)<0
function solve(f,a,b)
 delta=1e-6	-- tolerance
 n=0
 local z=bisect(f,a,b,f(a),f(b))
 print(format("after %d steps, root is %.10g",n,z))
end

-- our function
function f(x)
 return x*x*x-x-1
end

solve(f,1,2)