Question

MATLAB1. The Fibonacci sequence is defined by the recurrence relation Fn = Fn-1+Fn-2 where Fo = 0 and F1 = 1. Hence F2 = 1, F3 = 2,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

MATLAB code:

% measuring the time taken by each of function for n = 1 to 25

timerec = zeros(1 , 25);

timeloop = zeros(1 , 25);

timeEqn = zeros(1 , 25);

for n = 1 : 25

tic; fibrec(n);

timerec(n) = toc;

tic; fibloop(n);

timeloop(n) = toc;

tic; fibEqn(n);

timeEqn(n) = toc;

end

% Plotting the graph

plot([1:25] , timerec , [1:25] , timeloop , [1:25] , timeEqn);

xlabel('n')

ylabel('Average time')

legend('timerec' , 'timeloop' , 'timeEqn');

% Writing recursive code to calculate fibonacci number

function nelem = fibrec(n)

if(n == 0)

nelem = 0;

elseif(n == 1)

nelem = 1;

else

nelem = fibrec(n - 1) + fibrec(n - 2);

end

end

% Writing iterative code

function nelem = fibloop(n)

n_second_prev = 0;

n_prev = 1;

for i = 2 : n

nelem = n_prev + n_second_prev;

n_second_prev = n_prev;

n_prev = nelem;

end

end

% Writing closed form function

function nelem = fibEqn(N)

r = (1 + sqrt(5)) / 2;

nelem = round(((r^N) - ((1 - r) ^ N)) / sqrt(5));

end

1 2 3 4 5 6 % measuring the time taken by each of function for n = 1 to 25 timerec = zeros(1, 25); time loop = zeros(1, 25);

22 23 24 25 26 27 28 % Writing recursive code to calculate fibonacci number function nelem = fibrec(n) if(n == 0) nelem = 0;

OUTPUT:

X10-3 4.5 4 timerec timeloop timeEqn 3.5 3 2.5 Average time 2 1.5 1 0.5 0 5 10 15 20 25

Add a comment
Know the answer?
Add Answer to:
MATLAB 1. The Fibonacci sequence is defined by the recurrence relation Fn = Fn-1+Fn-2 where Fo...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. The sequence (Fn) of Fibonacci numbers is defined by the recursive relation Fn+2 Fn+1+ F...

    3. The sequence (Fn) of Fibonacci numbers is defined by the recursive relation Fn+2 Fn+1+ F for all n E N and with Fi = F2= 1. to find a recursive relation for the sequence of ratios (a) Use the recursive relation for (F) Fn+ Fn an Hint: Divide by Fn+1 N (b) Show by induction that an 1 for all n (c) Given that the limit l = lim,0 an exists (so you do not need to prove that...

  • this is using MATLAB 2. Fibonacci sequence: A Fibonacci sequence is composed of elements created by...

    this is using MATLAB 2. 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 called FL_fib_seq' (where F and L are your first and last initials) that creates a Fibonacci sequence. There should be three inputs. The first...

  • Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the...

    Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the linear recurrence equation Fn F-1 F-2 where F F2 1 and by convention Fo 0. For example, the first 8 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21. (a) For a given n, compute the nth Fibonnaci number using a for loop (b) For a given n, compute the nth Fibonnaci number using a while loop Print the 15th Fibonacci number...

  • (5) Fibonacci sequences in groups. The Fibonacci numbers F, are defined recursively by Fo = 0,...

    (5) Fibonacci sequences in groups. The Fibonacci numbers F, are defined recursively by Fo = 0, Fi-1, and Fn Fn-1 + Fn-2 for n > 2. The definition of this sequence only depends on a binary operation. Since every group comes with a binary operation, we can define Fibonacc type sequences in any group. Let G be a group, and define the sequence (n in G as follows: Let ao, ai be elements of G, and define fo-ao fa and...

  • Consider the sequence of functions fn : [0,1| R where each fn is defined to be...

    Consider the sequence of functions fn : [0,1| R where each fn is defined to be the unique piecewise linear function with domain [0, 1] whose graph passes through the points (0,0) (, n), (j,0), and (1,0) (a) Sketch the graphs of fi, f2, and f3. (b) Computefn(x) dx. (Hint: Compute the area under the graph of any fn) (c) Find a function f : [0, 1] -> R such that fn -* f pointwise, i.e. the pointwise limit of...

  • Consider the sequence of functions fn : [0,1| R where each fn is defined to be the unique piecewise linear function wit...

    Consider the sequence of functions fn : [0,1| R where each fn is defined to be the unique piecewise linear function with domain [0, 1] whose graph passes through the points (0,0) (, n), (j,0), and (1,0) (a) Sketch the graphs of fi, f2, and f3. (b) Computefn(x) dx. (Hint: Compute the area under the graph of any fn) (c) Find a function f : [0, 1] -> R such that fn -* f pointwise, i.e. the pointwise limit of...

  • Fibonacci sequence: Cauchy-Binet formula Let (Fn)n be the Fibo- nacci sequence defined recursively by F1 =...

    Fibonacci sequence: Cauchy-Binet formula Let (Fn)n be the Fibo- nacci sequence defined recursively by F1 = F2 = 1 and Fn = Fn−1 + Fn−2In this way it all reduces to computing a high power of a 2 × 2 matrix. How can you compute an arbitrary power of a matrix and can you come up with the Cauchy-Binet formula from here?

  • turn the following if function in to C++ % Define variables: % fn -- Fibonacci number...

    turn the following if function in to C++ % Define variables: % fn -- Fibonacci number % n -- The item in the sequence to calculate % Get n n = input('Enter the Fibonacci number n to evaluate (n>2): '); % Check to see that n is an integer greater than two if n <= 2 disp('Error--n must greater than two!'); elseif round(n) ~= n disp('Error--n must be an integer!'); else % Calculate fn fn = zeros(1,n); fn(1) = 1;...

  • Question 1. A linear homogeneous recurrence relation of degree 2 with constant coefficients is a recurrence...

    Question 1. A linear homogeneous recurrence relation of degree 2 with constant coefficients is a recurrence relation of the form an = Cian-1 + c2an-2, for real constants Ci and C2, and all n 2. Show that if an = r" for some constant r, then r must satisfy the characteristic equation, p2 - cir= c = 0. Question 2. Given a linear homogeneous recurrence relation of degree 2 with constant coefficients, the solutions of its characteristic equation are called...

  • Fibonacci num Fn are defined as follow. F0 is 1, F1 is 1, and Fi+2 =...

    Fibonacci num Fn are defined as follow. F0 is 1, F1 is 1, and Fi+2 = Fi + Fi+1, where i = 0, 1, 2, . . . . In other words, each number is the sum of the previous two numbers. Write a recursive function definition in C++ that has one parameter n of type int and that returns the n-th Fibonacci number. You can call this function inside the main function to print the Fibonacci numbers. Sample Input...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT