Program FixedPointIteration
!// 使用不動點迭代方法求解方程 x^3 + x - 1 = 0 的零點
Implicit none
Real(kind=8), parameter :: eps = 1.d-8
Real(kind=8), external :: func
Real(kind=8) :: xtemp, x0 = 0.5d0 !// x0爲迭代初始值
Do
xtemp = func ( x0 )
If ( abs( xtemp - x0 ) < eps ) exit
x0 = xtemp
End do
Write ( *,'(a,f13.9)' ) ' the result of fixed point iteration is', xtemp
End program FixedPointIteration
Real(kind=8) function func ( x )
Implicit none
Real(kind=8), intent(in) :: x
func = ( 1.d0 + 2.d0 * x * x * x ) / ( 1.d0 + 3.d0 * x * x )
End function func
!// 方程 x^3 + x - 1 = 0 主要的不動點格式有三種
!// 第一種:x = 1 - x^3 這一種格式不收斂
!// 第二種:x = ( 1 - x^3 )^(1/3),這種格式收斂
!// 第三種:就是此代碼中的收斂格式。x = ( 1 + 2*x^3 ) / ( 1 + 3*x^2 )
!// 這種格式不僅收斂,而且收斂速度快,精度高