bilinear extrapolation based on interp2

조회 수: 40 (최근 30일)
SA-W
SA-W 2023년 6월 16일
댓글: Matt J 2023년 6월 20일
[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
Torsten
Torsten 2023년 6월 16일
편집: Torsten 2023년 6월 16일
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 2023년 6월 16일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2023년 6월 16일
편집: Matt J 2023년 6월 16일
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
SA-W
SA-W 2023년 6월 20일
How would you test them? With finite differences?
Matt J
Matt J 2023년 6월 20일
yes, with finite differences.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2023년 6월 16일
편집: John D'Errico 2023년 6월 16일
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
SA-W
SA-W 2023년 6월 16일
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 2023년 6월 20일
편집: John D'Errico 2023년 6월 20일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by