Output argument not assigned during call
2 views (last 30 days)
Show older comments
I know this has been asked before, but I still can't figure out what's wrong. I'm not a coder and am only doing this because of a class I'm forced to take, so apologies in advance if I'm missing something obvious.
I have this on one script file:
function [x, e] = bisection(f, l, r)%, n)
% Inputs: f -- an inline function
% l,r -- left and right bounds of the interval
%
% Outputs: x -- the estimated solution of f(x) = 0
% e -- an upper bound on the error
format long
c = f(l); d = f(r);
if c*d > 0.0
error('Function has same sign at both endpoints')
end
disp('x, y')
%for i=1:n
while f(r) - f(l) > 0.1
x = (l + r)./2;
y = f(x);
disp([x, y])
if y == 0.0 % solved the equation exactly
e = 0;
break % jumps out of the for loop
end
if c*y < 0
r=x;
else
l=x;
end
end
e = (r-l)./2;
and then call it using the following
f = @(a) 402 + a.*cosh(215./a) - 757.7;
[x, e] = bisection(f, 1, 200);
but then get "Output argument "x" (and maybe others) not assigned during call to "bisection"."
any pointers?
edit: I got it to work by changing the while loop to a for i:n loop, and then adding the 'n' in as an input. However, I'm trying to keep the function going until the range of values is less than 0.1 apart, so I'm still not sure how to do that
0 Comments
Answers (1)
Greg
on 4 Feb 2018
The output variable x is only set (assigned a value) inside the while loop. What happens if your test condition ( f(r) - f(l) > 0.1 ) never evaluates true to enter the loop?
2 Comments
Greg
on 4 Feb 2018
Depending on the code that calls bisection (specifically what it does with the outputs), I would just use x = NaN before the loop.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!