我正在结合和重写一些系统动力学模型(即股票和流量模型)和基于代理的模型在网络徽标。
系统动力学模型通常包括描述非线性关系的表函数.如果我们有两个连续变量x和y,并且我们知道一个近似关系是(x = 0,y= 100;x= 1,y= 50;x= 2,y= 45,x= 3,y= 40;x= 4,y= 35),我们可以使用插值方法来找到任意x值的y值。例如,在R中,你可以使用appoxfun()函数。
在没有真正了解因果机制并在模型中编码的情况下,是否有办法在Netlogo中解决这些关系?我在网上搜索过资源,但没能找到多少。
谢谢你的帮忙!
发布于 2018-05-24 11:23:14
没有现有的功能。但是,构造一个可以调用的插值函数非常简单。此版本通过报告适当的结束y值来进行线性插值,并处理指定的x值范围之外的值。在列表函数方面比我更好的人(如精简)可能会发现一个更优雅的方法。
to-report calc-piecewise [#xval #xList #yList]
if not (length #xList = length #ylist)
[ report "ERROR: mismatched points"
]
if #xval <= first #xList [ report first #yList ]
if #xval >= last #xList [ report last #yList ]
; iterate through x values to find first that is larger than input x
let ii 0
while [item ii #xlist <= #xval] [ set ii ii + 1 ]
; get the xy values bracketing the input x
let xlow item (ii - 1) #xlist
let xhigh item ii #xlist
let ylow item (ii - 1) #ylist
let yhigh item ii #ylist
; interpolate
report ylow + ( (#xval - xlow) / (xhigh - xlow) ) * ( yhigh - ylow )
end
to testme
let xs [0 1 2 3 4]
let ys [100 50 30 20 15]
print calc-piecewise 1.5 xs ys
end
此版本要求您每次使用x值列表和y值列表(以及插值的x值)。如果您总是要使用相同的点,则可以在函数中指定它们。
https://stackoverflow.com/questions/50506275
复制相似问题