我之前从未使用过Matlab,而且我正在尝试将这部分代码转换为Python,但我不确定我是否真的理解发生了什么。这是代码:
% Calculate the price at all interior nodes
offsetConstants = [aj(1); cj(end)];
for i = N:-1:1
price(2:end-1,i) = A*price(2:end-1,i+1);
% Offset the first and last terms
price([2 end-1],i) = price([2 end-1],i) + ...
offsetConstants.*price([1 end],i+1);
到目前为止,我一直在使用http://mathesaurus.sourceforge.net/matlab-numpy.html来完成它,但我仍然在某些部分丢失了。继承人到目前为止我所拥有的
OffsetConstants正在创建包含来自aj和cj
的条目的矩阵For循环是在除了第1行和最后一行之外的所有值上的价格,然后乘以A,然后在第一个和最后一个术语中加回。
有人能更好地向我解释这个吗?
答案 0 :(得分:0)
offsetConstants = [aj(1); cj(end)];
创建2个术语的向量offsetConstants
。
一个是aj
中的第一个词,另一个是cj
中的最后一个词。你完全正确的。
for i = N:-1:1
反向样式看起来像是要反向传播price
中的列。
但要注意范围。 i+1
上升到N+1
,我猜price
矩阵有很多列。
price(2:end-1,i) = A*price(2:end-1,i+1);
从专栏N+1
开始,从第一个和最后一个字词开始,取整个列,然后乘以A
,A
为标量或(N+1)x(N+1)
矩阵。
然后,您将产品存储到列N
中。
price([2 end-1],i) = price([2 end-1],i) + ...
offsetConstants.*price([1 end],i+1);
最后,您将第N+1
列的第一个和最后一个字段,{@ 1}}逐个元素地加倍,然后将添加到其中第二个和第二个条款。
重复此过程,直到第2列并重新放入第1列。
注意:offsetConstants
相当于price([2 end-1],i)
,而非[price(2,i),price(end-1,i)]
。