Reputation: 59
program main
implicit none
double precision p, a, b, tol
a = 1.0d0
b = 30.0d0
tol = 1e-8
call newtonbisection(p, a, b, tol)
contains
function f(x)
implicit none
double precision x,f
f = SIN(X) - exp(-x)
end function
function fprime(x)
implicit none
double precision fprime, x
fprime = COS(x) + exp(-x)
end function
subroutine newtonbisection(p, a, b, tol)
implicit none
double precision p, a, b, tol
double precision f, fprime
double precision fb, fp, fp2
a = 1.0d0
b = 30.0d0
p = a
do
fb = f(b)
fp = f(p)
fp2 = fprime(p)
p = p - (fp/fp2)
if (p < a .or. p > b) then
p = (a+b) / 2.d0
end if
if (fp*fb < 0) then
a = p
else
b = p
end if
print*, "a, b, p, fp", a, b, p, fp
if (dabs(fp) < tol) then
exit
end if
end do
end subroutine
end program
when I compile (gfortran newtonbisection.f90) I'm getting following errors:
newtonbisection.f90:(.text+0x76): undefined reference to 'f_'
newtonbisection.f90:(.text+0x8f): undefined reference to 'f_'
newtonbisection.f90:(.text+0xa8): undefined reference to 'fprime_'
collect2: ld returned 1 exit status
Last time when I was having similar issues, putting implicit none and contains fixed them, but not this time. Why does it say f and fprime are not defined when they are?
Upvotes: 1
Views: 669
Reputation: 59998
You cannot put lines as
double precision f, fprime
into subroutine newtonbisection
. That means that f
and fprime
would be external functions, but they are not, they are internal functions of the main program.
Delete that line. It has no place there, the internal functions see the other functions due to the so-called host association, they are all contained in a single host program.
Also, internals functions and subroutines don't require repeated implicit none
, the implicit none
from the main program is valid in them, because they are contained in it.
Upvotes: 2