Keyword: 実線形等式制約, 最小二乗問題, LSE
概要
本サンプルは実線形等式制約つき最小二乗(LSE)問題を解くFortranによるサンプルプログラムです。 本サンプルは以下に示される実線形等式制約つき最小二乗(LSE)問題を解いて解を出力します。
※本サンプルはnAG Fortranライブラリに含まれるルーチン f08zaf() のExampleコードです。本サンプル及びルーチンの詳細情報は f08zaf のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本ルーチンの詳細はf08zaf のマニュアルページを参照)| このデータをダウンロード |
F08ZAF Example Program Data 6 4 2 :Values of M, N and P -0.57 -1.28 -0.39 0.25 -1.93 1.08 -0.31 -2.14 2.30 0.24 0.40 -0.35 -1.93 0.64 -0.66 0.08 0.15 0.30 0.15 -2.13 -0.02 1.03 -1.43 0.50 :End of matrix A 1.00 0.00 -1.00 0.00 0.00 1.00 0.00 -1.00 :End of matrix B -1.50 -2.14 1.23 -0.54 -1.68 0.82 :End of vector c 0.00 0.00 :End of vector d
- 1行目はタイトル行で読み飛ばされます。
- 3行目に行列Aの行数(m)、行列Aと行列Bの列数(n)、行列Bの行数(p)を指定しています。
- 5〜10行目に行列Aの要素を指定しています。
- 12〜13行目に行列Bの要素を指定しています。
- 15〜20行目にベクトルCの要素を指定しています。
- 12〜13行目にベクトルDの要素を指定しています。
出力結果
(本ルーチンの詳細はf08zaf のマニュアルページを参照)| この出力例をダウンロード |
F08ZAF Example Program Results
Constrained least-squares solution
0.4890 0.9975 0.4890 0.9975
Square root of the residual sum of squares
2.51E-02
- 4行目に制約つき最小二乗問題の解が出力されています。
- 7行目に残差平方和の平方根が出力されています。
ソースコード
(本ルーチンの詳細はf08zaf のマニュアルページを参照)
※本サンプルソースコードは科学技術・統計計算ライブラリである「nAG Fortranライブラリ」のルーチンを呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
PROGRAM f08zafe
! F08ZAF Example Program Text
! Mark 23 Release. nAG Copyright 2011.
! .. Use Statements ..
USE nag_library, ONLY : dgglse, dnrm2, nag_wp
! .. Implicit None Statement ..
IMPLICIT NONE
! .. Parameters ..
INTEGER, PARAMETER :: nb = 64, nin = 5, nout = 6
! .. Local Scalars ..
REAL (KIND=nag_wp) :: rnorm
INTEGER :: i, info, lda, ldb, lwork, m, n, p
! .. Local Arrays ..
REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), c(:), d(:), work(:), &
x(:)
! .. Executable Statements ..
WRITE (nout,*) 'F08ZAF Example Program Results'
WRITE (nout,*)
! Skip heading in data file
READ (nin,*)
READ (nin,*) m, n, p
lda = m
ldb = p
lwork = p + n + nb*(m+n)
ALLOCATE (a(lda,n),b(ldb,n),c(m),d(p),work(lwork),x(n))
! Read A, B, C and D from data file
READ (nin,*) (a(i,1:n),i=1,m)
READ (nin,*) (b(i,1:n),i=1,p)
READ (nin,*) c(1:m)
READ (nin,*) d(1:p)
! Solve the equality-constrained least-squares problem
! minimize ||c - A*x|| (in the 2-norm) subject to B*x = D
! The nAG name equivalent of dgglse is f08zaf
CALL dgglse(m,n,p,a,lda,b,ldb,c,d,x,work,lwork,info)
! Print least-squares solution
WRITE (nout,*) 'Constrained least-squares solution'
WRITE (nout,99999) x(1:n)
! Compute the square root of the residual sum of squares
! The nAG name equivalent of dnrm2 is f06ejf
rnorm = dnrm2(m-n+p,c(n-p+1),1)
WRITE (nout,*)
WRITE (nout,*) 'Square root of the residual sum of squares'
WRITE (nout,99998) rnorm
99999 FORMAT (1X,7F11.4)
99998 FORMAT (3X,1P,E11.2)
END PROGRAM f08zafe
