I have a code for finding the bisection (and it finally works!), but I need to include 3 more things:
- output- Root History a vector containing the sequence of midpoints obtained by the algorithm
- output- the absolute value of the function
-
f(x) at r, i.e., fRoot = f(r) input- max iterations
function [R, E] = myBisection(f, a, b, tol) m = (a + b)/2; R = m; E = abs(f(m)); while E(end) > tol if sign(f(a)) == sign(f(m)) a = m; else b = m; end m = (a + b)/2; R = [R, m]; E = [E, abs(f(m))]; end
how do I do this? thanks!!
I have corrected indents an you can see that you’ve left out
endfrom the end of the function. (it is optional but best not to leave those things out so you know you did not mean to write couple lines to the end but you forgot it.)RandEshould be returned now, if you call myBisection apropriately, that isIf you just call
it will only return
R.To add a limit on the number of iterations, you change
while‘s condition like so:or it is better to do it in a for loop, with an
ifplusbreak: