In: Statistics and Probability
Write a for-loop in MATLAB that generates a list of numbers such that each number is the sum of the previous three. Initialize your list of numbers at the values of 0, 0 and 1. In other words, "0" is the first element of the list, "0" is the second element of the list, and "1" is the third element of the list. What is the 20th value in the list?
function x = sum_of_previous_three (x0,x1,x2)
x = zeros(1,20);
x(1) = x0 ;
x(2) = x1 ;
x(3) = x2 ;
for i = 4 : 20
x (i) = x(i-1) + x(i-2) + x(i-3);
end
x(20)
// this function takes the first three value as input
sum_of_previous_three(0,0,1)
ans =
19513
ans =
Columns 1 through 11
0 0 1 1 2 4 7 13 24 44 81
Columns 12 through 20
149 274 504 927 1705 3136 5768 10609 19513
20th number is 19513
Please rate