I'm writing a simulation in FORTRAN 77, in which I need to get a parameter value from some experimental data. The data comes from an internet database, so I have downloaded it in advance, but there is no simple mathematical model that can be used to provide values on a continous scale - I only have discrete data points. However, I will need to know this parameter for any value on the x axis, and not only the discrete ones I have from the database.
To simplify, you could say that I know the value of f(x)
for all integer values of x
, and need a way to find f(x)
for any real x
value (never outside the smallest or largest x
I have knowledge of).
My idea was to take the data and make a linear interpolation, to be able to fetch a parameter value; in pseudo-code:
double xd = largest_data_x_lower_than(x)
double slope = (f(xd+dx)-f(xd))/dx // dx is the distance between two x values
double xtra = x-xd
double fofx = f(xd)+slope*xtra
To implement this, I need some kind of lookup for the data points. I could make the lookup for xd
easy by getting values from the database for all integer x
, so that xd = int(x)
and dx = 1
, but I still have no idea how to implement the lookup for f(xd)
.
What would be a good way to implement this?
The value will be fetched something like 10^7 to 10^9 times during one simulation run, so performance is critical. In other words, reading from IO each time I need a value for f(xd)
is not an option.
I currently have the data points in a text file with one pair of (tab-delimited) x,f(x)
on each line, so bonus points for a solution that also provides a smooth way of getting the data from there into whatever shape it needs to be.