How can I implement 2-dimensional interpolation in FORTRAN where the data looks like as shown below.
x and y are two co ordinates and z is a value dependent on them
x is spaced evenly but y is not uniformly spaced and the maximum value of y
corresponding to uniform values of x keep on increasing.
Without losing much of accuracy-
- What is the simplest method to obtain a value of z based on a given x
and y?
- What is the fastest method to obtain a value of z based on a
given x and y?
Thanks
SM
x y z
-----------
0 0 -
0 0.014 -
0 0.02 -
.....
....
0.1 0 -
0.1 0.02 -
0.1 0.03 -
.......
.....
1.0 0 -
1.0 0.05 -
1.0 0.08 -
.......
.......
I am going to assume you have already read your data into an array N x 3, following the format you gave. I am assuming you don't know ahead of time what the X spacing is - you definitely don't know the Y spacing as it varies. Thus I would recommend the following strategy:
- Figure out the X spacing: start at the first row, and go through the X elements until you see a change in value. You now know XSTART and XSTEP - you will need those later.
- Do a binary search in your array for value X, until you find a value XFOUND such that XFOUND < X < XFOUND + XSTART
- Assuming you are pointing "somewhere in the list", you find the corresponding Y value - depending on whether it's bigger or smaller than the value you need, you step up or down the array until you find the first entry < Y. The corresponding values are X11, Y11, Z11. The next line in the array has X12 Y12 and Z12.
- You need two more points before you can do the interpolation - repeat this process, looking for the "next larger value of X". This will give you XYZ21 and XYZ22
- Now you can think about calculating the interpolated Z value. In general there are different techniques, with different accuracies:
- "Nearest neighbor": find the closest point, and use its value of Z (simplest, least accurate)
- "linear interpolation": find the three closest points, and do a linear interpolation of values based on the relative distances
- "higher order estimates": to do this, you typically need to create a full connectivity mapping of the grid points, so you can do spline interpolation and get a smooth interpolation that will typically be more accurate at points between the grid points (assuming that the function being described by the samples is in fact a smooth function!)
My FORTRAN is a bit rusty - hope this is some help.
PS - potentially a simpler approach is to use the fact that the X values are already evenly spaced. This allows you to make a better interpolation. See this picture: