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

In Matlab: Tribonacci numbers are the numbers in a sequence in which the first t

ID: 3808482 • Letter: I

Question

In Matlab:

Tribonacci numbers are the numbers in a sequence in which the first three elements are 0, 1, and 1, and the value of each subsequent element is the sum of the previous three elements: 0, 1, 1, 2, 4, 7, 13, 24, ...

Write a user defined function to determine the nth Tribonacci number. Name the function Tribonacci(). The function takes an integer n 1 and returns its corresponding Tribonacci number. For example, the function must return 0 for n=1, 1 for n=2 and 13 for n=7 and so on.

The function must display an error message if the number entered was either less than 1 or not an integer.

The function must have a help section that shows what it exactly does, its input and its output.

Use the function to determine the Tribonacci number for n = 0, 2.5, 3, 10, 20, and 34.

Explanation / Answer

% Compute the Tribonacci number n
% input number n
% output tribonacci number of n
function tbn = Tribonacci(n)
if n < 1
disp("Errr: Number should be greater than equal to 1")
return
end
  
if floor(n)!=n
disp("Error: Number can not be non integer")
return
end
  
if n == 1
tbn = 0
return
end
  
if n == 2
tbn = 1
return
end
  
if n == 3
tbn = 1
return
end
  
prev_prev_prev = 0
prev_prev = 1
prev = 1
tbn = 0
for i = 4:n
tbn = prev + prev_prev + prev_prev_prev
prev_prev_prev = prev_prev
prev_prev = prev
prev = tbn
end
end

Arr = [0;2;3;10;20;34]
for i = 1:6
disp(Tribonacci(Arr(i)))
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