简体   繁体   English

在R中使用样条曲线插值创建包络

[英]Create Envelope with Spline Interpolation in R

Hello I have tried spline interpolation in Matlab. 您好,我已经在Matlab中尝试了样条插值。 I have the following data: 我有以下数据:

N = 36

I also have data "max" 我也有数据“最大”

>  max
    1
    5
    7
    10
    12
    14
    16
    20
    24
    27
    31
    33
    35

And "hmax" 和“ hmax”

>  hmax
    157
    124
    207
    208
    170
    178
    163
    160
    146
    151
    160
    173
    172

Then I want to create envelope with spline interpolation with Matlab, the following code: 然后我想用Matlab的样条插值法创建信封,代码如下:

maxenv = spline(max,hmax,1:N);

That code will show result 该代码将显示结果

>  maxenv
    157
    86.564389
    67.53534827
    84.9886334
    124
    169.6452037
    207
    224.3964594
    223.1919113
    208
    185.4207867
    170
    172.1744995
    178
    172.7562154
    163
    158.1641553
    157.9081319
    159.4480425
    160
    157.5512022
    153.1731874
    148.7085789
    146
    146.3035305
    148.5290764
    151
    152.5114649
    153.7458399
    155.857295
    160
    166.578645
    173
    175.921355
    172
    157.893225

Now, I want create envelope with spline interpolation in R with same code: 现在,我想用相同的代码在R中用样条插值创建信封:

maxenv <- spline(max,hmax,n=36)

But I get different result with my code in Matlab. 但是我在Matlab中的代码得到了不同的结果。 How can I get the same result in R? 如何在R中获得相同的结果? Or "spline" on Matlab and R is different function? 还是Matlab和R上的“样条曲线”功能不同?

Thanks you very much 非常感谢你

In the spline function, n specifies the number of "equally spaced points spanning the interval [xmin, xmax]". spline函数中, n指定“跨越间隔[xmin,xmax]的等距点”的数量。 However, your Matlab points includes x=36 which is 1 beyond the largest input data point at x=35 so the R and Matlab results are not for same x values. 但是,您的Matlab点包含x=36 ,这比最大输入数据点x=35超出1,因此R和Matlab结果的x值不同。 For comparison, you may want to use xout = the sequence 1:36 to get most direct comparison. 为了进行比较,您可能需要使用xout =序列1:36来进行最直接的比较。 With this correction and using the default R spline method (Forsythe, Malcolm and Moler), the largest discrepancy is in the initial values which probably has to do with R and Matlab using slightly different methods for starting the spline interpolation. 通过这种校正并使用默认的R样条方法(Forsythe,Malcolm和Moler),最大的差异在于初始值,这可能与R和Matlab有关,它们使用略有不同的方法来启动样条插值。

EDITED 已编辑

xx <- 1:36
maxenvR <- data.frame(spline(max, hmax, xout=xx, method="fmm"))
maxenvRnat <-  data.frame(spline(max, hmax, xout=xx, method="natural"))
plot(xx, maxenv[1:length(xx)], type="l", col="black", ylab = "hmax")
lines(maxenvR, col="blue")
lines(maxenvRnat, col="green")
points(max, hmax, col="red", pch=16)
legend("bottomright", legend=c("Matlab spline", "R fmm spline", "R natural spline", "Data Points"), 
       text.col=c("black","blue","green", "red"), col=c("black","blue","green","red"), lty="solid")

Interpolating spline algorithms can differ by their treatment of the boundary conditions at the ends of the data. 插值样条算法的不同之处在于它们对数据末端的边界条件的处理。 For general spline interpolation, R spline allows the options of using either natural cubic splines which sets the second derivative the interpolating cubic splines to zero at each end of the data or the fmm method which fits cubic polynomials to the first four points at each end of the data and then connects them with the spline equations. 对于常规样条插值,R spline允许使用以下选项:使用natural三次样条(将数据的每一端的二阶导数,插值三次样条设置为零)或fmm方法(将三次多项式拟合到数据的每一端的前四个点)数据,然后将它们与样条方程式联系起来。 As you can see from the plot, this can give very different results for the first few points but then tend to converge for the interior points. 从图中可以看出,这对于前几个点可能会产生非常不同的结果,但随后趋于收敛于内部点。 The Matlab documentation of it's spline funciton doesn't seem to describe how it treats the boundary conditions and I don't have access to the reference it gives but from the plot, it appears to be very close to the fmm method of R. The interpolated interior points agree to at least three significant figures. Matlab的样条曲线函数文档似乎没有描述它如何处理边界条件,并且我无法访问它给出的参考,但是从图中可以看出,它似乎非常接近R的fmm方法。插值内部点至少同意三个有效数字。 However, based on this example, it would appear that the spline functions from Matlab and R with fmm are not identical. 但是,根据此示例,看来Matlab和R中带有fmmspline函数并不相同。
在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM