F 0 = 0 ; F 1 = 1 ; F n + 2 = F n + 1 + F n ; n >=0. 1) Write a MATLAB program t
ID: 3544428 • Letter: F
Question
F0=0 ; F1=1 ; Fn+2=Fn+1+Fn ; n >=0.
1) 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.
2) 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?
3)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 them-selves 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
1)
clc;
close all ;
clear all ;
n= input('input the no.');
if n>2
for i=3:n+2;
F(1)=0;
F(2)=1;
F(i)=F(i-1)+F(i-2);
end
display(F);
elseif n==2
F(1)=0;
F(2)=1;
display(F);
elseif n==1
F(1)=0;
display(F);
else
display('error');
end
2)
it will not be overflowed if you will provide integers as we are doing in this case
3)
clc;
close all ;
clear all ;
n= input('input the no.');
n=uint32(n);
if n>2
for i=3:n+2;
F(1)=uint32(0);
F(2)=uint32(1);
F(i)=F(i-1)+F(i-2);
end
display(uint32(F));
elseif n==2
F(1)=uint32(0);
F(2)=uint32(1);
display(uint32(F));
elseif n==1
F(1)=uint32(0);
display(uint32(F));
else
display('error');
end
please rate it best answer
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.