HELP. Need to understand MATH behind scatteredInterpolant or Interp2 function

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

The scatteredInterpolant documentation page contains an Algorithm section. You are welcome to find and read the referenced paper.
Thank you Cris :). I've been reading that paper, however I was wondering if someone could point me to the source code that holds the equations and lines of code that are executed when the function is ran?
That way I don't have to do extra work to figure out whats happening in the backend.
I was wondering if someone could point me to the source code that holds the equations and lines of code that are executed when the function is ran
scatteredInterpolant is a built-in function (also griddedInterpolant, which underlies interp2), and MathWorks does not give out source code for built-in functions, unless, of course, you manage to get a job at The MathWorks.
Perhaps if you give SPECIFIC case where this happens, we can explain what is the difference.
okay ill give an example on something I've seen.
for data i have : azimuths, elevations, and values that correspond to those values
I'm interpolating what those values are in MATLAB and in another modeling tool and then finding the percent error between the two models calculations.
For the target of interest this is what I'm seeing:
MATLAB (MB) , Other Tool (AM)
Azimuth: -66.9421328918023 deg (MB), -66.9412 deg (AM)
Elevation: -6.34628091719976 deg (MB), -6.30099 deg (AM)
Interpolation Values: -38.5560589527745 (MB), -17.6123 (AM)
Let me know if you guys need more information.
We definitely need the values of your input-data as well. These are only one data-point where you aren't even using exactly the same inputs to the two interpolations. You'll have to use something that are guaranteed to be numerically identical inputs, and give us enough information to compare. If you for example are using the spline or cubic interpolation methods it is plausible that they are implemented differently (with respect to continuity of derivatives etc) which might lead to different results. But until you give us inputs to test we can only guess...
What exactly should i provide? i'm industry and and i cant provide my input data
OK here's an answer given the level of information you have provided:
For two points separated by 0.00093289 degrees in azimuth and 0.045291 degrees in elevation it is perfectly plausible that the interpolated values for an otherwise completely arbitrary surface differ by 20.944, that might after all be two points in a region with an unusually shapr gradient.
See this question from the perspective of those that are supposed to answer - what information have we gotten, what information would be the minimum information for us to give an informed advice?
Take a look at their default interpolation options, maybe they differ which can lead to this (big?) difference.
Secondly the query points do differ, is this a big difference? Can you set up the same grid for both softwares and then try to find (if there's any) common options for interpolation and then do the comparison.
Azimuth: -66.9421328918023 deg (MB), -66.9412 deg (AM)
Elevation: -6.34628091719976 deg (MB), -6.30099 deg (AM)
What method you are using?
If you use nearest, the result is the function value of the seed of the Voronoi cell where the query belong to.
If you use linear it's a linear function that interpolate the Delaunay triangle where the query point belong to.
If you use the natural, it's a convex sum of function values of the vertexes "augmented" adjacent Delaunay cells (insert query point to the data points) where the weights are given by eqt (28) of the paper https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.8.6623&rep=rep1&type=pdf
There we goes those are the "equations". The equations are straighforward once the geometry of Delaunay/Voronoi diagram is computed. The later is harder to compute but their definition is quite natural and intuitive.
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

"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

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)

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

Hey Steven Ive attched my data above
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.

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

Sorry Bruno I work for Industry so I have to be very careful on how I'm asking for help. Thank you for helping me. I greatly appreciate it. And yes your correct linear method is being used in both models.
i wish I could copy all the code used and i know its a headache I'm sorry. I'm just thankful you've taken the time to help me.
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?
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.

Categories

Community Treasure Hunt

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

Start Hunting!