MATLAB Answers

HELP. Need to understand MATH behind scatteredInterpolant or Interp2 function

157 views (last 30 days)
Hey all! So I need to understand the math behind how MATLAB calculates interpolated vaulses. I repeat MATH because I know how to use the funtions with ease and I understand the vargin, however my goal is to compare MATLABs equtions with another model I'm wokring in.
The other model im working is giving me an interpolated answer of -17 while MATLAB is producing a value of -38 with the same inputs in both models. I understand how the other model is interpolating. But when you step into intrep2 or scatteredInterpolant you are taken to the green comments. ARG. lol.
So i repeat community I need help with the findining the equations and making sense of them :).
interp_val = f0 * f10 - f9 * blah blah blah. Something of that nature guys/ladies.
Please help someone.
  11 Comments
Gregory Nixon
Gregory Nixon on 16 Feb 2021
Hey all I've attached a sample of data i think will be useful
You'll see what i mean as to why im confused based on those points why one model is giving me somethign so drastic

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 17 Feb 2021
"Based on the equation that I had in that screen shot would you assume that the other model is using a simplified linear interpolation model?"
Can't assume anything. The equation lacks of explanation. What are f00, f01, f10, f11, r1 r2 ?
It looks like bilinear formula, but it might hide a bug dependeing how those quantities are defined, and it's odd that bilnear can be applied on a non-parallelogram shape.
  1 Comment
Gregory Nixon
Gregory Nixon on 18 Feb 2021
Bruno you saved the day. Thank you everyone. We bascially learned that the two models simply use different method which explains the difference. Becasue of your code we were able to graphically show the diffence.

Sign in to comment.

More Answers (2)

Steven Lord
Steven Lord on 15 Feb 2021
If you believe scatteredInterpolant is computing the wrong answer but cannot share the data with the community, please send your call to scatteredInterpolant along with the data necessary to execute that call and a description of why you believe its answer is incorrect (such as the results from a different interpolation routine) to Technical Support for investigation. You can contact Support using the Contact Support link on the Support section of this website.
  2 Comments
Steven Lord
Steven Lord on 17 Feb 2021
Can you show us the exact line of code that you run that uses that data to generate the answer around -38.55?
Just doing a little sanity check, your X value of -66.94 is closer to -67.65 than to -65.93 so you'd expect the interpolated value to be closer to the values in the first column of "MATLAB Table". Similarly your Y value of -6.34 is closer to -5.77 than to -7.49 and so the interpolated value should probably be closer to -56.349 than to the other three data values. A -38.55 doesn't seem like an unreasonable answer given this rough argument.
x = [-67.65351209, -65.93466751];
y = [-7.493951899, -5.775107322];
text(x(1), y(1), '-26.1915')
text(x(1), y(2), '-56.3497')
text(x(2), y(1), '-13.1290')
text(x(2), y(2), '-14.2824')
hold on
plot(-66.94213289, -6.346280917, 'rx')
axis([-68, -65, -8, -5.5])

Sign in to comment.


Bruno Luong
Bruno Luong on 17 Feb 2021
Edited: Bruno Luong on 17 Feb 2021
Run this, that show the "formula" and how to get
zq =
-38.5561
zq_check =
-38.5561
Code to check 'linear' method (you seem to not bother to answer my question about the method):
x = [-67.65351209, -65.93466751];
y = [-7.493951899;
-5.775107322];
[X,Y] = meshgrid(x,y);
Z = [-26.19150131, -13.12900262;
-56.3497907, -14.28238121];
xq = -66.94213289;
yq = -6.346280917;
f = scatteredInterpolant(X(:), Y(:), Z(:));
zq = f(xq,yq)
% The Delaunay triangulation showz (xq,yq) belong to triangle of points 2/3/4
M = [X([2 3 4]);
Y([2 3 4]);
[1 1 1]];
w234 = M \ [xq; yq; 1]; % barycentric coordinate
zq_check = Z([2 3 4])*w234 % interpolation value
close all
T = delaunay(X,Y);
trimesh(T,X,Y,Z,'EdgeColor','k');
hold on
for i=1:numel(X)
text(X(i),Y(i),Z(i),num2str(i));
end
plot3(xq, yq, zq, '+r', 'Linewidth', 3, 'Markersize', 20)
text(xq, yq, zq, 'Query point', 'Color', 'r');
PS: Next time please attach data point and code and not only screen capture to avoid us to enter the data by hand.
  2 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 17 Feb 2021
Try and see what you get. This will give you some insight into what is going on:
[x,y] = meshgrid(0:1);
z = [3 2;0 3];
[xi,yi] = meshgrid(0:.1:1);
zi = interp2(x,y,z,xi,yi);
f = scatteredInterpolant(x(:), y(:), z(:));
zq = f(xi,yi);
sph1 = subplot(1,2,1);
surf(xi,yi,zq)
sph2 = subplot(1,2,2);
surf(xi,yi,zi)
linkprop([sph1,sph2],'view')
Then you can rotate the surfaces around, repeat the procedure in your other analysis environments.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by