what I am trying to do is to generate a series of vectors that emulate the structure of a non-recombining trinomial tree. Here is my code:
function Trinomial_tree
S{1}(1) = 100;
w{1} = 1.4;
w{2} = 1.1;
w{3} = 0.7;
T = 2;
%Compiling the w's into a vector
w = [w{1}, w{2}, w{3}];
%Actual vector-content generation goes here, right now the k-allocation
%doesn't work as intended. In the second run with i=3, k seems to be
%fixed on 3
%{
for i = 2:(T+1)
S{i} = zeros(1, 3^(1i-1));
end
%}
for i = 2:(T+1)
S{i} = Node(w, T, i, S{i-1});
end
display(S{1})
display(S{2})
display(S{3})
end
Here is the node-function:
function [S] = Node(w, T, i, S_1)
%Compute the continuing node of a point
%Pre-allocation
S = zeros(1, 3^(i-1));
%Nested loop which generates the different nodes
for k = 1:(3^(i-2))
for j = 1:((3^T)-2):3
S(j) = S_1(k) * w(1);
S(j+1) = S_1(k) * w(2);
S(j+2) = S_1(k) * w(3);
end
end
I ran various test-trials but it always ends with the same problem. The node.m function only edits the first 3 entries of the row-vector at time t=2 but leaves the other 6 entries out. It seems to me that I made a mistake within the loop such that it doesn't take the next value of the vector at time t=1.
It might also just be me incredibly overcomplicating the problem while there is a much easier and obvious solution which I overlooked.
Any help would be greatly appreciated.
For the j
loop of Node
you have j = 1:((3^T)-2):3
.
When T = 2
, this is equivalent to j = 1:7:3
.
Hence j
will only ever have a value of 1
.
(The next value it would take is 8, which is greater than 3, and hence looping stops.)