Keyword: 常微分方程式, ルンゲ・クッタ法
概要
本サンプルはルンゲ・クッタ法を用いて常微分方程式を解くFortranによるサンプルプログラムです。 本サンプルは以下に示される方程式を解いて結果を出力します。
※本サンプルはnAG Fortranライブラリに含まれるルーチン d02pcf() のExampleコードです。本サンプル及びルーチンの詳細情報は d02pcf のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本ルーチンの詳細はd02pcf のマニュアルページを参照)| このデータをダウンロード |
D02PCF Example Program Data 1 : method 0.0 6.28318530717958647692 : tstart, tend 0.0 1.0 : ystart(1:neq) 0.0 : hstart 1.0E-8 1.0E-8 : thres(1:neq) .FALSE. : errass
- 1行目はタイトル行で読み飛ばされます。
- 2行目は使用されるルンゲ・クッタ法(method=1:2(3)ペアを使用)を指定しています。
- 3行目は独立変数xの初期値(tstart)と最終値(tend)を指定しています。
- 4行目は解の初期値(ystart)を指定しています。
- 5行目は積分の最初のステップのサイズ(hstart)を指定しています。
- 6行目は閾値のベクトル(thres)を指定しています。
- 7行目は大域的誤差評価が計算されるかどうか(errass=.FALSE.:計算されない)を指定しています。
出力結果
(本ルーチンの詳細はd02pcf のマニュアルページを参照)| この出力例をダウンロード |
D02PCF Example Program Results
Calculation with TOL = 0.1E-02
t y1 y2
0.000 0.000 1.000
0.785 0.707 0.707
1.571 0.999 0.000
2.356 0.706 -0.706
3.142 0.000 -0.999
3.927 -0.706 -0.706
4.712 -0.998 0.000
5.498 -0.705 0.706
6.283 0.001 0.997
Cost of the integration in evaluations of F is 124
Calculation with TOL = 0.1E-03
t y1 y2
0.000 0.000 1.000
0.785 0.707 0.707
1.571 1.000 0.000
2.356 0.707 -0.707
3.142 0.000 -1.000
3.927 -0.707 -0.707
4.712 -1.000 0.000
5.498 -0.707 0.707
6.283 0.000 1.000
Cost of the integration in evaluations of F is 235
- 3行目には誤差許容値が出力されています。
- 7〜15行目に独立変数と誤差許容値 0.1e-002 の場合の解が出力されています。
- 17行目には最初の積分で使用されたユーザ提供関数 f の評価の数が出力されています。
- 19行目には誤差許容値が出力されています。
- 23〜31行目に独立変数と誤差許容値 0.1e-003 の場合の解が出力されています。
- 33行目には最初の積分で使用されたユーザ提供関数 f の評価の数が出力されています。
ソースコード
(本ルーチンの詳細はd02pcf のマニュアルページを参照)
※本サンプルソースコードは科学技術・統計計算ライブラリである「nAG Fortranライブラリ」のルーチンを呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
! D02PCF Example Program Text
! Mark 23 Release. nAG Copyright 2011.
MODULE d02pcfe_mod
! D02PCF Example Program Module:
! Parameters and User-defined Routines
! .. Use Statements ..
USE nag_library, ONLY : nag_wp
! .. Implicit None Statement ..
IMPLICIT NONE
! .. Parameters ..
REAL (KIND=nag_wp), PARAMETER :: tol1 = 1.0E-3_nag_wp
REAL (KIND=nag_wp), PARAMETER :: tol2 = 1.0E-4_nag_wp
INTEGER, PARAMETER :: neq = 2, nin = 5, nout = 6, &
npts = 8
INTEGER, PARAMETER :: lenwrk = 32*neq
CONTAINS
SUBROUTINE f(t,y,yp)
! .. Implicit None Statement ..
IMPLICIT NONE
! .. Scalar Arguments ..
REAL (KIND=nag_wp), INTENT (IN) :: t
! .. Array Arguments ..
REAL (KIND=nag_wp), INTENT (IN) :: y(*)
REAL (KIND=nag_wp), INTENT (OUT) :: yp(*)
! .. Executable Statements ..
yp(1) = y(2)
yp(2) = -y(1)
RETURN
END SUBROUTINE f
END MODULE d02pcfe_mod
PROGRAM d02pcfe
! D02PCF Example Main Program
! .. Use Statements ..
USE nag_library, ONLY : d02pcf, d02pvf, d02pyf, nag_wp
USE d02pcfe_mod, ONLY : f, lenwrk, neq, nin, nout, npts, tol1, tol2
! .. Implicit None Statement ..
IMPLICIT NONE
! .. Local Scalars ..
REAL (KIND=nag_wp) :: hnext, hstart, tend, tgot, tinc, &
tol, tstart, twant, waste
INTEGER :: i, ifail, j, method, stpcst, &
stpsok, totf
LOGICAL :: errass
! .. Local Arrays ..
REAL (KIND=nag_wp), ALLOCATABLE :: thres(:), work(:), ygot(:), &
ymax(:), ypgot(:), ystart(:)
! .. Intrinsic Functions ..
INTRINSIC real
! .. Executable Statements ..
WRITE (nout,*) 'D02PCF Example Program Results'
! Skip heading in data file
READ (nin,*)
READ (nin,*) method
ALLOCATE (thres(neq),work(lenwrk),ygot(neq),ymax(neq),ypgot(neq), &
ystart(neq))
! Set initial conditions and input for D02PVF
READ (nin,*) tstart, tend
READ (nin,*) ystart(1:neq)
READ (nin,*) hstart
READ (nin,*) thres(1:neq)
READ (nin,*) errass
! Set control for output
tinc = (tend-tstart)/real(npts,kind=nag_wp)
LOOP: DO i = 1, 2
IF (i==1) THEN
tol = tol1
ELSE
tol = tol2
END IF
! ifail: behaviour on error exit
! =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
ifail = 0
CALL d02pvf(neq,tstart,ystart,tend,tol,thres,method,'Usual Task', &
errass,hstart,work,lenwrk,ifail)
WRITE (nout,99999) tol
WRITE (nout,99998)
WRITE (nout,99997) tstart, ystart(1:neq)
twant = tstart
DO j = 1, npts
twant = twant + tinc
ifail = -1
CALL d02pcf(f,twant,tgot,ygot,ypgot,ymax,work,ifail)
WRITE (nout,99997) tgot, ygot(1:neq)
END DO
ifail = 0
CALL d02pyf(totf,stpcst,waste,stpsok,hnext,ifail)
WRITE (nout,99996) totf
END DO LOOP
99999 FORMAT (/' Calculation with TOL = ',E8.1)
99998 FORMAT (/' t y1 y2'/)
99997 FORMAT (1X,F6.3,2(3X,F7.3))
99996 FORMAT (/' Cost of the integration in evaluations of F is',I6)
END PROGRAM d02pcfe
