Please help me in this assignment. I need full steps. no steps means no rate. Th
ID: 3544599 • Letter: P
Question
Please help me in this assignment. I need full steps. no steps means no rate.
The purpose of this assignment is to write a MATLAB program to determine the nth number in the Fibonacci sequence. The sequence is defined formally as: F0 = 0; F1 = 1; Fn+2 = Fn+1 + Fn, n 0 This sequence was published in 1202 by Leonardo Pisano (Leonardo of Pisa), who is sometimes called Leonardo Fibonacci (Filius Bonaccii, son of Bonaccio). Your assignment is to write a MATLAB program to calcute the Fn given n (n should be the input to your program, and the return should be Fn). Your program should first inspect n to be sure it is greater than zero. You should also be mindful of how your program should behave if n is 0 or 1. By default MATLAB will use double precision for all numeric values unless you specify otherwise. For which n will Fn overflow? What does your program report as Fn in this case? Modify your program to force the use of unsigned 32-bit integers. You can do this by modifying n with n = uint32(n); F1 = uint32(0); F2 = uint32(1);. Any variables calculated using only unsigned 32-bit integer variables will become unsigned 32-bit integer variables themselves unless you specify otherwise. For which n will Fn overflow? What does your program report as Fn in this case? (Hint: What is 232 - 1?)Explanation / Answer
Program to calculate Fn
clc
clear
n = input('Enter n: ');
if n<=0
disp('n should be greater than zero')
else
F = zeros(1,n+1);
F(1) = 0;
F(2) = 1;
for i = 3:n+1
F(i) = F(i-1)+F(i-2);
end
end
a = sprintf('F(%d) = %d',n,F(n+1));
disp(a)
Question1:
clc
clear
F1 = 0;
F2 = 1;
n = 1;
while F2<realmax %realmax gives the largest positive number
F = F1+F2;
F1 = F2;
F2 = F;
n = n+1;
end
a = sprintf('Fn overflows at n = %d and in this case F(%d) = %d',n,n,F);
disp(a)
Question2:
clc
clear
F1 = uint32(0);
F2 = uint32(1);
n = 1;
while F2<uint32(realmax) %realmax gives the largest positive number
F = uint32(F1+F2);
F1 = uint32(F2);
F2 = uint32(F);
n = n+1;
end
a = sprintf('Fn overflows at n = %d and in this case F(%d) = %d',n,n,F);
disp(a)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.