我对fortran比较陌生,我有一个任务来找到正交权重和点,其中点是第n个legendre多项式的零点(使用牛顿法找到);我做了函数来找到Pn(x)和P'n(x)的值到sub into newtons方法然而当我实际使用我的正交子程序中的函数时,它回来了:
Coursework2a.f90:44.3:
x = x - P(n,x)/ dP(n,x) 1 错误:(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
答案 0 :(得分:3)
该行
x = -cos(((2.0*real(i)-1.0)/2.0*real(n))*Pi)
可能缺少分母周围的一组括号。实际上,该行将(2.0*real(i)-1.0)
除以2.0
,然后将整个事物乘以real(n)
。这可能就是为什么你为每个循环得到相同的根。
答案 1 :(得分:-1)
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