diff options
author | David Walter Seikel | 2014-04-21 20:59:39 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-04-21 20:59:39 +1000 |
commit | 9621add2918cc4943e6693b74ae85d51dd264fcf (patch) | |
tree | fff1edf2c69d7a08a0e12885eecc9b96ed847a6a /LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua | |
parent | LuaSL_test's window doesn't need to be so huge. (diff) | |
download | SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.zip SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.tar.gz SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.tar.bz2 SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.tar.xz |
We don't need the testlua directory any more.
Diffstat (limited to 'LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua')
-rw-r--r-- | LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua | 27 |
1 files changed, 0 insertions, 27 deletions
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua deleted file mode 100644 index f91e69b..0000000 --- a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.1/bisect.lua +++ /dev/null | |||
@@ -1,27 +0,0 @@ | |||
1 | -- bisection method for solving non-linear equations | ||
2 | |||
3 | delta=1e-6 -- tolerance | ||
4 | |||
5 | function 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 | ||
12 | end | ||
13 | |||
14 | -- find root of f in the inverval [a,b]. needs f(a)*f(b)<0 | ||
15 | function 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))) | ||
19 | end | ||
20 | |||
21 | -- our function | ||
22 | function f(x) | ||
23 | return x*x*x-x-1 | ||
24 | end | ||
25 | |||
26 | -- find zero in [1,2] | ||
27 | solve(f,1,2) | ||