Modify Fibonacci Function to Return nth Number - Matlab Homework

In summary, the conversation discusses modifying a function to only return the nth number in a sequence, specifically the Fibonacci sequence. The concept of returning the nth number in a matrix or vector is also mentioned. The use of the END function and its ability to return the last element in a vector is brought up as well as using scalar variables and a loop to generate the sequence.
  • #1
Joon
85
2

Homework Statement


Modify the function so that instead of returning all n numbers, it only returns the nth number.

Homework Equations

The Attempt at a Solution


I'm not sure how to return only the nth number of the fibonacci sequence. Please help.
 

Attachments

  • fibonacci problem.png
    fibonacci problem.png
    6.4 KB · Views: 373
Physics news on Phys.org
  • #2
If I gave you a sequence of any random set of numbers, could you return only the Nth one?
 
  • #3
I know how to return a number in a nth row / nth column matrices/vectors but I'm confused where to start.
 
  • #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.
 
  • #5
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.
 
  • #6
Joon said:
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.

So you're familar with END. Are you aware that f(end) is the last element of vector f?
 

Related to Modify Fibonacci Function to Return nth Number - Matlab Homework

1. What is the Matlab Fibonacci problem?

The Matlab Fibonacci problem involves creating a program in Matlab to calculate and display the Fibonacci sequence. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding numbers, starting with 0 and 1.

2. How do I write a Matlab program to solve the Fibonacci problem?

To write a program in Matlab to solve the Fibonacci problem, you will need to use a loop to calculate each number in the sequence. You can use either a for loop or a while loop, and update the values with each iteration until you reach the desired number in the sequence.

3. What is the syntax for writing a loop in Matlab?

The syntax for a for loop in Matlab is:
for index = start_value : end_value
    statements
end
The syntax for a while loop in Matlab is:
while condition
    statements
end

4. How can I display the Fibonacci sequence in Matlab?

To display the Fibonacci sequence in Matlab, you can use the disp() function to print each number as it is calculated in the loop. Alternatively, you can store the numbers in an array and use the disp() function to print the entire array at the end of the program.

5. Can I use Matlab functions to solve the Fibonacci problem?

Yes, you can use pre-existing Matlab functions such as fibonaccifun() or fibonacciseries() to calculate the Fibonacci sequence. These functions are built-in to Matlab and can save you time and effort in writing your own code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
6K
  • Precalculus Mathematics Homework Help
Replies
11
Views
224
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
976
  • Engineering and Comp Sci Homework Help
Replies
22
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
Back
Top