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

USING MATLAB I will like to see the commands and outputs acc numbers are the num

ID: 3811993 • Letter: U

Question

USING MATLAB I will like to see the commands and outputs acc numbers are the numbers in a sequence in which the first three elements are 0,1, 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 integern 21and 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. Tribonacci number for n 0, 2.5, 3, 10, 20, Use the function to determine the and 34.

Explanation / Answer

%%Save as Tribonacci.m

%%The function Tribonacci that takes an integer value

%%then finds the tribonacci number . It display a error

%%message if num is <1 or decimal numbers

function result=Tribonacci(num)

%%checking for invalid number

if num<1 || num~=floor(num)

disp('invalid integer value')

elseif num==1

%%set result=0 for num=1

result=0 ;

elseif num==2

%%set result=1 for num=2

result=1;

elseif num==3

%%set result=1 for num=3

result=1;

else

%%finding tribonacci numbers

a=0;

b=1;

c=1;

d=a+b+c;

for i =4:num-1

a=b;

b=c;

c=d;

d=a+b+c;

end

result=d;

end

end

-------------------------------------------------------

Result output:

--> Tribonacci(-3)
invalid integer value
ans =
2
--> Tribonacci(3.5)
invalid integer value

--> Tribonacci(7)
ans =
13
--> Tribonacci(8)
ans =
24
--> Tribonacci(5)
ans =
4
--> Tribonacci(3)
ans =
1