MATLAB Code Please! Tribonacci numbers are the numbers in a sequence in which th
ID: 3812572 • Letter: M
Question
MATLAB Code Please!
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 greaterthanorequalto 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
Tribonacci.m
function [ f ] = Tribonacci( N )
if N<1
fprintf (' Input number is not positive ')
elseif mod(N,1)~=0
fprintf (' Input Number is not integer. ')
else
f(1)=0; f(2)=1; f(3)=1;
for k=1:N-3
f(k+3)=f(k)+f(k+1)+f(k+2);
end
fprintf('Tribonacci number for N=%d is : %d ',N,f(N))
end
Output :-
Input number is not positive
>> Tribonacci(0)
Input number is not positive
>> Tribonacci(2.3)
Input Number is not integer.
>> Tribonacci(3)
Tribonacci number for N=3 is : 1
>> Tribonacci(10)
Tribonacci number for N=10 is : 81
>> Tribonacci(20)
Tribonacci number for N=20 is : 35890
>> Tribonacci(34)
Tribonacci number for N=34 is : 181997601
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.