0

I have to develop a linear interpolation program, but keep getting these errors. Here is the source code:

!Interpolation program for exercise 1 of portfolio
PROGRAM interpolation
IMPLICIT NONE
!Specify table 1 for test of function linter
REAL, DIMENSION (5):: x,f
!Specify results for table 1 at intervals of 1
REAL, DIMENSION (10):: xd, fd
!Specify table 2 to gain linter results 
REAL, DIMENSION (9):: xx,ff
!Specify results for table 2 of at intervals of 0.25
REAL, DIMENSION (36):: xxd, ffd

INTEGER :: i, j

!Write values for table dimensions

!Enter x values for Table 1
x(1)=-4.0
x(2)=-2.0
x(3)=0.0
x(4)=2.0
x(5)=4.0
f(1)=28.0
f(2)=11.0
f(3)=2.0
f(4)=1.0
f(5)=8.0
xd(1)=-4.0
xd(2)=-3.0
xd(3)=-1.0
xd(4)=0.0
xd(5)=1.0
xd(6)=2.0
xd(7)=3.0
xd(9)=4.0

!Print Table 1 Array
PRINT *,"Entered Table Values are", x,f
PRINT *,"Interpolation Results for Table 1", xd, fd

END PROGRAM

SUBROUTINE interpol(x,f, xd,fd)
DO i=1, 5
    DO j=1, 5
        IF (x(j) <  xd(i) .AND. xd(i) <=  x(j+1)) THEN
        fd=linterp (x(j),x(j+1),f(j))
        END IF
    END DO 
END DO
END SUBROUTINE interpol


!Linear Interpolation function
FUNCTION linterp(x(i),x(i+1),f(i),f(i+1),x)
    linterp=f(i)+((x-x(i))/(x(i+1)-x(i)))*(f(i+1)-f(i))
END FUNCTION

With it giving these errors;

lin.f90:55:18: Error: Expected formal argument list in function definition at (1)
lin.f90:56:19:

  linterp=f(i)+((x-x(i))/(x(i+1)-x(i)))*(f(i+1)-f(i))
                   1
Error: Expected a right parenthesis in expression at (1)
lin.f90:57:3:

 END FUNCTION
   1
Error: Expecting END PROGRAM statement at (1)

Could anyone please point me in the right direction?

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • 2
    You have a [shortage of `implicit none`](http://stackoverflow.com/questions/24337413/where-to-put-implicit-none-in-fortran) (as does Alexander Vogt's answer): you may have a surprise with `linterp` returning an integer value. – francescalus Aug 03 '15 at 15:38

2 Answers2

2

It is exactly what the compiler complains about: you are missing a right parenthesis.

Either remove the superfluous left (:

linterp=f(i)+ ( x-x(i) ) / ( x(i+1)-x(i) )* ( f(i+1)-f(i) )

or add another )

linterp=f(i)+ ( (x-x(i)) ) / ( x(i+1)-x(i) )* ( f(i+1)-f(i) )

Note that I removed another miss-placed ) in the middle part.


Apart from that, your function declaration is broken! You cannot have x(i) in the declaration!

Try:

real FUNCTION linterp(xI,xIp1,fI,fIp1,x)
    implicit none
    real, intent(in)    :: xI,xIp1,fI,fIp1,x
    linterp = fI + (x-xI)/(xIp1-xI)*(fIp1-fI)
END FUNCTION

Alternatively, you can provide the whole arrays (including its length N) and the current index:

real FUNCTION linterp(x,f,N,i,xx)
    implicit none
    integer, intent(in) :: N
    real, intent(in)    :: x(N), f(N), xx
    integer, intent(in) :: i
    linterp = f(i) + (xx-x(i))/( x(i+1)-x(i) )*( f(i+1)-f(i) )
END FUNCTION
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
0

In addition to everything Alexander said. You also need to make sure that you have the same amount of inputs in your function declaration as you do when you call it:

fd=linterp (x(j),x(j+1),f(j))

has two less inputs than in your function declaration:

FUNCTION linterp(x(i),x(i+1),f(i),f(i+1),x)

Also, don't forget to add an index to fd, either i or j:

fd(i)=linterp (x(j),x(j+1),f(j))

otherwise you're replacing the entire array with the linterp result every time.

crockski
  • 21
  • 1