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的依賴性,這樣我終於設法使算法工作。 –