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

Need help with MATLAB For Loops. Thank you The Fibonacci sequence is an integer

ID: 3937045 • Letter: N

Question

Need help with MATLAB For Loops. Thank you

The Fibonacci sequence is an integer sequence calculated by adding previous numbers together to calculate the next value. This is represented mathematically by saying that F_n = F_n-1| + F_n-2 (where F_n is the nth value in the sequence, F) or: 0 + 1= 1 + 1 = 1 + 2= 2 + 3= 3 + 5= 5 + 8= 8 + 13= F_n-1 + F_n-2 = 1 2 3 5 8 13 21 .... F_n Note this sequence starts with the underlined values (0, 1) and calculates the remaining values in the sequence based on the sum of the previous two values. Professor Bowman found this sequence to be extremely insufficient and created the Bowman sequence, which is an integer sequence calculated by adding the previous three numbers together (instead of two like in the Fibonacci sequence) to calculate the next value. This is represented mathematically by saying that F_n = F_n-1 + F_n-2 + F_n-3(where F_n is the nth value in the sequence, F) or: 0 + 1 + 2= 1 + 2+ 3= 2 + 3 + 6= 6+ 11+ 20= F_n-3 + F_n-2 + Fn-1 0 1 2 3 6 11 37 ... F_n Note this sequence starts with the underlined values (0, 1, 2) and calculates the remaining values in the sequence based on the sum of the previous three values. Write a MATLAB function that implements the Bowman sequence that accepts one input argument, the length of the desired Bowman sequence to generate, and returns one output variable, the Bowman sequence stored inside of an array. This function should also check to see if the number passed in to the function is a valid Bowman sequence length (think about what might constitute valid sequence lengths!). If the input is invalid, your function should display an error message and the output variable should contain only one number: -1. Otherwise if the input is valid, your function should calculate the Bowman sequence and display each value in the sequence in the Command Window. Sample Output: >> B=FUNCTIONNAME(8); Bowman sequence: 0 1 2 3 6 11 20 37

Explanation / Answer

function [ B ] = FUNCTIONNAME( length )
    if length < 1
        fprintf(' Invalid Input: Should be >= 1 ');
        B = [ -1 ];
    else
        if length == 1
            B = [0];
        elseif length == 2
            B = [0 1];
        elseif length == 3
            B = [0 1 2];
        else
            B = zeros(1,length);
            B(1,1)= 0; B(1,2)=1; B(1,3)=2;
            for i=4:length
                B(1,i) = B(1,i-1) + B(1,i-2) + B(1,i-3);
            end
        end
        fprintf(' Bowman sequence: ');
        for i=1:size(B,2)
            fprintf('%d   ',B(1,i));
        end
        fprintf(' ');
    end
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote