- #1
joseph2015
- 14
- 2
I have a simple Fortran code for solving velocity and displacement of spring, the code works fine when writing all together without using a subroutine. but as I am learning Fortran I tried to do it using subroutine, but I keep getting errors
appreciate any help in advance.
appreciate any help in advance.
Fortran:
program msd
implicit none
real, parameter :: tstop=100.
integer i
real, dimension(2000) :: t,y,v
open (2, file = "test.txt")!v(1)=0.0
!y(1)=1.0
!t(1)=0.0
do i=1,1000
call deriv(v,y,t)
write (2,*) v(i),y(i),t(i)
end do
close(2)
end program msd
!******************************************************
subroutine deriv(v,y,t)
implicit none
real, parameter :: m=1.,b=0.2,k=1., dt=0.1
real, dimension(2000) :: t,y,v
integer i
v(1)=0.0
y(1)=1.0
t(1)=0.0
v(i+1) = v(i) +(-(b/m)*v(i)-(k/m)*y(i))*dt
y(i+1) = y(i) + v(i)*dt
t(i+1) = t(i)+dt
v(i)=v(i+1)
y(i)=y(i+1)
t(i)=t(i+1)
end subroutine deriv
Last edited by a moderator: