I was trying to code this problem in fortran. The output file contains the result in two columns. I am having hard time to modify my code (below) to get a gnuplot-ready output file containing nt columns and nx lines. Can anybody help me? Thanks!
PROGRAM odlc
IMPLICIT NONE
INTEGER::i,it,nx,nt,k,ierr
DOUBLE PRECISION::dx,dt,c
DOUBLE PRECISION,DIMENSION(2000)::u,un
ierr=0
nx=20
nt=50
dt=0.01
c=1.0
dx=2./(nx-1.)
!initial condition
DO i = 1,nx
IF(i*dx>=0.5 .and. i*dx<=1) THEN
u(i) = 2
ELSE
u(i)=1
ENDIF
ENDDO
!Finite Difference
OPEN(UNIT=200,FILE='tab2.txt',STATUS='REPLACE',ACTION='WRITE',IOSTAT=ierr)
DO it=1,nt
DO k=1,nx
un(k)=u(k)
ENDDO
DO i=2,nx-1
u(i)=un(i)-c*dt/dx*(un(i)-un(i-1))
ENDDO
DO i=1,nx
WRITE(200,'(I7,F10.2)')i,u(i)
ENDDO
ENDDO
CLOSE(UNIT=200)
END
I’m not 100% sure that it is exactly what you want but here you go:
Declare
and initialize it like this
This just creates a format string which writes a single line of
nxreals.Then replace
by
Now in your
tab2.txtfile, you will getntlines andnxcolumns.After your update, here is the (slightly shortened) code that will do what you want: