简体   繁体   中英

Get X from Y with MATLAB Spline

I have two vectors in MATLAB, X and Y.
Y is a function of X but X is not a function of Y.

I would like to get an X value from the spline(X,Y) for a given Y value.

How can I do this? I tried using the pp form the spline but I didn't get very far with that.

You say yourself that x is not a function of y. This means there are multiple solutions to the problem, for at least SOME values of y.

You have several choices, depending on your facility with the pp form used in MATLAB. All choices will work best if you first...

Use a bracketing scheme to identify an interval where a root exists. If the spline is non-monotonic over a given interval, you may need to get tricky here, but it is not difficult to find the location where a cubic polynomial attains its maximum/minimum value, since that requires no more than finding the roots of a quadratic polynomial.

Once you have an interval for the solution:

  1. You can use fzero, finding a solution to (f(x)-y). While fzero does not need a bracket to work most of the time, if may fail to find a solution in bad cases. And if you provide a bracket, then fzero will find the solution you choose if there are multiple solutions. (In that event, I set a rule dictated by the customer, that I would choose always the right-most or left-most solution.)

  2. You can use roots on the chosen pp segment. This will be of course faster than using fzero, and considerably more accurate. Of course, roots will return multiple solutions, so you need to choose the correct root. Some of those solutions may be complex numbers to weed out.

  3. You can use the explicit solution for the roots of a cubic polynomial. It will return multiple solutions of course again, so you will need to be careful which solution you take.

The last solution is the fastest, IF you write the code carefully. In fact, it can be vectorized to solve for all points at once. (Having done it, I will assert that it can be done. Ok, actually, it was a programming assignment to a co-op student who worked for me at the time.)

My guess as to why you failed to use the pp form properly is you did not understand how the pp form works. This is of course essential to using these tools properly.

Try plotting a single pp segment of a spline. Use ppval to evaluate it. Does it not seem to work properly at first? This means you misunderstand that these segments are left shifted, so that the left hand end-point of ANY interval is always assumed to be zero. This makes the cubic polynomial segment more accurately evaluable, depending on the break points of the spline. For example, a spline that has breaks [0 1 2 3] and a spline that has breaks at [1000001 1000001 1000002 1000003] should work the same way, IF the function values are identical. See that this will be a numerical problem in floating point arithmetic when you cube numbers that large otherwise.

Again, I will even assert that all of this can be carefully vectorized, although that took some mental effort when we did it. The simplest solution overall is to use fzero, as I described it.

You could use lsqnonlin .

Make the fun argument a function which evaluates the spline, and subtract out the desired Y value so that the result will be zero when the desired Y value is achieved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM