Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab Fibonacci problem

  1. Oct 18, 2018 #1
    1. The problem statement, all variables and given/known data
    Modify the function so that instead of returning all n numbers, it only returns the nth number.

    2. Relevant equations

    3. The attempt at a solution
    I'm not sure how to return only the nth number of the fibonacci sequence. Please help.

    Attached Files:

  2. jcsd
  3. Oct 18, 2018 #2


    Staff: Mentor

    If I gave you a sequence of any random set of numbers, could you return only the Nth one?
  4. Oct 18, 2018 #3
    I know how to return a number in a nth row / nth column matrices/vectors but I'm confused where to start.
  5. Oct 18, 2018 #4
    For example, I know that c = A(2,2:2:end); sets c equal to columns {2,4,6,8,10} of row 2 of A
    and that c=A(2,2) sets c equal to the number that is in row 2 column 2 of A.
  6. Oct 19, 2018 #5


    User Avatar

    Staff: Mentor

    If you only are required to return the nth number I don't see why you'd bother to store them all in an array; you only need the previous two to generate the next one. You should be able to do this with a small set of scalar variables and a loop.
  7. Oct 19, 2018 #6
    So you're familar with END. Are you aware that f(end) is the last element of vector f?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?