HELP. Need to understand MATH behind scatteredInterpolant or Interp2 function
Mostrar comentarios más antiguos
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 comentarios
Cris LaPierre
el 12 de Feb. de 2021
Editada: Cris LaPierre
el 15 de Feb. de 2021
The scatteredInterpolant documentation page contains an Algorithm section. You are welcome to find and read the referenced paper.
Gregory Nixon
el 15 de Feb. de 2021
Walter Roberson
el 15 de Feb. de 2021
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
John D'Errico
el 15 de Feb. de 2021
Editada: John D'Errico
el 15 de Feb. de 2021
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.
Gregory Nixon
el 15 de Feb. de 2021
Editada: Gregory Nixon
el 15 de Feb. de 2021
Bjorn Gustavsson
el 15 de Feb. de 2021
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...
Gregory Nixon
el 15 de Feb. de 2021
Editada: Gregory Nixon
el 15 de Feb. de 2021
Bjorn Gustavsson
el 15 de Feb. de 2021
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?
Mario Malic
el 15 de Feb. de 2021
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)
Bruno Luong
el 16 de Feb. de 2021
Editada: Bruno Luong
el 16 de Feb. de 2021
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.
Gregory Nixon
el 16 de Feb. de 2021
Editada: Gregory Nixon
el 16 de Feb. de 2021
Respuesta aceptada
Más respuestas (2)
Steven Lord
el 15 de Feb. de 2021
0 votos
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 comentarios
Gregory Nixon
el 16 de Feb. de 2021
Editada: Gregory Nixon
el 16 de Feb. de 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])
Bruno Luong
el 17 de Feb. de 2021
Editada: Bruno Luong
el 17 de Feb. de 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 comentarios
Gregory Nixon
el 17 de Feb. de 2021
Editada: Gregory Nixon
el 17 de Feb. de 2021
Bjorn Gustavsson
el 17 de Feb. de 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.
Categorías
Más información sobre Spline Postprocessing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
