generate tribonacci sequence in matlab

To generate a Tribonacci sequence in MATLAB, you can use loops and arrays. The Tribonacci sequence is similar to the Fibonacci sequence, but instead of adding the previous two numbers, you add the previous three numbers.

Here's an example MATLAB function that generates a Tribonacci sequence:

main.m
function t = tribonacci(n)
% Generate the first n numbers of the Tribonacci sequence

t = zeros(1, n);

% Set the first three numbers of the sequence
t(1) = 0;
t(2) = 0;
t(3) = 1;

% Generate the rest of the sequence
for i = 4:n
    t(i) = t(i-1) + t(i-2) + t(i-3);
end
270 chars
15 lines

You can call this function with a number argument n to generate the first n numbers of the sequence, like this:

main.m
>> tribonacci(10)
ans =     0     0     1     1     2     4     7    13    24    44
84 chars
3 lines

This will generate the first 10 numbers of the Tribonacci sequence.

Note that the function initializes an array of zeros, and then sets the first three values to 0, 0, and 1. This is because the sequence starts with these three numbers. The loop then generates the rest of the sequence by adding the previous three numbers together.

gistlibby LogSnag