What is wrong with my bisection method code?

26 views (last 30 days)
Can someone help me find what's wrong with my MatLab code for the bisection method?
This is my code in MatLab:
function [z] = bisect1(a,b,fopg1,tol)
a = 0;
b = 2;
tol = 1e-8;
if (f(a)*f(b) > 0)
error ('invalid choice of interval')
end
r=0;
n=0;
while ((b-a)/2 > 0)
n = n + 1;
r = (a+b)/2;
if (f(r) == 0)
break;
elseif (f(a)*f9=(r) < 0)
b = r ;
else
a = r;
end
end
fopg1 is te function defined in another tab, where
y = exp(2x) - 3x -4
If I run the programme, it displays:
"Undefined function or variable 'a'.
What does this mean?
Thanks!

Accepted Answer

James Tursa
James Tursa on 23 Feb 2018
Edited: James Tursa on 23 Feb 2018
You don't show us how you are calling this function, so it is hard to advise you what to correct for that.
For the code itself, some comments:
This line gives fopg1 as an input, but your code uses f:
function [z] = bisect1(a,b,fopg1,tol)
So maybe change this line to:
function [z] = bisect1(a,b,f,tol)
I assume the following line is a typo:
elseif (f(a)*f9=(r) < 0)
and should be this instead:
elseif (f(a)*f(r) < 0)
Your function returns a z value but you never set z in your code. Maybe you meant to use r as the return value? E.g.,
function [r] = bisect1(a,b,f,tol)
Finally, you never make use of the tol input. You should have some type of check to see when you meet the tolerance and then exit the function. This will avoid unnecessary calculations and also avoid a potential infinite loop. E.g., if you were to use your code to try to find the value of pi by looking for the root of sin(x) near 3, your current code would end up in an infinite loop. That's because sin(pi) will not be exactly 0 (because of floating point effects), and the (a+b)/2 calculation will eventually degrade and return just a again and you will end up repeating that last iteration over and over again, never quitting and never meeting your current exit criteria. In addition to a tol check, maybe check to see if n gets too large and exit on that condition as well (maybe with an error).
  2 Comments
Jan
Jan on 24 Feb 2018
Edited: Jan on 24 Feb 2018
+1. James' explanations help. It is important to point out, that tol is missing in the code yet and while (b-a)/2 > 0 is not a good method to stop a bisection method.
The problem was the calling of the function. Then it is much more useful to explain, how a function is called with input arguments, than to convert the function to a script - which is still not working correctly due to the missing tol.

Sign in to comment.

More Answers (1)

John BG
John BG on 23 Feb 2018
Hi Microwave97
Now your code doesn't show syntax errors:
f=@(x) exp(2*x) - 3*x -4 % use your support function instead
a = 0;b = 2;tol = 1e-8;
if (f(a)*f(b) > 0)
error ('invalid choice of interval')
end
r=0; n=0;
while ((b-a)/2 > 0)
n = n + 1;
r = (a+b)/2;
if (f(r) == 0)
break;
elseif (f(a)*f(r) <= 0)
b = r ;
else
a = r;
end
end
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG
  3 Comments
John BG
John BG on 24 Feb 2018
nothing more nothing else than a syntax correction, that is all that was needed.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!