USING MATLAB 1. Fibonacci sequence A Fibonacci sequence is composed of elements
ID: 3607609 • Letter: U
Question
USING MATLAB
1. Fibonacci sequence
A Fibonacci sequence is composed of elements created by adding the two previous elements. The simplest Fibonacci sequence starts with 1,1 and proceeds as follows:
1, 1, 2, 3, 5, 8, 13, ...
However, a Fibonacci sequence can be created with any two starting numbers.
Create a MATLAB function that creates a Fibonacci sequence. There should be three inputs. The first and second number of the desired Fibonacci sequence and the total number of elements desired for the Fibonacci sequence.
For example, if you use your completed function to solve for the Fibonacci sequence where the first two numbers are 3 & 4 and your total number of elements is 5, your function should return:
[3, 4, 7, 11, 18]
Bonus:
Plot your results on a polar graph. Use the element number (the index) for the angle and value of the element in the sequence as the radius.
Explanation / Answer
% function that takes 3 parameters
function fib = fibo(x, y, n)
% declaring first 2 number of series
f(1) = x;
f(2) = y;
% looping for n times and printing number in series
% adding last two numbers for current number
for i = 1:n
disp(f(i))
f(i+2) = f(i+1) + f(i);
end
end
% taking user input and calling function
a = input("Enter first number in series: ");
b = input("Enter second number in series: ");
n = input('Enter the number of Fibonacci numbers you want: ');
fibo(a,b, n)
SAMPLE OUTPUT
Enter first number in series: 3
Enter second number in series: 4
Enter the number of Fibonacci numbers you want: 5
3
4
7
11
18
% function that takes 3 parameters
function fib = fibo(x, y, n)
% declaring first 2 number of series
f(1) = x;
f(2) = y;
% looping for n times and printing number in series
% adding last two numbers for current number
for i = 1:n
disp(f(i))
f(i+2) = f(i+1) + f(i);
end
end
% taking user input and calling function
a = input("Enter first number in series: ");
b = input("Enter second number in series: ");
n = input('Enter the number of Fibonacci numbers you want: ');
fibo(a,b, n)
SAMPLE OUTPUT
Enter first number in series: 3
Enter second number in series: 4
Enter the number of Fibonacci numbers you want: 5
3
4
7
11
18
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.