-1

R で 3 次スプラインの実装を試みています。R ライブラリで利用可能な spline、smooth.spline、smooth.Pspline 関数を既に使用していますが、結果に満足していないので、自分自身を納得させたいと思っています。 「自家製」のスプライン関数による結果の一貫性。3 次多項式の係数は既に計算しましたが、結果をプロットする方法がわかりません..ランダムな点のようです。以下のソースコードを見つけることができます。どんな助けでも大歓迎です。

x = c(35,36,39,42,45,48)
y = c(2.87671519825595, 4.04868309245341,   3.95202175000174,   
  3.87683188946186, 4.07739945984612,   2.16064840967985)


n = length(x)

#determine width of intervals
h=0
for (i in 1:(n-1)){
   h[i] = (x[i+1] - x[i])
}

A = 0
B = 0
C = 0
D = 0
#determine the matrix influence coefficients for the natural spline
for (i in 2:(n-1)){
  j = i-1
  D[j] = 2*(h[i-1] + h[i])
  A[j] = h[i]
  B[j] = h[i-1] 

}

#determine the constant matrix C
for (i in 2:(n-1)){
  j = i-1
  C[j] = 6*((y[i+1] - y[i]) / h[i] - (y[i] - y[i-1]) / h[i-1])
}

#maximum TDMA length
ntdma = n - 2

#tridiagonal matrix algorithm

#upper triangularization
R = 0
for (i in 2:ntdma){
  R = B[i]/D[i-1]
  D[i] = D[i] - R * A[i-1]
  C[i] = C[i] - R * C[i-1] 
}

#set the last C
C[ntdma] = C[ntdma] / D[ntdma]

#back substitute
for (i in (ntdma-1):1){
  C[i] = (C[i] - A[i] * C[i+1]) / D[i]
}

#end of tdma

#switch from C to S
S = 0
for (i in 2:(n-1)){
  j = i - 1
  S[i] = C[j]
}
#end conditions
S[1] <- 0 -> S[n]

#Calculate cubic ai,bi,ci and di from S and h
for (i in 1:(n-1)){
 A[i] = (S[i+ 1] - S[i]) / (6 * h[i])
 B[i] = S[i] / 2
 C[i] = (y[i+1] - y[i]) / h[i] - (2 * h[i] * S[i] + h[i] * S[i + 1]) / 6
 D[i] = y[i]
}


#control points
xx = c(x[2],x[4])
yy = 0
#spline evaluation
for (j in 1:length(xx)){
  for (i in 1:n){
    if (xx[j]<=x[i]){
      break
    }
    yy[i] = A[i]*(xx[j] - x[i])^3 + B[i]*(xx[j] - x[i])^2 + C[i]*(xx[j] - x[i]) + D[i]

 }
points(x,yy ,col="blue")
}

ありがとうございました

4

1 に答える 1

9

よし、これで…

ここでの「コントロール ポイント」は、3 次スプラインを評価するポイントです。したがって、返されるポイントの数 (yy) は xx と同じ長さです。これは私に何かを見つけさせました:

for (j in 1:length(xx)){
  for (i in 1:n){
    if (xx[j]<=x[i]){
      break
    }
    yy[i] = A[i]*(xx[j] - x[i])^3 + B[i]*(xx[j] - x[i])^2 + C[i]*(xx[j] - x[i]) + D[i]

 }

これは、yy の「n」個の値のみを計算しています。こんにちは、ここで何が問題なのですか? length(xx) 値を返す必要があります...

次に、何か他のものを見つけたと思います-あなたの「ブレーク」はforループから脱落します。あなたが本当に望むのは、そのiをスキップして、あなたのポイントに関連するものに到達するまで次のものに進むことです:

#spline evaluation
for (j in 1:length(xx)){
  for (i in 1:n){
    if (xx[j]<=x[i]){
      next
     }
    yy[j] = A[i]*(xx[j] - x[i])^3 + B[i]*(xx[j] - x[i])^2 + C[i]*(xx[j] - x[i]) + D[i]

 }
}

これは、いくつかの yy[j] を計算してループの次のラウンドでそれらをダンプしているため、非効率的ですが、それでも仕事は完了します。

これを関数にラップして、簡単に操作できるようにします。私の関数 'myspline' は、適合するデータに x と y を使用し、予測位置に xx ベクトルを使用します。できます:

> xx=seq(35,48,len=100)
> yy = myspline(x,y,xx)
> plot(xx,yy,type="l")
> points(x,y)
> 

そして、(x、y)ポイントを通る素敵な曲線が得られます。見逃しているように見えてゼロに向かう最初のポイントを除いて、どこかにまだ1つずれているエラーがあると思います。しかたがない。99%完了。

コードは次のとおりです。

myspline <- function(x,y,xx){

n = length(x)

h=0;yy=0
#determine width of intervals
for (i in 1:(n-1)){
   h[i] = (x[i+1] - x[i])
}

A = 0
B = 0
C = 0
D = 0
#determine the matrix influence coefficients for the natural spline
for (i in 2:(n-1)){
  j = i-1
  D[j] = 2*(h[i-1] + h[i])
  A[j] = h[i]
  B[j] = h[i-1] 

}

#determine the constant matrix C
for (i in 2:(n-1)){
  j = i-1
  C[j] = 6*((y[i+1] - y[i]) / h[i] - (y[i] - y[i-1]) / h[i-1])
}

#maximum TDMA length
ntdma = n - 2

#tridiagonal matrix algorithm

#upper triangularization
R = 0
for (i in 2:ntdma){
  R = B[i]/D[i-1]
  D[i] = D[i] - R * A[i-1]
  C[i] = C[i] - R * C[i-1] 
}

#set the last C
C[ntdma] = C[ntdma] / D[ntdma]

#back substitute
for (i in (ntdma-1):1){
  C[i] = (C[i] - A[i] * C[i+1]) / D[i]
}

#end of tdma

#switch from C to S
S = 0
for (i in 2:(n-1)){
  j = i - 1
  S[i] = C[j]
}
#end conditions
S[1] <- 0 -> S[n]

#Calculate cubic ai,bi,ci and di from S and h
for (i in 1:(n-1)){
 A[i] = (S[i+ 1] - S[i]) / (6 * h[i])
 B[i] = S[i] / 2
 C[i] = (y[i+1] - y[i]) / h[i] - (2 * h[i] * S[i] + h[i] * S[i + 1]) / 6
 D[i] = y[i]
}


#control points
#xx = seq(x[2],x[4],len=100)

#spline evaluation
for (j in 1:length(xx)){
  for (i in 1:n){
    if (xx[j]<=x[i]){
      next
     }
    yy[j] = A[i]*(xx[j] - x[i])^3 + B[i]*(xx[j] - x[i])^2 + C[i]*(xx[j] - x[i]) + D[i]
 }
}
return(yy)
}
于 2011-11-24T14:52:47.133 に答える