1

I wrote a function that displays the Fibonacci sequence up to the nth term. The code runs fine, but I want to make two changes and am not sure how to do so.

Here is my code:

function [ F ] = get_fib( k )
    F(1) = 1;
    F(2) = 1;
    i = 3;

    while k >= i;
        F(i) = F(i-1) + F(i-2);
        i = i + 1;
    end
end

The first problem is that the code does not accept 0 as an input. I tried changing the function to:

function [ F ] = get_fib( k )
    F(0) = 0;
    F(1) = 1;
    F(2) = 1;
    i = 3;

    while k >= i;
        F(i) = F(i-1) + F(i-2);
        i = i + 1;
    end
end

But the following error appears:

Attempted to access F(0); index must be a positive integer or logical.

Error in get_fib (line 2)
F(0) = 0;

I would also like the code to display the last term in the sequence, rather than the entire sequence.

I changed the function to:

function [ F ] = get_fib( k );
    F(1) = 1;
    F(2) = 1;
    i = 3;

    while k >= i;
        F(i) = F(i-1) + F(i-2);
        i = i + 1;
    end

    term = F(k)
end

but the sequence it still being assigned to ans.

How can I make my function accept 0 as an argument and only display the last term of the sequence?

rgajrawala
  • 2,148
  • 1
  • 22
  • 35
rubisco_
  • 63
  • 1
  • 7

3 Answers3

0

First let's get your function to output just the last item in the sequence. You're setting term like this:

term = F(k);

Which is good (notice I added the ; at the end, though). But the return value from your function is F. You need to change it to term.

function [ term ] = get_fib( k )
     %//   ^^^^- change this    ^-- semicolon not necessary here

Now, to handle an input of 0, you can add a special check for zero:

function [ term ] = get_fib( k )
   if k == 0
      term = [];
      return;
   end

   while k >= i
       %//     ^-- semicolon not needed here, either
   <the rest of your code>

The semicolons after the function header and the while statement don't hurt anything, they just represent empty statements. But they might be misleading, so it's best to remove them.

The semicolon after the assignment to term prevents the ans = ... line from printing out to the console.

beaker
  • 16,331
  • 3
  • 32
  • 49
0

To address your first problem, F(0) is not a valid call. This is because MATLAB indexing starts from 1. In other words, the first element of a matrix is index 1. There is no 0th index of a matrix in MATLAB. See here for why MATLAB indexing starts from 1. What I would recommend to address this is to shift your output array by one index.

Thus, your code for the function should be:

function [ F ] = get_fib( k )
k = k + 1
F(1) = 0;    % 0th Fibonacci term
F(2) = 1;    % 1st Fibonacci term
F(3) = 1;    % 2nd Fibonacci term
i = 4;

while k >= i
    F(i) = F(i-1) + F(i-2);
    i = i + 1;
end
   term = F(k)
end

To address your second problem, it would depend on what you want. Do you (1) want the last term in the sequence to be returned when you call term = get_fib(k)or (2) want the last term of the sequence to be displayed and the entire sequence to be returned?

To achieve (1), fix the top line of your code to function term = F(k). To achieve (2), call the function with F = get_fib(some_number), as @rayryeng stated.

Community
  • 1
  • 1
Takeshi H.
  • 143
  • 5
0

Since others already pointed out how to fix your code, I'd like to show you one approach to calculate the nth Fibonacci number without relying on the F(n-1) and F(n-2) terms calculation.

It involves the golden ratio, and you can read more about its relationship to the Fibonacci sequence here.

function [ F ] = get_fib( n )
    % Changed your input variable from k to n (standard notation)    
    Phi = (1+sqrt(5))/2; % Golden ratio value
    F = round((Phi^n - ((-1)^n)/(Phi^n))/sqrt(5)); %nth fibonacci number

end

Since you are only interested in the last value of the sequence, it could speed up the calculation for large values of n.

Note i have rounded the output (F) to avoid floating point arithmetic errors.

brodoll
  • 1,851
  • 5
  • 22
  • 25