我对Fortran还是比较陌生,我有一项任务是查找正交权重和点,这些点和点是第n个Legendre多项式的零(使用牛顿法找到);我制作了函数来查找Pn(x)和P'n(x)的值,以将其归入牛顿方法。
但是当实际使用正交子例程中的函数时,它会返回:
Coursework2a.f90:44.3:
x = x - P(n,x)/dP(n,x)
1
Error: Unclassifiable statement at (1)
有人知道为什么将此陈述归为无法归类吗?
subroutine Quadrature(n)
implicit none
integer, parameter :: dpr = selected_real_kind(15) !Double precision
real(dpr) :: P, dP, x, x_new, error = 1, tolerance = 1.0E-6, Pi = 3.141592 !Define Variables
integer, intent(in) :: n
integer :: i
!Next, find n roots. Start with first guess then iterate until error is greater than some tolerance.
do i = 1,n
x = -cos(((2.0*real(i)-1.0)/2.0*real(n))*Pi)
do while (error > tolerance)
x_new = x
x = x - P(n,x)/dP(n,x)
error = abs(x_new-x)
end do
print *, x
end do
end subroutine Quadrature
行
x = -cos(((2.0*real(i)-1.0)/2.0*real(n))*Pi)
很可能在分母周围缺少括号。照原样,该行将(2.0*real(i)-1.0)
除以2.0
,然后将整个内容乘以real(n)
。这可能就是为什么每个循环的根都相同的原因。
real function p(n,x)
real::n,x
p=2*x**3 !or put in the function given to you.
end function
real function dp(n,x)
real::n,x
dp=6*x**2 !you mean derivative of polynomial p, I guess.
end function
在主程序外单独定义这样的功能。在主程序中声明以下函数:
real,external::p, dp