53.5k views
5 votes
Homework 8 Matlab Write a function called fibonacciMatrix. It should have three inputs, col1, col2, and n. col1 and col2 are vertical arrays of the same length, and n is an integer number greater than 2. It should return an output, fib, a matrix with n columns. The first two columns should be col1 and col2. For every subsequent column:

2 Answers

10 votes

Final answer:

The fibonacciMatrix function in MATLAB generates a matrix with n columns, where the first two columns are col1 and col2. The values in the subsequent columns are calculated based on the Fibonacci sequence.

Step-by-step explanation:

The fibonacciMatrix function is a MATLAB function that takes in three inputs: col1, col2, and n. These inputs are used to generate a matrix with n columns, where the first two columns are the values in col1 and col2. For every subsequent column, the values are calculated based on the Fibonacci sequence.

Here is an example implementation of the fibonacciMatrix function:

function fib = fibonacciMatrix(col1, col2, n)
fib = [col1, col2];
for i = 3:n
fib(:,i) = fib(:,i-1) + fib(:,i-2);
end
end

In this code, the Fibonacci sequence is generated by adding the previous two columns together to get the next column. The resulting matrix 'fib' has n columns, where each column represents a value in the Fibonacci sequence.

User Yazan Jaber
by
4.6k points
4 votes

Final answer:

To solve this problem, use a for loop to generate the Fibonacci sequence up to the desired number of columns, n. Start by creating an empty fib matrix with the first two columns as col1 and col2. Then, in each iteration of the loop, calculate the next column by summing the previous two columns. Finally, append the new column to the fib matrix.

Step-by-step explanation:

To solve this problem, you can use a for loop to generate the Fibonacci sequence up to the desired number of columns, n. You would start by creating an empty matrix, fib, with the first two columns as col1 and col2. Then, in each iteration of the loop, you would calculate the next column of the Fibonacci sequence by summing the previous two columns. Finally, you would append the new column to the fib matrix. Here's an example of how the code could look:

function fib = fibonacciMatrix(col1, col2, n)
fib = [col1(:), col2(:)];
for i = 3:n
next_col = fib(:, i-1) + fib(:, i-2);
fib = [fib, next_col];
end
end

With this code, the function fibonacciMatrix will return the fib matrix with n columns, where the first two columns are col1 and col2, and the subsequent columns are the next numbers in the Fibonacci sequence.

User Ankush Bist
by
4.6k points