How to find (interpolated) zeros in a data array
10 views (last 30 days)
Show older comments
Hey there,
given to me is the following problem:
I have a 8x11 double array filled with measurements. There is no noise
The x-coordinate (amount of columns) represents 11 variations of parameter A
The y-coordinate (amount of columns) represents 8 variations of parameter B
The values in the array then represents the measured values due to the corresponding values of A und B. Due to data policy I'm not allowed to post the original data, but let's asume something similar, like:
data = [10 5 3 -1 -2 -8 -2 -1 3 5 10;
12 6 4 1 -1 -4 -1 1 4 6 12;
20 9 8 7 6 1 6 7 8 9 20;
* further rows only positive numbers*
];
As you can you can imagine, there exists a "U-shaped" graph representing the zeros-points in this array (when interpolate). I hope you understand, what I mean.. In other words: If you consider the data arrays values as a surface in a three-dimensional space, then the cut line with the f=0 -plane would be the mentioned U-shape.
Now my task is, to determine a certain amount (e.g. 100) of A-B-pairs that lead (while interpolating) to those U-shaped graph. Or in other words, determine A-B-pairs that would lead to measurement value =0. Here is, what I tried:
search = 0
c = contourc(vec_A, vec_B, data, [search search])
num_pts = c(2,1);
% Here, the coresponding coordinates are stored
x_pts = c(1, 2:1+num_pts)
y_pts = c(2, 2:1+num_pts)
In gerneral this work's. But it's not as exact as I would like it to be. I did cross-ckecks with interp2() for validation
data_interp = interp2(vec_A, vec_B, data, x_pts(4), y_pts(4) , "makima")
Now I'm out of ideas.. I would be very pleased, if someone could give me a hint or an improovement idea or a code that generates a certain amount of A-B-pairs that results in 0 if I to a check with interp2(). In the best case, it would be possible to define a failture tolerance.
Best regards and have a nice day,
Philipp
0 Comments
Answers (1)
John D'Errico
on 1 Mar 2023
There is no assurance that interp2 and contourc are using identically the same scheme for interpolation. Even in the case of "linear" interpolation, there will be problems in uniquely defining a linear interpolant in 2-dimensions, and that can cause problems. Even worse, if you are checking for an exactly zero result, you need to understand that floating point arithmetic is not accurate to an infinite precision, and therefore the computations can still yield different results within the realm of floating point trash. For example, consider the following test for equality, which surely should result in a mathematical identity, correct?
0.3 -0.1 -0.2 == 0
So why are you surprised that two different codes do not yield identical results?
But as I said above, if the two codes are using a different linear interpolant, then they can produce different results far above any floaing point tolerance.
For example, contouring codes will typically split the rectangular grid into triangles, then interpolate across those triangles. But the linear interpolant in interp2 will be a tensor product "linear" interpolant (sometimes called bilinear interpolation), which is subtly different. Both are linear in some respects, but differently linear. That is because 3 points will determine a plane, but 4 points will determine ... something else.
If you absolutely need "perfection" then you will need to write your own code, so that you can choose the method of interpolation. Even that will not be truly perfect, since a linear interpolant will be at best an approximation. So you might be chasing a mirage here. Is there truly a need for perfection in this matter?
See Also
Categories
Find more on Interpolation 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!