Output argument not assigned during call
조회 수: 7 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Greg
2018년 2월 4일
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
Greg
2018년 2월 4일
Depending on the code that calls bisection (specifically what it does with the outputs), I would just use x = NaN before the loop.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!