Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Code resulting infinity values

Henry26
Beginner
754 Views

Hello, everyone.

Please, could somebody help-me with the code below? It is resulting in infinity value.

 

            PROGRAM MR
!============================================================
!DECLARACAO DAS VARIAVEIS
!============================================================
            IMPLICIT NONE
            REAL, EXTERNAL::F
            INTEGER, PARAMETER :: N=6, NPC=4
            INTEGER I, J
            REAL :: X(0:N),Y(0:N),A(0:6),B(0:6),HP,XP(0:NPC),P(0:NPC)

            OPEN(UNIT=20, FILE="FUNCA.txt", STATUS="UNKNOWN")
!============================================================
!DADOS DA FUNCAO INTERPOLADA
!============================================================
            PRINT*, "DADOS DA FUNCAO"
            DO I=0, N
                X=X(I)+1
                Y(I)=F(X)
                WRITE(20,*)X(I),Y(I)
                WRITE(*,*)I,X(I),Y(I)
            END DO
!============================================================
!COEFICIENTES DAS FUNCOES INTERPOLADAS
!============================================================
            PRINT*, "COEFICIENTE DAS FUNCOES"
            DO I=0, N-1
                A(I)=(Y(I+1)-Y(I))/(X(I+1)-X(I))
                B(I)=(Y(I)*X(I+1)-(I)*Y(I+1))/(X(I+1)-X(I))
            WRITE (*,*)I,A(I),B(I)
            END DO

            END PROGRAM MR
!============================================================
!FUNCAO
!============================================================
            REAL FUNCTION F(X)
            IMPLICIT NONE
            REAL*4::x
            F = SIN(X/2)
            END

 

 

The results is:

 

 

 DADOS DA FUNCAO
           0   1.00000000      0.479425550
           1   2.00000000      0.841470957
           2   3.00000000      0.997494996
           3   4.00000000      0.909297407
           4   5.00000000      0.598472118
           5   6.00000000      0.141120002
           6   7.00000000     -0.350783229
 COEFICIENTE DAS FUNCOES
           0         Infinity         Infinity
           1         Infinity         Infinity
           2        -Infinity         Infinity
           3        -Infinity         Infinity
           4        -Infinity         Infinity
           5        -Infinity         Infinity

 

 Thank you!!

0 Kudos
1 Solution
jimdempseyatthecove
Honored Contributor III
724 Views

Would the following represent what you intend?

            DO I=0, N
!**change**     X=X(I)+1
                X(I)=I+1
!**change**     Y(I)=F(X)
                Y(I)=F(X(I))
                WRITE(20,*)X(I),Y(I)
                WRITE(*,*)I,X(I),Y(I)
            END DO

View solution in original post

0 Kudos
6 Replies
JohnNichols
Valued Contributor III
742 Views

You  need to fix this error first 

Screenshot 2022-10-20 133512.png

I am usign the latest compiler IFX and IFORT both complain. 

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
736 Views

 

            PRINT*, "DADOS DA FUNCAO"
            DO I=0, N
                X=X(I)+1

 

Two issues with the above:

1) At the point of execution of line 3 (line 17 of posted code), the array X was(is) undefined.

2) The statement X=X(I)+1, takes the scalar variable result of the expression "X(I)+1", and assigns it to the entire array X(0:N). In this case X(0:N) receives "undefined value at X(0) +1", and repeats this for "X(1)+1", ... "X(N)+1". And at the end of the loop, "X(N)+1". Note, the entire array of X changes at each iteration (each iteration using undefined values including values defined using undefined values).

 

EDIT: Strike (1) the above, apparently the code snip above is missing lines that defined the values of X(0:9).

Point (2) may still be of importance.

 

Jim Dempsey

0 Kudos
Henry26
Beginner
735 Views

Thanks you for the answer @jimdempseyatthecove 

In this case, how could I fix it? I just need to make an array from 0-N and after use the values of this array to input in a function

0 Kudos
jimdempseyatthecove
Honored Contributor III
725 Views

Would the following represent what you intend?

            DO I=0, N
!**change**     X=X(I)+1
                X(I)=I+1
!**change**     Y(I)=F(X)
                Y(I)=F(X(I))
                WRITE(20,*)X(I),Y(I)
                WRITE(*,*)I,X(I),Y(I)
            END DO
0 Kudos
jimdempseyatthecove
Honored Contributor III
724 Views

Note, the second change is what JohnNichols pointed out as an error to line 18,

X is a rank-1 array

F(X) is a rank-1 array of SIN's of the array X.

This cannot be assigned to a scalar.

 

Jim Dempsey

0 Kudos
Henry26
Beginner
719 Views
0 Kudos
Reply