Table functions/interpolation 在 Netlogo 中
Table functions/interpolation in Netlogo
我正在用 Netlogo 中基于代理的模型组合和重写一些系统动力学模型(即存量和流量模型)。
系统动力学模型通常包括 table 用于描述非线性关系的函数。如果我们有两个连续变量 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 中解决这些关系?我在 Internet 资源上四处搜索,但没能找到多少。
感谢您的帮助!
不存在现有函数。但是,构造一个您可以调用的插值函数非常简单。此版本通过报告适当的结束 y 值进行线性插值并处理超出指定 x 值范围的值。在列表函数(例如reduce)方面比我更好的人可能会找到更优雅的方法。
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值)。如果您总是要使用相同的点,可以在函数内指定它们。
我正在用 Netlogo 中基于代理的模型组合和重写一些系统动力学模型(即存量和流量模型)。
系统动力学模型通常包括 table 用于描述非线性关系的函数。如果我们有两个连续变量 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 中解决这些关系?我在 Internet 资源上四处搜索,但没能找到多少。
感谢您的帮助!
不存在现有函数。但是,构造一个您可以调用的插值函数非常简单。此版本通过报告适当的结束 y 值进行线性插值并处理超出指定 x 值范围的值。在列表函数(例如reduce)方面比我更好的人可能会找到更优雅的方法。
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值)。如果您总是要使用相同的点,可以在函数内指定它们。