【发布时间】:2021-04-03 03:23:13
【问题描述】:
我正在尝试实现 Newton 的方法,但收到一条令人困惑的错误消息。在我的代码中,您会看到我用 f1 和 f2 调用 external ,我假设它告诉计算机查找函数,但它根据错误消息将它们视为变量。我已经阅读了与我的问题类似的堆栈溢出帖子,但似乎没有一个解决方案有效。我尝试过使用和不使用外部,但问题仍然存在。希望有人能看到我缺少的东西。
implicit none
contains
subroutine solve(f1,f2,x0,n, EPSILON)
implicit none
real(kind = 2), external:: f1, f2
real (kind = 2), intent(in):: x0, EPSILON
real (kind = 2):: x
integer, intent(in):: n
integer:: iteration
x = x0
do while (abs(f1(x))>EPSILON)
iteration = iteration + 1
print*, iteration, x, f1(x)
x = x - (f1(x)/f2(x))
if (iteration >= n) then
print*, "No Convergence"
stop
end if
end do
print*, iteration, x
end subroutine solve
end module newton
Program Lab10
use newton
implicit none
integer, parameter :: n = 1000 ! maximum iteration
real(kind = 2), parameter :: EPSILON = 1.d-3
real(kind = 2):: x0, x
x0 = 3.0d0
call solve(f(x),fp(x),x0,n, EPSILON)
contains
real (kind = 2) function f(x) ! this is f(x)
implicit none
real (kind = 2), intent(in)::x
f = x**2.0d0-1.0d0
end function f
real (kind = 2) function fp(x) ! This is f'(x)
implicit none
real (kind = 2), intent(in)::x
fp = 2.0d0*x
end function fp
end program Lab10```
【问题讨论】:
标签: fortran