| New Reply |
Fortran: Data file problem |
Share Thread |
| Sep20-11, 06:58 AM | #1 |
|
|
Fortran: Data file problem
Hello guys. I want to save the numbers between 0 and 1 with 0.01 increasement to a dat file. I wrote a code for this using Fortran however, after a point I get 0.314999 instead of 0.315000. Can you help me to solve my problem?
<code> REAL, DIMENSION(:), ALLOCATABLE :: Y REAL STEP, JEND, PX, GR, RE, RATIO INTEGER N, I, J JEND = 1.00 STEP = 1E-3 N = JEND/STEP+1 RE=1 GR=1 RATIO=GR/RE PX=-12.00 ALLOCATE (Y(N)) Y(1) = 0.0 OPEN(UNIT=8, FILE='HEATLINE.DAT', STATUS='OLD', FORM='FORMATTED') DO I = 2, N Y(I) = Y(I-1)+STEP END DO PRINT *, 'N IS:' , N PRINT *, 'I IS:',I 53 format('VARIABLES = "Y"') WRITE(8,53) do i=1,n write(8,*) Y(I) end do CLOSE(8) END </code> |
| Sep20-11, 09:15 AM | #2 |
|
Recognitions:
|
If you want to increment Y by 0.01 each time, why is STEP = 1E-3?
|
| Sep20-11, 09:20 AM | #3 |
|
|
i.e: if RE is a real and you want to declare it try RE=(1.0) instead of RE=1 ( 1 is an integer so may affect the operation ) Use real numbers when dealing with real numbers, I can humbly suggest. Good Luck ! |
| Sep20-11, 11:11 AM | #4 |
|
|
Fortran: Data file problem |
| Sep20-11, 02:42 PM | #5 |
Recognitions:
|
I assume you already know that floating point values stored in real variables are almost never exact. If you set STEP = 1E-3, the value will not be exactly 0.001, though it should be accurate to about 6 or 7 significant figures.
The problem with your code is that you are adding STEP to itself a very large number of times, so these small errors accumulate. If you add STEP to itself 10 times, instead of 6 or 7 signifincat digits of accuracy you will only have about 5 or 6 digits. If you add it to itself 100 times, the accuracy reduces to 4 or 5 digits. For 1000 times, 3 or 4 digits, etc. The best way to avoid this problem is to calculate each value of Y independently of the others. Instead of Code:
Y(1) = 0.0 do I = 2,N Y(I) = Y(I-1) + STEP end do Code:
do i = 1,n Y(i) = (i-1)*STEP enddo |
| Sep20-11, 02:49 PM | #6 |
Recognitions:
|
|
| New Reply |
| Tags |
| data, error, fortran |
Similar discussions for: Fortran: Data file problem
|
||||
| Thread | Forum | Replies | ||
| Fortran: Read data from a line in a file | Programming & Comp Sci | 1 | ||
| Fortran 90, problem with reading a file | Programming & Comp Sci | 9 | ||
| using a batch file to create NAMELIST input file for FORTRAN | Programming & Comp Sci | 0 | ||
| Fortran locate symbol in data file | Programming & Comp Sci | 1 | ||
| Find peak in the historgram data file... - FORTRAN! | Programming & Comp Sci | 1 | ||