R中的基本二维三次样条拟合

时间:2011-12-23 01:52:31

标签: r matlab

here所示的2D空间中,简单的Matlab三次样条插值的R等价是多少,即

n = 7;
x = rand(n,1);
y = rand(n,1);
plot(x,y,'.')
axis([0 1 0 1])
t = 1:n;
ts = 1:1/10:n;
xs = spline(t,x,ts);
ys = spline(t,y,ts);
hold on
plot(xs,ys,'r');
hold off

我尝试了R中的变体,但它们似乎需要对x向量进行排序,并且挖掘相关问题并没有让我更进一步。感谢...

1 个答案:

答案 0 :(得分:10)

可能这是R版本:

n <- 7
x <- runif(n)
y <- runif(n)
t <- 1:n
ts <- seq(1, n, by = 1/10)
xs <- splinefun(t, x)(ts)
ys <- splinefun(t, y)(ts)

plot(x, y, xlim = c(0, 1), ylim = c(0, 1))
lines(xs, ys)

请注意,我不确定样条曲线的算法是否与matlab完全相同。

enter image description here