Hi all. I have a Lagrange program that uses a 4-point data set to interpolate some non-tabulated value
in between any of those data points. I want to extend this script to act on a large input file, call
it file1.dat, where the data is arranged in two columns Xin Yin. Then the script should interpolate
for a range of set values Xnew to get Ynew (i.e. a new output data file file2.dat with two columns
Xnew Ynew). I need to use this to investigate the convergence of some simulations which I run for two
different resolutions whereby the input gridpoints do not coincide for those two resolutions. So, what
I want to do is to interpolate the higher resolution data (file1.dat) onto the lower resolution one so
that it can be possible to plot some quantity from the two different runs on the same graph and study
clearly its convergence.
in between any of those data points. I want to extend this script to act on a large input file, call
it file1.dat, where the data is arranged in two columns Xin Yin. Then the script should interpolate
for a range of set values Xnew to get Ynew (i.e. a new output data file file2.dat with two columns
Xnew Ynew). I need to use this to investigate the convergence of some simulations which I run for two
different resolutions whereby the input gridpoints do not coincide for those two resolutions. So, what
I want to do is to interpolate the higher resolution data (file1.dat) onto the lower resolution one so
that it can be possible to plot some quantity from the two different runs on the same graph and study
clearly its convergence.
Code:
program Lagrange_int
! A program that approximates a value of a function/experimental quantity at a point based
! on a given set of data
implicit none
integer, parameter :: n = 4 ! number of data points:length of each data vector
integer :: i, j
double precision, dimension(n) :: xvect, yvect! vectors of known data: x,y-values
double precision, dimension(n) :: L ! Lagrange cardinal function
double precision :: z, Pz ! the set evaluation point "z" and the value of the
! ploynomial/function/quantity at that point
! Initializations of z, Pz and L
z = 3.0d0 ! the point of evaluation
Pz = 0.0d0 ! initializing the polynomia value at z
L = 1.0d0 ! initalizing the vector of cardinal functions to 1
! The data
data (xvect(i), i=1,n)/3.2d0,2.7d0,1.0d0,4.8d0/
data (yvect(i), i=1,n)/22.d0,17.8d0,14.2d0,38.3d0/
! Performing the interpolation
do i = 1,n
do j = 1, n
if (i /= j) then
L(i) = ( (z - xvect(j)) / (xvect(i) - xvect(j)) )* L(i) ! part of L(i)
end if
end do
Pz = Pz + L(i)*yvect(i) ! update Pz ~ f(z)
end do
write(*,*) "The approximate value of y(",z,")=", Pz
end program Lagrange_int