diff options
author | Lua Team <team@lua.org> | 2010-05-18 12:00:00 +0000 |
---|---|---|
committer | repogen <> | 2010-05-18 12:00:00 +0000 |
commit | f970e1e83ed07bbcf8a20fc1a95f91a0a2aae620 (patch) | |
tree | 005b26e8ebf7553ba5c7a66700866be3e42443d0 /test/bisect.lua | |
parent | ecd48c2901f08a88db32139b97c35c59eba1f19e (diff) | |
download | lua-github-5.2.0-work3.tar.gz |
Lua 5.2.0-work35.2.0-work3
Diffstat (limited to 'test/bisect.lua')
-rw-r--r-- | test/bisect.lua | 27 |
1 files changed, 0 insertions, 27 deletions
diff --git a/test/bisect.lua b/test/bisect.lua deleted file mode 100644 index f91e69bf..00000000 --- a/test/bisect.lua +++ /dev/null @@ -1,27 +0,0 @@ --- bisection method for solving non-linear equations - -delta=1e-6 -- tolerance - -function bisect(f,a,b,fa,fb) - local c=(a+b)/2 - io.write(n," c=",c," a=",a," b=",b,"\n") - if c==a or c==b or math.abs(a-b)<delta then return c,b-a 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]. needs f(a)*f(b)<0 -function solve(f,a,b) - n=0 - local z,e=bisect(f,a,b,f(a),f(b)) - io.write(string.format("after %d steps, root is %.17g with error %.1e, f=%.1e\n",n,z,e,f(z))) -end - --- our function -function f(x) - return x*x*x-x-1 -end - --- find zero in [1,2] -solve(f,1,2) |