Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created December 22, 2018 04:46
Show Gist options
  • Save komasaru/8a11eb403c78caf5282fafe548386571 to your computer and use it in GitHub Desktop.
Save komasaru/8a11eb403c78caf5282fafe548386571 to your computer and use it in GitHub Desktop.
Fortran 95 source code to computer Taylor expansion(cos(x)).
!****************************************************
! テイラー展開 [ cos(x) ]
!
! * 自作関数と組込関数の計算結果を比較
!
! date name version
! 2018.12.12 mk-mode.com 1.00 新規作成
!
! Copyright(C) 2018 mk-mode.com All Rights Reserved.
!****************************************************
!
module const
! SP: 単精度(4), DP: 倍精度(8)
integer, parameter :: SP = kind(1.0)
integer(SP), parameter :: DP = selected_real_kind(2 * precision(1.0_SP))
real(DP), parameter :: PI = 4.0_DP * atan(1.0_DP) ! 円周率
real(DP), parameter :: D2R = PI / 180.0_DP ! 度 => ラジアン
real(DP), parameter :: EPS = 1.0e-8_DP ! 精度
end module const
module taylor_expansion
use const
implicit none
private
public :: my_cos
contains
! テイラー展開
!
! :param(in) real(8) x: X
! :param(inout) real(8) y: 展開後
function my_cos(x) result(y)
implicit none
real(DP), intent(in) :: x
real(DP) :: y, d, s, e, x_tmp
integer(SP) :: k
! 変数初期化
d = 1.0_DP
s = 1.0_DP
e = 1.0_DP
! x の値が 0 から 2π の範囲外の場合、0 から 2π に収める
x_tmp = mod(x, 2.0_DP * PI)
! 最大200回ループ処理
! ( ただし、偶数項は 0 なので除外 )
do k = 1, 200, 2
d = s ! d 和
e = -e * x_tmp * x_tmp / (k * (k + 1)) ! 各項の値
s = s + e ! s 和
! 打ち切り誤差
if (abs(s - d) / abs(d) < EPS) then
y = s
return
end if
end do
! 収束しない時
y = 9999.0_DP
end function my_cos
end module taylor_expansion
program taylor_expansion_cos
use const, only : SP, DP, D2R
use taylor_expansion
implicit none
integer(SP) :: x
real(DP) :: r_x
! x = 0 〜 180 を 10 刻みで計算
print '(A)', " x my_cos(x) cos(x)"
do x = 0, 180, 10
r_x = real(x, DP)
print '(X, F5.1, 2X, F9.6, 2X, F9.6)', &
& r_x, my_cos(r_x * D2R), cos(r_x * D2R)
end do
end program taylor_expansion_cos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment