What is wrong with my bisection method code?

17 Ansichten (letzte 30 Tage)
Microwave97
Microwave97 am 23 Feb. 2018
Kommentiert: John BG am 24 Feb. 2018
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!

Akzeptierte Antwort

James Tursa
James Tursa am 23 Feb. 2018
Bearbeitet: James Tursa am 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 Kommentare
John BG
John BG am 24 Feb. 2018
the question reads
function defined in another tab, where
y = exp(2x) - 3x -4
by 'another tab' one may understand another MATLAB tab that may be a different .m file.
I simplified to defining that support function with anonymous, and then fix the syntax, which was causing the error originating the question.
John BG
Jan
Jan am 24 Feb. 2018
Bearbeitet: Jan am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

John BG
John BG am 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 Kommentare
Jan
Jan am 24 Feb. 2018
To be exact, he question was:
Can someone help me find what's wrong with my MatLab code for
the bisection method?
"Undefined function or variable 'a'.
What does this mean?
Converting the function to a script and using an anonymous function instead of a function does not concern these questions. Posting a version without syntax errors is not useful, because the code is still not a valid bisection method due to the missing application of tol.
John BG
John BG am 24 Feb. 2018
nothing more nothing else than a syntax correction, that is all that was needed.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by