Cheng Cosine <asecant@gmail.com> wrote in message <3e0c9d186f2c44689e7aa1ef12744b8c@37g2000yqm.googlegroups.com>...
> Hi:
>
> Suppose y is a function of x, and we have a set of data points from
> experiment.
>
> How do we determine the value of x_target corresponding to a target
> value of y_target? If we know the function form of y = f(x), and then
> we can use rootfinding algorithms such as Newton method. But now we
> only have discrete data points.
IF the underlying functional relationship has a single value
of y for any value of x, then just swap x and y, then use
interp1.
If not, then you can use Doug Schwarz's intersections code
from the FEX to do this nicely. It will find the multiple
solutions.
If you wish to fit the data, doing some smoothing, then you
can use my SLM tools to do the fitting (also on the file
exchange.) slmeval can solve for the inverse that you wish
to find.
Note that sometimes, swapping x and y as I have suggested
will result in not that terribly good of an interpolant. For
example,
x = 0:.2:1;
fun = @(x) x.^2;
y = fun(x);
See that fitting this curve with a spline, in the direction y(x)
yields a very accurate fit. In fact, it will be exact, to within
floating point noise.
spl = spline(x,y);
xev = 0:0.0001:1;
std(ppval(spl,xev)  fun(xev))
ans =
3.55121582665278e17
But, suppose that we invert the relationship? How well does
a spline fit?
splinv = spline(y,x);
yev =0:0.0001:1;
funinv = @(y) sqrt(y);
std(ppval(splinv,yev)  funinv(yev))
ans =
0.0125954702997721
It turns out that the inverse relationship is not well represented
by a spline near x == 0, since there is a derivative singularity
at that location.
HTH,
John
