-2

This program approximates the roots of the polynomial using the false position method. The program prompts the user to input the coefficients of the polynomial, root approximations, and number of iterations. The code won't compile because of the error.

I don't understand why the line after Do while gives an Unclassifiable statement at (1) error.


      integer aNew,bNew,cNew,dNew,eNew, n
      real funcA, funcB, funcC, X0, X1, X2

      Write(*,*) 'Please enter the coefficients for the polynomial'
      Write(*,*) 'polynomial should be no more than order 4'

      PRINT*, 'Enter values of A,B,C,D,E'
      READ(*,*) aNew,bNew,cNew,dNew,eNew
      Write(*,*) 'Enter initial approximation, '
      Write(*,*) 'the values should be between -5 and 5 '
      Read(*,*) X0,X1
      Write(*,*) 'Enter number of tolerance (number of search iterations) '
      Read(*,*) n

      DO 10 I = 1,n
      funcA = CALL F(aNew,bNew,cNew,dNew,eNew,X1)  
      funcB = CALL F(aNew,bNew,cNew,dNew,eNew,X0)
        X2 = (X0*funcA - X1*funcB)/(funcA - funcB)

      funcC = CALL F(aNew,bNew,cNew,dNew,eNew, X2)
      IF(funcA*funcC .LT. 0.0)THEN
      X0 = X2
      ELSE
      X1 = X2
      END IF
  10  Continue
      Write(*,*) 'Value of the root',X2

      CONTAINS
      FUNCTION F(A,B,C,D,E,x)
      integer A,B,C,D,E
      real F
      F = A*x**4 + B*x**3 + C*x**2 + D*x + E
      END FUNCTION F


      END Program
francescalus
  • 30,576
  • 16
  • 61
  • 96
KAZ999
  • 1
  • 1
  • 2
    What `DO WHILE`? Please [edit] the question such that the code posted matches the question. – John Alexiou Oct 06 '22 at 13:00
  • 1
    Always use **`implicit none`**. It is really important. Even inside the funsions and subroutines, if they are external like here. – Vladimir F Героям слава Oct 06 '22 at 13:09
  • And also give complete context for the error message of the compiler. (For example, if you are mistakenly saying `do while` when you really mean the `do 10 I = 1,n`, it'd be obvious to us what you mean instead of wondering, as John Alexiou points out, whether you're simply showing us the wrong version of your code.) – francescalus Oct 06 '22 at 13:36

1 Answers1

1

There were a few errors in your code.

  1. Missing type declaration for i. Edit the following line to

    integer aNew,bNew,cNew,dNew,eNew, i, n
    
  2. Incorrect calling of a function F(). Remove the call keyword

    funcA = F(aNew,bNew,cNew,dNew,eNew,X1)
    
  3. Missing declaration for x in F(). Edit the following line

    real F,x
    

The following are not errors, but best practices

  1. Do not rely on implicit typing for variable declarations. Add the implicit none keyword under each program and each module.

  2. Replace the do/continue loop with do/end do

  3. Add intent(in) keywords for dummy arguments where applicable. Also declare function return type before function declaration, or using a result statement.

    real FUNCTION F(A,B,C,D,E,x)
    integer, intent(in) :: A,B,C,D,E
    real, intent(in) :: x
    F = A*x**4 + B*x**3 + C*x**2 + D*x + E
    END FUNCTION F
    

Here is the full code with the above corrections

program Console1
implicit none

integer aNew,bNew,cNew,dNew,eNew, i, n
real funcA, funcB, funcC, X0, X1, X2

Write(*,*) 'Please enter the coefficients for the polynomial'
Write(*,*) 'polynomial should be no more than order 4'

PRINT*, 'Enter values of A,B,C,D,E'
READ(*,*) aNew,bNew,cNew,dNew,eNew
Write(*,*) 'Enter initial approximation, '
Write(*,*) 'the values should be between -5 and 5 '
Read(*,*) X0,X1
Write(*,*) 'Enter number of tolerance (number of search iterations) '
Read(*,*) n

DO I = 1,n
    funcA = F(aNew,bNew,cNew,dNew,eNew,X1)
    funcB = F(aNew,bNew,cNew,dNew,eNew,X0)
    X2 = (X0*funcA - X1*funcB)/(funcA - funcB)

    funcC = F(aNew,bNew,cNew,dNew,eNew, X2)
    IF(funcA*funcC .LT. 0.0)THEN
        X0 = X2
    ELSE
        X1 = X2
    END IF
END DO
Write(*,*) 'Value of the root',X2

CONTAINS
real FUNCTION F(A,B,C,D,E,x)
integer, intent(in) :: A,B,C,D,E
real, intent(in) :: x
F = A*x**4 + B*x**3 + C*x**2 + D*x + E
END FUNCTION F
end program Console1

Note that because the function F() is defined within the program scope, and after the contains keyword, it does not need an interface block definition. If you move the function to another file (not inside a module) then you would might need to define an interface block with the function header.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • implicit typing = bad idea. – John Alexiou Oct 06 '22 at 13:29
  • Such a function (the one in the `contains`) doesn't *need* an interface block even it is outside of a `contains` and outside of a `module`, because the argument list is compatible with legacy F77. It is just a good practice to write an interface block, not a requirement. – PierU Oct 06 '22 at 13:36
  • @PierU - related post https://stackoverflow.com/questions/31630535/why-should-i-use-interfaces – John Alexiou Oct 06 '22 at 14:23
  • Unless I am missing something, this post doesn't say that interfaces are always *required* – PierU Oct 06 '22 at 14:54
  • @PierU they are not required if you use the `external` keyword, F77 style. But for use inside a module it is required (I think). Anyway, my last comment is beyond the point of the answer. It was just an observation. – John Alexiou Oct 06 '22 at 15:05
  • *"But for use inside a module it is required (I think)"* : no, it isn't. Regarding `external`, I am not sure it is required either (in practice it is not, but it could be a long standing compiler tolerance) – PierU Oct 06 '22 at 15:33