I’m attempting to calculate a specific y-value from a natural cubic spline given an x-value, using the natural spline form from http://mathworld.wolfram.com/CubicSpline.html and coded in Java at http://www.cse.unsw.edu.au/~lambert/splines/natcubic.html.
Unfortunately this spline form takes a t-value on the range from 0->1 for each segment of the spline in order to compute x and y coordinates along each segment. I’d like to input a given x-coordinate and get the corresponding y-coordinate, but i’m not sure how to get a t-value from my x-coordinate. I.e., the coefficients of the spline are stored as y=f(t) and x=f(t), but i’d like to get y=f(x). Short of creating a look-up table, are there any exact solutions to get y=f(x) from this form of spline?
I’ve tried other implementations that take an x-value and return a y-value (e.g., apache commons math, Flanagan’s scientific library), but the spline version from the websites above appear to behave favorably in my application.
Thanks.
What you want is not always possible.
There can be a single segment of the spline, which passes through two different points for a single value of x.
If you really need it as a function of x, you can solve the cubic equation X(t) = x0 for t (using for example Cardano‘s formulas) and then substitute the value of t in Y(t).