本文介绍了正在寻找ApproxFun的2D版本()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在R中寻找stats::approxfun()
的2D版本,它可以从(x,y,z)数据集生成内插函数f(x,y)。我在包akima
或其他地方找不到。
推荐答案
我会尝试重新调整R包fields
中的interp.surface
函数。改编自?interp.surface
的示例:
加载包
library(fields)
#> Warning: package 'fields' was built under R version 4.1.2
#> Loading required package: spam
#> Warning: package 'spam' was built under R version 4.1.2
#> Loading required package: dotCall64
#> Warning: package 'dotCall64' was built under R version 4.1.2
#> Loading required package: grid
#> Spam version 2.7-0 (2021-06-25) is loaded.
#> Type 'help( Spam)' or 'demo( spam)' for a short introduction
#> and overview of this package.
#> Help for individual functions is also obtained by adding the
#> suffix '.spam' to the function name, e.g. 'help( chol.spam)'.
#>
#> Attaching package: 'spam'
#> The following objects are masked from 'package:base':
#>
#> backsolve, forwardsolve
#> Loading required package: viridis
#> Loading required package: viridisLite
#>
#> Try help(fields) to get started.
加载数据
data(lennon)
obj <- list(x = 1:20, y = 1:20, z = lennon[201:220, 201:220])
定义近似函数
approxfun_2d <- function(x, y) {
interp.surface(obj, cbind(x, y))
}
测试
set.seed(1)
approxfun_2d(x = runif(1, min = 1, max = 20), y = runif(1, min = 1, max = 20))
#> [1] 33.34148
绘制原始数据
image.plot(obj)
计算内插
x_interp <- runif(2e2, 1, 20)
y_interp <- runif(2e2, 1, 20)
z_interp <- approxfun_2d(x_interp, y_interp)
曲线图近似
quilt.plot(x_interp, y_interp, z_interp)
在2021-11-23创建
这不是100%等同于approxfun
的2D版本,但对于大多数用途来说可能足够好。
这篇关于正在寻找ApproxFun的2D版本()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!