-- bisection method for solving non-linear equations12delta=1e-6 -- tolerance34function bisect(f,a,b,fa,fb)5local c=(a+b)/26io.write(n," c=",c," a=",a," b=",b,"\n")7if c==a or c==b or math.abs(a-b)<delta then return c,b-a end8n=n+19local fc=f(c)10if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end11end1213-- find root of f in the inverval [a,b]. needs f(a)*f(b)<014function solve(f,a,b)15n=016local z,e=bisect(f,a,b,f(a),f(b))17io.write(string.format("after %d steps, root is %.17g with error %.1e, f=%.1e\n",n,z,e,f(z)))18end1920-- our function21function f(x)22return x*x*x-x-123end2425-- find zero in [1,2]26solve(f,1,2)272829