Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\\(F_{0} = 0\\) ; \\(F_{1} = 1\\) ; \\(F_{n+2} = F_{n+1} +F_{n} \\) ; n >=0. 1)

ID: 1861914 • Letter: #

Question

     (F_{0} = 0)      ;      (F_{1} = 1)     ;      (F_{n+2} = F_{n+1} +F_{n} )     ;     n >=0.

1) Write a MATLAB program to calcute the      (F_{n})      given n (n should be the input to your program, and the return should be      (F_{n})     ). 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      (F_{n})      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      (2^{32}-1)      ?)

Please help me with this, I am very new to this Matlab. If you can, please show all your work. I will give you all the point. I really appreciate it.

Explanation / Answer

> Need to create a while loop for the Fibonacci Sequence. Need to input nth limit and need to input first and second numbers. I have the for loop to calculate this but I'm having a hard time with the while loop. Thanks in advance:
>
> %% Problem 9.6: Fibonacci Sequence
> clc
> clear
> max =input('Enter the total number of elements: ');
> n=zeros(max,1);
> n(1)=input('Enter the first number: ');
> n(2)=input('Enter the second number: ');
> for i=3:max
> n(i)=n(i)+n(i-1)+n(i-2)
> end

Since you wrote some code here, I'll point out a problem
or two.

Naming a variable max is surely going to cause you
problems. Don't name variables with the same name as
existing and terribly useful functions. MATLAB then gets
confused, and then does not know if you want to use
variable max or function max.

And this line is wrong.

> n(i)=n(i)+n(i-1)+n(i-2)

It should logically be

n(i)=n(i-1)+n(i-2)

And, don't forget the semi-colon at the end there, or
it will spew lots of junk to the screen.