MATLAB Help: The Fibonacci Sequence is a series of integers that starts with 0 a
ID: 3725329 • Letter: M
Question
MATLAB Help:
The Fibonacci Sequence is a series of integers that starts with 0 and 1. The next value in the sequence is the sum of the two proceeding values. The sequence is infinitely long. Write a MATLAB script that asks the user for 2 integers, M and N, with M>0 and N>M. You need to validate the values provided by the user, assuming they only enter numbers.
The program will then create the Fibonnaci sequence with the said method, and display each of the Mth through Nth values of the sequence in a nicely formatted output. When displaying the values, place the phrase "(prime)" nextto the value if the the value is a prime number. Use the built in function isprime().
DO NOT hardcode any values of the sequence except for the two starting values, 0 and 1.
Example:
----------------------
Enter an integer thats greater than 0: (1)
Enter another integer > 1: (10)
Seq Location Fibonnaci Value
1 0
2 1
3 1
4 2 (prime)
----------------------------------
Explanation / Answer
%MATLAB program for printing fibonacci series.
f(1) = 0;
f(2) = 1;
c=char(10);
x = 'Enter an integer thats greater than 0:';
m = input(x);
disp(c);
n = input('Enter another integer >');
disp(m);
if m>n
'Enter valid inputs'
else
disp(f(1));
disp(f(2));
for i = m : n
f(i) = f(i-1) + f(i-2);
str = [num2str(f(i))];
disp(str);
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.