Keyword: 3次, スプライン, フィット
概要
本サンプルは3次スプライン曲線フィットを行うFortranによるサンプルプログラムです。 本サンプルは以下に示されるデータについてスプライン曲線フィットを行い、ノットの値とBスプライン係数を求めて出力します。
※本サンプルはnAG Fortranライブラリに含まれるルーチン e02bef() のExampleコードです。本サンプル及びルーチンの詳細情報は e02bef のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本ルーチンの詳細はe02bef のマニュアルページを参照)| このデータをダウンロード |
E02BEF Example Program Data 15 M, the number of data points 0.0000E+00 -1.1000E+00 1.00 X, Y, W, abscissa, ordinate and weight 5.0000E-01 -3.7200E-01 2.00 1.0000E+00 4.3100E-01 1.50 1.5000E+00 1.6900E+00 1.00 2.0000E+00 2.1100E+00 3.00 2.5000E+00 3.1000E+00 1.00 3.0000E+00 4.2300E+00 0.50 4.0000E+00 4.3500E+00 1.00 4.5000E+00 4.8100E+00 2.00 5.0000E+00 4.6100E+00 2.50 5.5000E+00 4.7900E+00 1.00 6.0000E+00 5.2300E+00 3.00 7.0000E+00 6.3500E+00 1.00 7.5000E+00 7.1900E+00 2.00 8.0000E+00 7.9700E+00 1.00 End of data points 1.0 S, smoothing factor 0.5 S, smoothing factor 0.1 S, smoothing factor
- 1行目はタイトル行で読み飛ばされます。
- 2行目にデータ点の数(m)を指定しています。
- 3〜17行目には独立変数x、従属変数y、重み(w)を指定しています。
- 18〜20行目に平滑化因子(s)を指定しています。
出力結果
(本ルーチンの詳細はe02bef のマニュアルページを参照)| この出力例をダウンロード |
E02BEF Example Program Results
Calling with smoothing factor S = 1.000E+00
B-Spline
J Knot LAMDA(J+2) Coefficient C(J)
1 -1.3201
2 0.0000 1.3542
3 4.0000 5.5510
4 8.0000 4.7031
5 8.2277
Weighted sum of squared residuals FP = 1.000E+00
Calling with smoothing factor S = 5.000E-01
B-Spline
J Knot LAMDA(J+2) Coefficient C(J)
1 -1.1072
2 0.0000 -0.6571
3 1.0000 0.4350
4 2.0000 2.8061
5 4.0000 4.6824
6 5.0000 4.6416
7 6.0000 5.1976
8 8.0000 6.9008
9 7.9979
Weighted sum of squared residuals FP = 5.001E-01
Calling with smoothing factor S = 1.000E-01
B-Spline
J Knot LAMDA(J+2) Coefficient C(J)
1 -1.0901
2 0.0000 -0.6401
3 1.0000 0.0334
4 1.5000 1.6390
5 2.0000 2.1243
6 3.0000 4.5591
7 4.0000 4.2174
8 4.5000 4.9105
9 5.0000 4.5475
10 5.5000 4.6960
11 6.0000 5.7370
12 8.0000 6.8179
13 7.9953
Weighted sum of squared residuals FP = 9.999E-02
- 3行目に呼び出される際の平滑化因子が出力されています。
- 7〜11行目にノットの位置とBスプライン係数が出力されています。
- 13行目に重みづけされた残差平方和が出力されています。
- 16行目に呼び出される際の平滑化因子が出力されています。
- 20〜28行目にノットの位置とBスプライン係数が出力されています。
- 30行目に重みづけされた残差平方和が出力されています。
- 33行目に呼び出される際の平滑化因子が出力されています。
- 37〜49行目にノットの位置とBスプライン係数が出力されています。
- 51行目に重みづけされた残差平方和が出力されています。
ソースコード
(本ルーチンの詳細はe02bef のマニュアルページを参照)
※本サンプルソースコードは科学技術・統計計算ライブラリである「nAG Fortranライブラリ」のルーチンを呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
PROGRAM e02befe
! E02BEF Example Program Text
! Mark 23 Release. nAG Copyright 2011.
! .. Use Statements ..
USE nag_library, ONLY : e02bbf, e02bef, nag_wp
! .. Implicit None Statement ..
IMPLICIT NONE
! .. Parameters ..
INTEGER, PARAMETER :: nin = 5, nout = 6
! .. Local Scalars ..
REAL (KIND=nag_wp) :: fp, s, txr
INTEGER :: ifail, ioerr, j, lwrk, m, n, nest, r
CHARACTER (1) :: start
! .. Local Arrays ..
REAL (KIND=nag_wp), ALLOCATABLE :: c(:), lamda(:), sp(:), w(:), wrk(:), &
x(:), y(:)
INTEGER, ALLOCATABLE :: iwrk(:)
! .. Executable Statements ..
WRITE (nout,*) 'E02BEF Example Program Results'
! Skip heading in data file
READ (nin,*)
! Input the number of data points, followed by the data points (X),
! the function values (Y) and the weights (W).
READ (nin,*) m
nest = m + 4
lwrk = 4*m + 16*nest + 41
ALLOCATE (x(m),y(m),w(m),iwrk(nest),lamda(nest),wrk(lwrk),c(nest), &
sp(2*m-1))
DO r = 1, m
READ (nin,*) x(r), y(r), w(r)
END DO
start = 'C'
! Read in successive values of S until end of data file.
DATA: DO
READ (nin,*,IOSTAT=ioerr) s
IF (ioerr<0) THEN
EXIT DATA
END IF
! Determine the spline approximation.
ifail = 0
CALL e02bef(start,m,x,y,w,s,nest,n,lamda,c,fp,wrk,lwrk,iwrk,ifail)
! Evaluate the spline at each X point and midway between
! X points, saving the results in SP.
DO r = 1, m
ifail = 0
CALL e02bbf(n,lamda,c,x(r),sp((r-1)*2+1),ifail)
END DO
DO r = 1, m - 1
txr = (x(r)+x(r+1))/2.0E0_nag_wp
ifail = 0
CALL e02bbf(n,lamda,c,txr,sp(r*2),ifail)
END DO
! Output the results.
WRITE (nout,*)
WRITE (nout,99999) 'Calling with smoothing factor S =', s
WRITE (nout,*)
WRITE (nout,*) ' B-Spline'
WRITE (nout,*) &
' J Knot LAMDA(J+2) Coefficient C(J)'
WRITE (nout,99998) 1, c(1)
DO j = 2, n - 5
WRITE (nout,99997) j, lamda(j+2), c(j)
END DO
WRITE (nout,99998) n - 4, c(n-4)
WRITE (nout,*)
WRITE (nout,99999) 'Weighted sum of squared residuals FP =', fp
IF (fp==0.0E0_nag_wp) THEN
WRITE (nout,*) '(The spline is an interpolating spline)'
ELSE IF (n==8) THEN
WRITE (nout,*) &
'(The spline is the weighted least-squares cubic polynomial)'
END IF
WRITE (nout,*)
start = 'W'
END DO DATA
99999 FORMAT (1X,A,1P,E12.3)
99998 FORMAT (11X,I4,20X,F16.4)
99997 FORMAT (11X,I4,2F18.4)
END PROGRAM e02befe
