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

Problem 1: Recursive Branching Consider a Tribonacci sequence Ti, T2, T3, where

ID: 3751627 • Letter: P

Question

Problem 1: Recursive Branching Consider a Tribonacci sequence Ti, T2, T3, where the sequence is initialized with Ti-1, T2-1, and T3-2, and the nth Tribonacci number is determined by Tn Tn-2 Tn-3 for n-4, 5, 6,...etc. Mathematically this can be written as a piecewise function T(n -1) + T(n -2) + T(n - 3) n>.3 Write a recursive function Tn - tribNum (n) that takes an integer number n (1xl double greater than 0) and computes the nh Tribonacci number Tn and assigns it to a variable called Tn (1xl double). You can assume that n is always a positive integer. TEST CASE 1 >Tn-tribNum (1) Tn - TEST CASE 2 >Tn-tribNum (2) TEST CASE 3 >>Tn-tribNum (3) 2 TEST CASE 4 >> Tn = tribNum(4) Tn TEST CASE 5 >> Tn = tribNum(5) Tn = TEST CASE 6 > Tn-tribNum (20) Tn = 66012

Explanation / Answer

-------------------------tribNum.m--------------------

function [Tn] = tribNum(n)

    % T(1) = 1

    if n == 1

       

        Tn = 1;

       

    % T(2) = 1

    elseif n == 2

       

        Tn = 1;

       

    % T(3) = 2

    elseif n == 3

       

        Tn = 2;

      

    % T(1) = T(n-1) + T(n-2) + T(n-3)

    else

       

        Tn = tribNum( n - 1 ) + tribNum( n - 2 ) + tribNum( n - 3 );

       

    end

end

-----------------------main.m----------------------

Tn = tribNum(1)
Tn = tribNum(2)
Tn = tribNum(3)
Tn = tribNum(5)
Tn = tribNum(20)

Sample Output

Tn =

1


Tn =

1


Tn =

2


Tn =

7


Tn =

66012