2 -- bisection method for solving non-linear equations
4 function bisect(f
,a
,b
,fa
,fb
)
5 write(n
," a=",a
," fa=",fa
," b=",b
," fb=",fb
,"\n")
7 if abs(a
-b
)<delta
then return c
end
10 if fa
*fc
<0 then return bisect(f
,a
,c
,fa
,fc
) else return bisect(f
,c
,b
,fc
,fb
) end
13 -- find root of f in the inverval [a,b]. bisection needs that f(a)*f(b)<0
15 delta
=1e-6 -- tolerance
17 local z
=bisect(f
,a
,b
,f(a
),f(b
))
18 write(format("after %d steps, root is %.10g\n",n
,z
))