I need help with this trivial function.

7 ビュー (過去 30 日間)
Abner Andrade
Abner Andrade 2013 年 8 月 1 日
I'm trying to run this script but I keep getting this error mensage:
??? Input argument "x" is undefined.
Error in ==> fibonacci at 8
Could anybody help me? Here is the code:
function y = fibonacci(x)
if x == 0
y = 0
elseif x == 1
y = 1
else
y = fibonacci(x-1) + fibonacci(x-2)
end
  2 件のコメント
Roger Stafford
Roger Stafford 2013 年 8 月 1 日
It should be pointed out that evaluating the Fibonacci series using this kind of recursion is particularly inefficient. The number of recursive calls on the function for an argument value of x would be even greater than the corresponding Fibonacci value at x, which is to say that it increases exponentially in x.
Jan
Jan 2013 年 8 月 1 日
@Roger: Exactly. And this is the cause, why Fibonacci is such a famous example for learning how to apply recursive programming and why to avoid it. See also: http://www.mathworks.com/matlabcentral/answers/2346-hump-day-challenger-recursion

サインインしてコメントする。

採用された回答

Dishant Arora
Dishant Arora 2013 年 8 月 1 日
編集済み: Dishant Arora 2013 年 8 月 1 日
This isn't a script, it's a function. You need to pass input argument while calling the function.
x = 4 ;
y = fibonacci(4) or fibonacci(x);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by