0

我想要做的是生成一系列向量来模拟非重组三叉树的结构。这是我的代码:

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

这是节点功能:

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

我进行了各种测试,但总是以同样的问题结束。node.m 函数仅在时间 t=2 编辑行向量的前 3 个条目,但将其他 6 个条目排除在外。在我看来,我在循环中犯了一个错误,以至于它在时间 t=1 时没有采用向量的下一个值。

这也可能只是我把问题复杂化了,而我忽略了一个更容易和明显的解决方案。

任何帮助将不胜感激。

4

1 回答 1

0

对于你有的j循环。Nodej = 1:((3^T)-2):3

当 时T = 2,这相当于j = 1:7:3。因此j将永远只有一个值1。(下一个值是 8,大于 3,因此循环停止。)

于 2015-07-21T13:09:23.043 回答