Tribonacci numbers are the numbers in a sequence in which the first three elemen
ID: 2081441 • Letter: T
Question
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 n^th Tribonacci number. Name the function Tribonacci(). The function takes an integer n greaterthanequalto 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
matlab Script:
Tribonacci.m
function y = Tribonacci(n)
sum = 0;
tri = [0 1 1 zeros(1,n-3)];
if n<1 || rem(n,1)~=0
disp('invalid entry');
elseif n==1
disp(0);
elseif n==2
disp(1);
elseif n==3
disp(1);
else
idx = 3;
for i=4:n
for j=1:idx
tri(idx+1) = tri(idx+1)+tri(j);
end
idx = idx+1;
end
y = tri(n);
end
command window ouput:
>> clear all
>> Tribonacci(0)
invalid entry
>> Tribonacci(2.5)
invalid entry
>> Tribonacci(3)
1
>> Tribonacci(10)
ans =
128
>> Tribonacci(20)
ans =
131072
>> Tribonacci(34)
ans =
2.1475e+009
>>
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.