bilinear extrapolation based on interp2

50 views (last 30 days)
SA-W
SA-W on 16 Jun 2023
Commented: Matt J on 20 Jun 2023
[X,Y] = meshgrid(0:10);
Z = X.^2 + Y.^2;
[Xq,Yq] = meshgrid(0:0.25,10);
V = interp2(X,Y,Z,Xq,Vq,'linear');
I want to use interp2 to (bi)linearly interpolate the function Z(x,y) in the interior of the grid.
Although I have to take extrapolating with a pinch of salt, I want to come up with a bilinear extrapolation in case I have to evaluate outside the grid.
Say is an evaluation point outside the predefined grid, and is its closest point on the boundary. Then, the extrapoland is given by
where is the value of Z at t, similarly for the partial derivatives.
I would compute the derivative of the extrapoland as
Is this gradient correct? I am uncertain about that since t, the closest point on the boundary, is also a function of (x,y). But I do not know how would one differentiate that.
  12 Comments
Torsten
Torsten on 16 Jun 2023
Edited: Torsten on 16 Jun 2023
But I think the issue is that Z(t_x,t_y), dZ/dx(t_x,t_y), and dZ/dy(t_x,t_y) are itself all functions of x and y because t_x,t_y depend on x and y.
No. It's a Taylor approximation of Z_extrap(x,y) of first-order with center point (t_x,t_y), the point nearest to (x,y).
SA-W
SA-W on 16 Jun 2023
If I go from (x,y) to (x+ deltaX,y), then t_x and t_y may be different, hence also Z(t_x,t_y), dZ/dx(t_x,t_y), and dZ/dy(t_x,t_y) change with x in a discontinuous manner.
But I do not know. Let's see what MattJ can say about that.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 16 Jun 2023
Edited: Matt J on 16 Jun 2023
Although you've been advised against it, if you truly must extrapolate bilinearly then griddedInterpolant will do it for you,
[X,Y] = deal(0:10);
[Xq,Yq] = deal(0:0.25,10);
Z = X.^2 + (Y').^2;
F=griddedInterpolant({X,Y}, Z,'linear','linear');
V=F({Xq,Yq})
  36 Comments
SA-W
SA-W on 20 Jun 2023
How would you test them? With finite differences?
Matt J
Matt J on 20 Jun 2023
yes, with finite differences.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 16 Jun 2023
Edited: John D'Errico on 16 Jun 2023
Ugh. This is just a bad idea, Extrapolation in general is a bad thing to do. A better word is probably excrapolation. You will get exactly what you should expect from the name I just made up. There will be difficulties in trying to extrapolate, because if you try to extrapolate one cell, it is not going to be consistent with the extrapolant from the cell immediately next to it.
It is FAR better to use a tool that can at least try to intelligently extrapolate, using the shape of the surface near the boundary. In this case, I'll suggest my inpaint_nans tool, as found on the file exchange.
A = NaN(100,100);
A(30:70,30:70) = sin((X+Y)/5);
Ahat = inpaint_nans(A);
surf(Ahat)
hold on
H = surf(A);
H.FaceColor = 'r';
I doubt you can do too much better than that. It is actually not too bad, considering the extent of the extrapolation. (Yes, one of the things on my lengthy list of round-tuits is a better version of inpaint_nans for problems like this. But inpaint_nans is nearly 20 years old, and I fleshed out the idea for that improvement 20 years ago. Sigh.)
  2 Comments
SA-W
SA-W on 16 Jun 2023
Thank you!
I am aware of your nice FEX tool inpaint_nans. But as stated in the comments under the question, I need both function values and the first derivatives of the extrapoland which I can not easily do with your tool.
So I think I have to come up with an extrapolation scheme that gives me more control.
I think I provided one of the simplest extrapolation scheme in my question.
John D'Errico
John D'Errico on 20 Jun 2023
Edited: John D'Errico on 20 Jun 2023
The derivatives that you compute will not be very good, in the sense that they are themselves just based on local finite differences. And then you base the extrapolant on those local derivatives. Do you see this is a bad idea?
I'll strongly argue that you FIRST perform the extrapolation using a good tool like inpaint_nans. and THEN you can compute a derivative, from the results. This is better because inpaint_nans implicitly uses that same derivative information around the perimeter to infer the shape of the extrapolated surface. But it also uses all of that information at once, to infer a SMOOTHLY behaved extrapolant.
You are trying to solve the problem from the wrong direction. Hey, it is your choice of course. Of course, if you already know how to solve the problem, then why did you ask this question in the first place?

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!