简体   繁体   中英

Create Envelope with Spline Interpolation in R

Hello I have tried spline interpolation in 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
    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:

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:

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

But I get different result with my code in Matlab. How can I get the same result in R? Or "spline" on Matlab and R is different function?

Thanks you very much

In the spline function, n specifies the number of "equally spaced points spanning the interval [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. For comparison, you may want to use xout = the sequence 1:36 to get most direct comparison. 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.

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. 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. However, based on this example, it would appear that the spline functions from Matlab and R with fmm are not identical.
在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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