tests/fail/demo_bisect.lua

Wed, 24 Nov 2010 02:52:37 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 24 Nov 2010 02:52:37 +0000
changeset 138
f9bb0e212d28
parent 57
3e148db7f933
permissions
-rw-r--r--

Replace error reporting code with calls to LVM.traceback()

57
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- bisect.lua
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- bisection method for solving non-linear equations
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 delta=1e-6 -- tolerance
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 function bisect(f,a,b,fa,fb)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local c=(a+b)/2
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 io.write(n," c=",c," a=",a," b=",b,"\n")
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if c==a or c==b or math.abs(a-b)<delta then return c,b-a end
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 n=n+1
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local fc=f(c)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 -- find root of f in the inverval [a,b]. needs f(a)*f(b)<0
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 function solve(f,a,b)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 n=0
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local z,e=bisect(f,a,b,f(a),f(b))
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 io.write(string.format("after %d steps, root is %.17g with error %.1e, f=%.1e\n",n,z,e,f(z)))
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 -- our function
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 function f(x)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return x*x*x-x-1
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 -- find zero in [1,2]
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 solve(f,1,2)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29

mercurial