递归函数生成/打印斐波那契数列

时间:2021-11-12 21:53:51

I am trying to create a recursive function call method that would print the Fibonacci until a specific location:

我正在尝试创建一个递归函数调用方法,它将打印斐波那契直到一个特定的位置:

1 function f = fibonacci(n)
2 fprintf('The value is %d\n', n)
3 if (n==1)
4     f(1) = 1;
5     return;
6 elseif (n == 2)
7     f(2) = 2;
8 else
9     f(n) = fibonacci(n-1) + fibonacci(n-2);   
10 end
11 end

As per my understanding the fibonacci function would be called recursively until value of argument n passed to it is 1. Then the function stack would rollback accordingly. So when I call this function from command:

按照我的理解,斐波那契函数将被递归调用,直到参数n的值为1。然后函数堆栈相应地回滚。当我从命令调用这个函数时

>> fibonacci(4)

The value of n is 4, so line 9 would execute like:

n的值是4,因此第9行执行如下:

9 f(4) = fibonacci(3) + fibonacci(2);

Now I believe that that first fibonacci(3) would be called - hence again for fibonacci(3)

现在我相信第一个fibonacci(3)会被调用——因此,对于fibonacci(3)

9 if(3) = fibonacci(2) + fibonacci(1);

The ifs in line number 3 and 6 would take care.

第3行和第6行中的ifs会注意。

But now how fibonacci(2) + fibonacci(1) statement would change to:

但是现在fibonacci(2) + fibonacci(1)语句将如何变为:

 if(3) = 2 + 1;

I am receiving the below error and unable to debug further to resolve it:

我收到以下错误,无法进一步调试解决:

>> fibonacci(4)
The value is 4
The value is 3
The value is 2
The value is 1
In an assignment  A(I) = B, the number of elements in B and I must be the same.

Error in fibonacci (line 9)
    f(n) = fibonacci(n-1) + fibonacci(n-2);

Error in fibonacci (line 9)
    f(n) = fibonacci(n-1) + fibonacci(n-2);

Please provide some insight for the solution and with which parameter would fibonacci function be recursively called at line number 9 first and consequently.

请为解决方案提供一些见解,以及在第9行递归地调用斐波那契函数的参数。

Ex For n = 4

例n = 4

f(n) = fibonacci(3) + fibonacci(2);

So will MATLAB call fibonacci(3) or fibonacci(2) first?

那么MATLAB会先调用fibonacci(3)还是fibonacci(2) ?

Shouldn't the code be some thing like below:

代码不应该是如下所示:

1 function f = fibonacci(n)
2 fprintf('The valus is %d\n', n)
3 if (n==1)
4     f(1) = 1;
5     return f(1);
6 elseif (n == 2)
7     f(2) = 2;
8    return f(2);
9 else
10   f(n) = fibonacci(n-1) + fibonacci(n-2);   
11 end
12 end

fibonacci(4) Error: File: fibonacci.m Line: 5 Column: 12 Unexpected MATLAB expression.

斐波纳契(4)错误:文件:斐波那契。m行:5列:12个意想不到的MATLAB表达式。

Why return expression in a function is resulting in an error?

为什么函数中的返回表达式会导致错误?

3 个解决方案

#1


2  

Try this:

试试这个:

 function f = fibonacci(n)
 if (n==1)
     f= 1;
 elseif (n == 2)
     f = 2;
 else
     f = fibonacci(n-1) + fibonacci(n-2);   
 end

Note that this is also a recursion (that only evaluates each n once):

注意,这也是一个递归(只计算每个n一次):

function f=fibonacci(n)
  f=additive(n,1,2);

function a=additive(n,x0,x1)
  if(n==1)
    a=x0;
  else 
    if(n==2)
      a=x1;
    else 
      a=additive(n-1,x1,x0+x1);
    end
  end

#2


1  

If you HAVE to use recursive approach, try this -

如果必须使用递归方法,请尝试以下方法。

function out = fibonacci(n)
fprintf('The valus is %d\n', n)

if (n==1)
    out = 1;
    return;
elseif (n == 2)
    out = 2;
    return;
else
    out = fibonacci(n-1) + fibonacci(n-2);
end

return;

