Newton Divided Interpolation Method | FORTRAN 95

Fortran Programming - Bottom Science

NOTE – CREATE the ‘data.txt’ file in your program folder before running the program.

data.txt

100   10.63
150   13.03
200   15.04
250   16.81
300   18.42
350   19.90
400   21.27

Fortran Code

PROGRAM difference_table
IMPLICIT NONE
INTEGER::i,j,n,ierror,k=1
REAL::x(20),y(20),dt(20,20),t,s=0.0,p
OPEN(UNIT=1, FILE='data.txt', STATUS='UNKNOWN', ACTION='READ', IOSTAT=ierror)

PRINT *, "============================================" 
PRINT *, "Program for Newton divided interpolation method - [BY - www.BottomScience.com]" 
PRINT *, "============================================"
PRINT *,'DIFFERENCE TABLE'
PRINT *,'Number of values?'
READ(*,*)n
PRINT *,'Value at which we want calculation?'
READ(*,*)t
PRINT *,'Storing values in x and y arrays....'
READ(1,*)(x(i),y(i),i=1,n)
CLOSE(1)
CALL diff_table(x,y,dt,n)

!PRINTING
DO j=1,n-1
  WRITE(*,*)(dt(j,i),i=1,n-j)
END DO
!VALUE OF FUNCTION 
s=y(k)
DO i=1,n-1
  p=1.0
  DO j=1,i
    p=p*(t-x(j))
  END DO
  s=s+p*dt(k,i)
END DO
PRINT *,"=================================================="
PRINT *,"CALCULATED VALUE OF DIVIDED DIFFERENCE AT",t,"-",s
END PROGRAM
!CALCULATING DIVIDED DIFFERENCES
SUBROUTINE diff_table(x1,y1,dt1,n1)
INTEGER::i1,j1,n1
REAL::x1(20),y1(20),dt1(20,20)
DO j1=1,n1-1
  DO i1=1,n1-j1
    IF(j1==1) THEN
      dt1(i1,j1)=(y1(i1+1)-y1(i1))/(x1(i1+1)-x1(i1))
    ELSE
      dt1(i1,j1)=(dt1(i1+1,j1-1)-dt1(i1,j1-1))/(x1(i1+j1)-x1(i1))
    END IF
  END DO
END DO
RETURN
END SUBROUTINE

Leave a Reply

Your email address will not be published.