Brent's algorithm to solve equations.
f | The function to be solved. |
y0 | Looking for x0 s.t. f(x0)=y0 |
a | The l.h.s of the bracket. |
b | The r.h.s. of the bracket. |
max_steps | (Optional, defaults to 100) The maximum number of function evaluations. |
tol_x | (Optional, defaults to 1e-15) Tolerance on x. |
tol_y | (Optional, defaults to 0, i.e. only equation terminates.) Tolerance on f(x)-y0. |
return value | A stucture {x:x, y:y, exit:n}, c.f. description. |
We are solving the equation f(x)=y0 for x. The function requires a bracket [a,b], i.e. $ f(a)<y0 $ and $ f(b)>y0 $, or v.v. The algorithm returns a stucture {x:x, y:y, exit:n}, where x is the approximate solution, y is the corresponding f(x), exit is 0 if one of the exit criteria is met, 1 if no approx. solution is found in max_steps (in this case, the last iteration point is still returned as approximate solution in (x,y)), and 2 if [a,b] is not a bracket (in this case the fields x and y are not filled).
solveBrent(@(x) x^3, 0, -2V, 1V, 123, 1e-10V, 1e-100V^3)