So, I've written the following code for the bisection method, however it doesn't seem to be returning any values.
function [r, n] = bisection(a,b,f,tol)if (f(a)*f(b) > 0) error('Invalid choice of interval');endr = 0;n = 0;while ((b - a)/2 > tol) n = n + 1; r = (a + b)/2; if(f(r) == 0) break; elseif (f(a)*f(b) < 0) b = r; else a = r; endend
I'm using it with inputs of (0,1,q,0.5) I also tried tol as 0.1 too. q is some anonymous function that I defined, in this case it's q(x) = 3x – 1. Like, I type the command Bisection(0,1,q,0.5) and it executes without returning an error but doesn't return any numerical values whatsoever. Any ideas why this is?
Best Answer