Asked by Steelierelk
on 22 Feb 2018

I am trying to use the bisection method to evaluate a function.

function [r,iter] = bisect(xl,xu,es,imax)%inputs are: left x value, right x value, stopping criterion, maximum iterations

iter = 0;

f=@(x)-25+82*x-90*x^(2)+44*x^(3)-8*x^(4)+0.7*x^(5);

while (abs(xu-xl)>=es) %runs a nonstop loop under specified conditions

xr = (xl+xu)/2; %midpoint of xl and xu values to approximate root

xrold = xr;

iter = iter+1;

if xr~=0

ea=abs((xr-xrold)/xr)*100; %calculate approximate error

end

test = feval(f,xl)*feval(f,xr); %test if functions have opposite signs

if test < 0

xu = xr;

elseif test > 0

xl = xr;

else

ea = 0;

end

if ea < es || iter >= imax %if approx. error is less than stopping criterion, or number of iterations is greater than or equal to maximum amount of iterations, stop loop

break

end

r = xr;

end

disp(r)

However, when I run the function with inputs, it returns "Undefined function or variable 'r'". As a test to see what was wrong, I evaluated the line

xr=(xl+xu)/2

it says that xl is undefined, even though I put in a value for it upon running the function through my command line. Can anyone help me figure out what's making my code respond with this, and what I should do to avoid this problem? Thank you.

Answer by Geoff Hayes
on 22 Feb 2018

Steelierelk - I suspect that the

"Undefined function or variable 'r'"

corresponds to the

disp(r)

line. If this is true, then this is because whatever inputs you have entered are such that the while loop condition

while (abs(xu-xl)>=es)

evaluates to false...and so the r variable never gets set. I would set a breakpoint at this line (the while condition) and verify that the values you have passed into this function make sense given the condition.

It is also good practice to assign default values to output variables so that at least something is assigned to them.

And, what do you mean by you evaluated the line xr=(xl+xu)/2. When did you evaluate this? After you had run the function (in which case the variables would be out of scope and so be undefined)? Or were you stepping through the code with the debugger?

Steelierelk
on 22 Feb 2018

Thank you for answering. I meant that I highlighted the line and evaluated the selection(not sure if that's considered debugging). I think you're spot on with the

disp(r)

being a problem. Could you provide an example of assigning default values to output variables? I'm not quite sure what you mean by that. I'm not too familiar with how breakpoints work. I'll have to do some reading on that. Thank you again

Stephen Cobeldick
on 22 Feb 2018

"I'm not too familiar with how breakpoints work. I'll have to do some reading on that."

"Could you provide an example of assigning default values to output variables? I'm not quite sure what you mean by that"

At the start of your function define r to be a suitable value, e.g.:

r = []

Then even if the while loop condition is not met (as you have now), there will still be an output defined. But this depends on whether you want to accept this case or not: maybe throwing an error makes more sense. Only you can decide this.

Sign in to comment.

Answer by Roger Stafford
on 22 Feb 2018

In your code you set xrold equal to xr and compute ea:

ea=abs((xr-xrold)/xr)*100;

which not surprisingly gives ea a zero value. This guarantees that your later test ea < es for es a positive number will produce a break on the first trip through the while loop. This means that the line r = xr never get executed and hence r is undefined.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Stephen Cobeldick (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/384103-undefined-function-or-variable-r-but-i-am-confident-i-defined-it#comment_538140

Sign in to comment.