フィルターのクリア

Output argument not assigned during call

5 ビュー (過去 30 日間)
Andres Gomez
Andres Gomez 2018 年 2 月 4 日
コメント済み: Greg 2018 年 2 月 4 日
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

回答 (1 件)

Greg
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 件のコメント
Andres Gomez
Andres Gomez 2018 年 2 月 4 日
Thanks for the answer! I just noticed that and ended up changing the while loop to "for i:n" and specifying 'n' in the function input. However, I still want the function to run until the range of values is less than 0.1 apart (hence why I had the while loop at first). Should I have to add an 'else' for when f(l) - f(r) < 0.1?
Greg
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 ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by