Unlike C/C++, in MATLAB with 'return', one can't return a value, but only the control goes back to the calling function. The output to be returned to the calling function is to be stored in the output variable that is defined at the start of the function.

与C/ c++不同,在MATLAB中有“return”,不能返回一个值,但只有控件返回到调用函数。要返回给调用函数的输出将存储在函数开头定义的输出变量中。

EDIT 1: For the entire fibonacci series and which assumes that the series starts from 1, use this -

编辑1:对于整个斐波那契数列,假设数列从1开始,使用这个-

N = 16; %// Number of fibonacci numbers needed

all_nums = zeros(1,N);
all_nums(1) = 1;
for k = 2:N
    all_nums(k) = fibonacci(k-1);
end

Gives -

给了,

1     1     2     3     5     8    13    21    34    55    89   144   233   377   610   987

#3


0  

Create a M-file for fibonacci function and write code as given below

为fibonacci函数创建一个m文件并编写如下所示的代码

function [ result ] = fibonacci( n )

if n==0|n==1
    result = n;

else
    result = fibonacci(n-2)+fibonacci(n-1);
end
end

Write following code in command window of matlab

在matlab的命令窗口中编写下列代码

for n = 0:10
    fprintf('Fibonacci(%d)= %d\n', n, fibonacci(n));
    end

Output :-

输出:

Fibonacci(0)= 0
Fibonacci(1)= 1
Fibonacci(2)= 1
Fibonacci(3)= 2
Fibonacci(4)= 3
Fibonacci(5)= 5
Fibonacci(6)= 8
Fibonacci(7)= 13
Fibonacci(8)= 21
Fibonacci(9)= 34
Fibonacci(10)= 55

#1


2  

Try this:

试试这个:

 function f = fibonacci(n)
 if (n==1)
     f= 1;
 elseif (n == 2)
     f = 2;
 else
     f = fibonacci(n-1) + fibonacci(n-2);   
 end

Note that this is also a recursion (that only evaluates each n once):

注意,这也是一个递归(只计算每个n一次):

function f=fibonacci(n)
  f=additive(n,1,2);

function a=additive(n,x0,x1)
  if(n==1)
    a=x0;
  else 
    if(n==2)
      a=x1;
    else 
      a=additive(n-1,x1,x0+x1);
    end
  end

#2


1  

If you HAVE to use recursive approach, try this -

如果必须使用递归方法,请尝试以下方法。

function out = fibonacci(n)
fprintf('The valus is %d\n', n)

if (n==1)
    out = 1;
    return;
elseif (n == 2)
    out = 2;
    return;
else
    out = fibonacci(n-1) + fibonacci(n-2);
end

return;

Unlike C/C++, in MATLAB with 'return', one can't return a value, but only the control goes back to the calling function. The output to be returned to the calling function is to be stored in the output variable that is defined at the start of the function.

与C/ c++不同,在MATLAB中有“return”,不能返回一个值,但只有控件返回到调用函数。要返回给调用函数的输出将存储在函数开头定义的输出变量中。

EDIT 1: For the entire fibonacci series and which assumes that the series starts from 1, use this -

编辑1:对于整个斐波那契数列,假设数列从1开始,使用这个-

N = 16; %// Number of fibonacci numbers needed

all_nums = zeros(1,N);
all_nums(1) = 1;
for k = 2:N
    all_nums(k) = fibonacci(k-1);
end

Gives -

给了,

1     1     2     3     5     8    13    21    34    55    89   144   233   377   610   987

#3


0  

Create a M-file for fibonacci function and write code as given below

为fibonacci函数创建一个m文件并编写如下所示的代码

function [ result ] = fibonacci( n )

if n==0|n==1
    result = n;

else
    result = fibonacci(n-2)+fibonacci(n-1);
end
end

Write following code in command window of matlab

在matlab的命令窗口中编写下列代码

for n = 0:10
    fprintf('Fibonacci(%d)= %d\n', n, fibonacci(n));
    end

Output :-

输出:

Fibonacci(0)= 0
Fibonacci(1)= 1
Fibonacci(2)= 1
Fibonacci(3)= 2
Fibonacci(4)= 3
Fibonacci(5)= 5
Fibonacci(6)= 8
Fibonacci(7)= 13
Fibonacci(8)= 21
Fibonacci(9)= 34
Fibonacci(10)= 55