PDA

View Full Version : Problem with reading data from an external text file in RNLIN



Voivod
02-28-2009, 01:59 AM
Hello

I'm using an old Compaq Visual Fortran Compiler v6.6.0 along with the respective version of IMSL fortran library.

My attempt is to make RNLIN program to read the already existing in the program XDATA and YDATA data from an external text file and perform the same nonlinear regression fitting. In order to do this, in subroutine EXAMPL i replaced
the following code lines:

DATA YDATA/54.0, 50.0, 45.0, 37.0, 35.0, 25.0, 20.0, 16.0, 18.0, 13.0, 8.0, 11.0, 8.0, 4.0, 6.0/

DATA XDATA/2.0, 5.0, 7.0, 10.0, 14.0, 19.0, 26.0, 31.0, 34.0, 38.0, 45.0, 52.0, 53.0, 60.0, 65.0/

with the following:

open (10, file='exampl_data.txt')

do 1 IOBS=1,NOBS
read (10,*) XDATA(IOBS),YDATA(IOBS)
1 end do

In the code above, exampl_data.txt is the file i made with the same XDATA and YDATA data in the original DATA declaration statements

Every time i run ryn/compile the modified program i get the following error message (i attached a relevant image in the present post), although i get 0 errors 0 warnings during code compilation.

Is there a way tο read data from an external file with RNLIN?? For my work, I have large data files (5000 data points or more) on which i must perfmorm nonlinear regression fitting

Thanx in advance for any help given

brian
03-01-2009, 06:36 AM
Hello Voivod,

I have done something similar (code highlighted in blue, below) and it seems okay.

You should probably not be using IOBS as your increment counter.


do 1 IOBS=1,NOBS
read (10,*) XDATA(IOBS),YDATA(IOBS)
1 end do

I believe this is a parameter passed between RNLIN and your user supplied subroutine. In the case of this example, IOBS is used to select a particular value from the dependant and independent data for the evaluation function. The loop you implemented will likely fix IOBS at 15 for every evaluation.


E = YDATA(IOBS) - THETA(1)*EXP(THETA(2)*XDATA(IOBS))

You may have discrepancy between the format your READ statement is expecting and that in your data file. You may want to post (copy/paste) the data file if you have further difficulty.





XDATA,YDATA
2,54
5,50
7,45
10,37
14,35
19,25
26,20
31,16
34,18
38,13
45,8
52,11
53,8
60,4
65,6




USE RNLIN_INT
USE UMACH_INT
USE WRRRN_INT
IMPLICIT NONE
INTEGER LDR, NOBS, NPARM
PARAMETER (NOBS=15, NPARM=2, LDR=NPARM)
!
INTEGER IRANK, NOUT
REAL DFE, R(LDR,NPARM), SSE, THETA(NPARM)
EXTERNAL EXAMPL
!
DATA THETA/60.0, -0.03/
!
CALL UMACH (2, NOUT)
!
CALL RNLIN (EXAMPL, THETA, r=r, irank=irank, dfe=dfe, sse=sse)
WRITE (NOUT,*) 'THETA = ', THETA
WRITE (NOUT,*) 'IRANK = ', IRANK, ' DFE = ', DFE, ' SSE = ', &
SSE
CALL WRRRN ('R', R)
END
!
SUBROUTINE EXAMPL (NPARM, THETA, IOPT, IOBS, FRQ, WT, E, DE, &
IEND)
INTEGER NPARM, IOPT, IOBS, IEND, NVARS, NPNTS
REAL THETA(NPARM), FRQ, WT, E, DE(1)
!
INTEGER NOBS, I, J
PARAMETER (NOBS=15)
!
REAL EXP, XDATA(NOBS), YDATA(NOBS)
INTRINSIC EXP
CHARACTER*5 VAR(20)

! DATA YDATA/54.0, 50.0, 45.0, 37.0, 35.0, 25.0, 20.0, 16.0, 18.0, &
! 13.0, 8.0, 11.0, 8.0, 4.0, 6.0/
! DATA XDATA/2.0, 5.0, 7.0, 10.0, 14.0, 19.0, 26.0, 31.0, 34.0, &
! 38.0, 45.0, 52.0, 53.0, 60.0, 65.0/

