0
什么,我试图做的是要产生一系列模拟非重组三叉树结构的载体。这里是我的代码:在MATLAB生成三叉树
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时获得下一个向量值。
它也可能只是我难以置信的过分复杂的问题,同时还有我忽略了一个更容易和明显的解决方案。
任何帮助将不胜感激。
非常感谢菲尔!我在那里肯定犯了一个错误。它遗憾地没有立即解决我的问题,但你的提示使我能够证明j对k的依赖性,这样我终于设法使算法工作。 –