- #1
Vrbic
- 407
- 18
Hello, I'm fortran beginner. I have problem with calling or definition of functions and subroutines. I decided to establish my routine file, where I collect all functions and subroutines who I code (for now I have there function for derivative, only this works).
My first question, is it good way, to colect all in external file and call from it?
May I have functions and subroutins in same file?
May I call function from same routine file into subroutine in same file?
I'm following Numerical recipes in fortran 77 from Shapiro at all. Now I'm struggeling with runge-kutta 4 subroutine. I just write it into my routine file and don't call. To main program I call only derivative function above. But compiler reports many errors:
1)
INTEGER :: i
1
Error: Unexpected data declaration statement at (1)
2)
DOUBLE PRECISION :: h6,hh,xh,dym(NMAX),dyt(NMAX),yt(NMAX)
1
Error: Unexpected data declaration statement at (1)
3) atc.
atc. but I followed recipies...
Can you advice me?
My code
Main
routines
My first question, is it good way, to colect all in external file and call from it?
May I have functions and subroutins in same file?
May I call function from same routine file into subroutine in same file?
I'm following Numerical recipes in fortran 77 from Shapiro at all. Now I'm struggeling with runge-kutta 4 subroutine. I just write it into my routine file and don't call. To main program I call only derivative function above. But compiler reports many errors:
1)
INTEGER :: i
1
Error: Unexpected data declaration statement at (1)
2)
DOUBLE PRECISION :: h6,hh,xh,dym(NMAX),dyt(NMAX),yt(NMAX)
1
Error: Unexpected data declaration statement at (1)
3) atc.
atc. but I followed recipies...
Can you advice me?
My code
Main
Fortran:
DOUBLE PRECISION FUNCTION X2(a)
DOUBLE PRECISION :: a,OUT
OUT=sin(a)
RETURN
END FUNCTION X2
DOUBLE PRECISION FUNCTION DDX2(a)
DOUBLE PRECISION :: a,OUT
OUT=cos(a)
RETURN
END FUNCTION DDX2
PROGRAM HLAVNI
IMPLICIT NONE
INTEGER :: i
DOUBLE PRECISION :: X, DX2, h
DOUBLE PRECISION, EXTERNAL :: X2, DDX2
h=10.0**(-5)
WRITE(*,*) 'Zadejte ve kterem bode chcete derivovat'
READ(*,*) X
wRITE(*,*) 'Hodnota v bode x', X2(X)
CALL derivs(X,X2,DX2)
wRITE(*,*) 'Derivace v bode x', DX2
wRITE(*,*) 'Opravdova derivace v bode x', DDX2(X)
READ(*,*) X
END PROGRAM
Fortran:
!*********************************!
!****** Derivative function ******!
!*********************************!
DOUBLE PRECISION FUNCTION derivs(x,y,dydx)
DOUBLE PRECISION :: x,y,dydx,h
h=10.0**(-10)
dydX=(y(x+h)-y(x-h))/(2*h)return
END FUNCTION derivs
!*********************************!
!***** Runge-Kutta 4th order *****!
!*********************************!
SUBROUTINE rk4(y,dydx,n,x,h,yout)
INTEGER :: n,NMAX
DOUBLE PRECISION :: h,x,dydx(n),y(n),yout(n)
!DOUBLE PRECISION, EXTERNAL :: derivs
NMAX=50 !Maximum number of equation
INTEGER :: i
DOUBLE PRECISION :: h6,hh,xh,dym(NMAX),dyt(NMAX),yt(NMAX)
hh=h*0.5
h6=h/6.
xh=x+hh
!I move calls to cycle and add (i) to dyt(i) atc.
!First step
do i=1,n
yt(i)=y(i)+hh*dydx(i)
call derivs(xh,yt,dyt(i))
end do
!Second step
do i=1,n
yt(i)=y(i)+hh*dyt(i)
call derivs(xh,yt,dym(i))
end do
!Third step
do i=1,n
yt(i)=y(i)+hh*dym(i)
dyn(i)=dyt(i)+dym(i)
call derivs(x+h,yt,dyt(i))
end do
!Fourth step
do i=1,n
yout(i)=y(i)+h6*(dydx(i)+dyt(i)+2.*dym(i))
end do
return
END SUBROUTINE