aboutsummaryrefslogtreecommitdiff
path: root/libs/lua/test/bisect.lua
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-05-16 10:58:21 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-11-01 09:52:45 (GMT)
commit3c1d7bb1c521e824dfa46903fd4014722d0f5a29 (patch)
treeb9d8ab1f95aa608cbef817550ccc4bd42534dcd6 /libs/lua/test/bisect.lua
parent2223c879bf41d2b4f40fa43db478ba1bce8523de (diff)
downloadfluxbox_paul-3c1d7bb1c521e824dfa46903fd4014722d0f5a29.zip
fluxbox_paul-3c1d7bb1c521e824dfa46903fd4014722d0f5a29.tar.bz2
Add lua as an internal library in libs/lua
Diffstat (limited to 'libs/lua/test/bisect.lua')
-rw-r--r--libs/lua/test/bisect.lua27
1 files changed, 27 insertions, 0 deletions
diff --git a/libs/lua/test/bisect.lua b/libs/lua/test/bisect.lua
new file mode 100644
index 0000000..f91e69b
--- /dev/null
+++ b/libs/lua/test/bisect.lua
@@ -0,0 +1,27 @@
1-- bisection method for solving non-linear equations
2
3delta=1e-6 -- tolerance
4
5function bisect(f,a,b,fa,fb)
6 local c=(a+b)/2
7 io.write(n," c=",c," a=",a," b=",b,"\n")
8 if c==a or c==b or math.abs(a-b)<delta then return c,b-a end
9 n=n+1
10 local fc=f(c)
11 if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end
12end
13
14-- find root of f in the inverval [a,b]. needs f(a)*f(b)<0
15function solve(f,a,b)
16 n=0
17 local z,e=bisect(f,a,b,f(a),f(b))
18 io.write(string.format("after %d steps, root is %.17g with error %.1e, f=%.1e\n",n,z,e,f(z)))
19end
20
21-- our function
22function f(x)
23 return x*x*x-x-1
24end
25
26-- find zero in [1,2]
27solve(f,1,2)