3

I have a function V that is computed from two inputs (X,Y). Since the computation is quite demanding I just perform it on a grid of points and would like to rely on 2d linear interpolation. I now want to inverse that function for fixed Y. So basically my starting point is:

X = [1,2,3];
Y = [1,2,3];
V =[3,4,5;6,7,8;9,10,11];

Is is of course easy to obtain V at any combination of (X,Y), for instance:

Vq = interp2(X,Y,V,1.8,2.5)

gives

Vq =

    8.3000

But how would I find X for given V and Y using 2d linear interploation? I will have to perform this task a lot of times, so I would need a quick and easy to implement solution.

Thank you for your help, your effort is highly appreciated.

P.

  • 3
    Is your "function" ``V = F(X,Y)``" injective? I.e. is there for given V_q only one pair (X_q, Y_q) which yields ``F(X_q, Y_q) == V_q``? Otherwise it will be quite impossible to get the correct pair. What happens for V_q which are larger than ``max(V)`` and for V_q which are smaller than ``min(V)``? Anyway: one hack-approach: evaulate your data on a (very) fine grid. Then find the closest distance ``min(abs(V_fine_grid-Vq))``, the desired value will be close to that position in your matrix. – Nras Oct 24 '14 at 14:07

2 Answers2

1

EDIT using additional info

If not both x and y have to be found, but one of them is given, this problem reduces to finding a minimum in only 1 direction (i.e. in x-direction). A simple approach is formulating this in a problem which can be minizmied by an optimization routine such as fminsearch. Therefore we define the function f which returns the difference between the value Vq and the result of the interpolation. We try to find the x which minimizes this difference, after we give an intial guess x0. Depending on this initial guess the result will be what we are looking for:

% Which x value to choose if yq and Vq are fixed?
xq = 1.8; % // <-- this one is to be found
yq = 2.5; % // (given)
Vq = interp2(X,Y,V,xq,yq); % // 8.3 (given)

% this function will be minimized (difference between Vq and the result
% of the interpolation)
f = @(x) abs(Vq-interp2(X, Y, V, x, yq));
x0 = 1; % initial guess)
x_opt = fminsearch(f, x0) % // solution found: 1.8
Nras
  • 4,251
  • 3
  • 25
  • 37
  • Thanks but it seems here we try to find both x and y given a fixed Z. I would only need to find x, Given both, Z and Y. Some more Info you might find helpful: Given Y the Z is (not monotonically) increasing in X. Z is always in [0,1]. – InfiniteVariance Oct 24 '14 at 16:20
  • @user3575894 your problem simplifies greatly then and can be solved by the last codeblock i wrote. Have a look! – Nras Oct 27 '14 at 08:39
  • Thanks you very much, I solved this differently, please have a look below, I would be very happy to discuss this with you. – InfiniteVariance Oct 30 '14 at 09:26
0

Nras, thank you very much. I did something else in the meantime:

function [G_inv] = G_inverse (lambda,U,grid_G_inverse,range_x,range_lambda)



for t = 1:size(U,1)

        for i = 1:size(U,2) 

            xf = linspace(range_x(1), range_x(end),10000);
            [Xf,Yf] = meshgrid(xf,lambda);
            grid_fine = interp2(range_x,range_lambda,grid_G_inverse',Xf,Yf);
            idx = find (abs(grid_fine-U(t,i))== min(min(abs(grid_fine-U(t,i))))); % find min distance point and take x index
            G_inv(t,i)=xf(idx(1));

        end

end

G_inv is supposed to contain x, U is yq in the above example and grid_G_inverse contains Vq. range_x and range_lambda are the corresponding vectors for the grid axis. What do you think about this solution, also compared to yours? I would guess mine is faster but less accurate. Spped is, however, a major issue in my code.

  • I am not sure. Why do you now use different variable names? The first 3 lines in your inner loop can be moved outside of both loops (``grid_fine`` doesn't change within the loop). Also i think both loops can be avoided, check the edits in my answer for a non-loop-fine-grid approach. Are the results the same (i.e. almost the same)? As for speed: i don't know. Did you check for speed? – Nras Oct 30 '14 at 09:37