OPEN (1,FILE= 'test.csv', STATUS='old')

NVARS=2

READ (1,*) (VAR(J),J=1,NVARS)

DO I=1, NOBS
read (1,*) XDATA(I),YDATA(I)
END DO

CLOSE(1)
!
IF (IOBS .LE. NOBS) THEN
WT = 1.0E0
FRQ = 1.0E0
IEND = 0
E = YDATA(IOBS) - THETA(1)*EXP(THETA(2)*XDATA(IOBS))
ELSE
IEND = 1
END IF
RETURN
END




THETA = 58.60651 -3.9586343E-02
IRANK = 2 DFE = 13.00000 SSE = 49.45929

R
1 2
1 1.9 1139.8
2 0.0 1139.3
Press any key to continue . . .


Regards,

brian

brian
03-01-2009, 09:31 AM
Hello Voivod,

Reading data in your subroutine may result in a repeated reading of your file (EXAMPL is called multiple times). You may want to opt. for (critical code in blue):



MODULE mymod
REAL, DIMENSION(:), ALLOCATABLE :: XDATA, YDATA
END MODULE mymod

USE RNLIN_INT
USE UMACH_INT
USE WRRRN_INT
USE mymod
IMPLICIT NONE
INTEGER LDR, NOBS, NPARM, NVARS, NPNTS
PARAMETER (NOBS=15, NPARM=2, LDR=NPARM)
!
INTEGER IRANK, NOUT, I, J
REAL DFE, R(LDR,NPARM), SSE, THETA(NPARM)
! , XDATA(NOBS), YDATA(NOBS) **USE module
CHARACTER*5 VAR(20)
! COMMON /ARGMNT/ XDATA, YDATA **USE module
EXTERNAL EXAMPL
!
ALLOCATE (XDATA(NOBS))
ALLOCATE (YDATA(NOBS))
DATA THETA/60.0, -0.03/

OPEN (1,FILE= 'test.csv', STATUS='old')

NVARS=2

READ (1,*) (VAR(J),J=1,NVARS)

DO I=1, NOBS
read (1,*) XDATA(I),YDATA(I)
END DO

CLOSE(1)
!
CALL UMACH (2, NOUT)
!
CALL RNLIN (EXAMPL, THETA, r=r, irank=irank, dfe=dfe, sse=sse)
WRITE (NOUT,*) 'THETA = ', THETA
WRITE (NOUT,*) 'IRANK = ', IRANK, ' DFE = ', DFE, ' SSE = ', &
SSE
CALL WRRRN ('R', R)
END
!
SUBROUTINE EXAMPL (NPARM, THETA, IOPT, IOBS, FRQ, WT, E, DE, &
IEND)
USE mymod
INTEGER NPARM, IOPT, IOBS, IEND
REAL THETA(NPARM), FRQ, WT, E, DE(1)
!
INTEGER NOBS
PARAMETER (NOBS=15)
!
REAL EXP
! , XDATA(NOBS), YDATA(NOBS) **USE module
INTRINSIC EXP
! COMMON /ARGMNT/ XDATA, YDATA **USE module

! DATA YDATA/54.0, 50.0, 45.0, 37.0, 35.0, 25.0, 20.0, 16.0, 18.0, &
! 13.0, 8.0, 11.0, 8.0, 4.0, 6.0/
! DATA XDATA/2.0, 5.0, 7.0, 10.0, 14.0, 19.0, 26.0, 31.0, 34.0, &
! 38.0, 45.0, 52.0, 53.0, 60.0, 65.0/

IF (IOBS .LE. NOBS) THEN
WT = 1.0E0
FRQ = 1.0E0
IEND = 0
E = YDATA(IOBS) - THETA(1)*EXP(THETA(2)*XDATA(IOBS))
ELSE
IEND = 1
END IF
RETURN
END


Regards,

brian

Voivod
03-01-2009, 04:14 PM
The loop you implemented will likely fix IOBS at 15 for every evaluation

Brian thank you so much for your feedback.You were right. This was the problem.

I wrote the following lines in the EXAMPL subroutine:



open (10, file='exampl_data')

do 1 I=1,NOBS
read (10,*) XDATA(I),YDATA(I)
1 end do

close (10)

A "close" statement was needed for correct compilation of the code.

Many thanks again :)