# Maximum of 2D data interpolation

7 views (last 30 days)
Albert Zurita on 6 Feb 2023
Commented: Albert Zurita on 7 Feb 2023
Hi, I have 2D data for which I know there is a single maximum, but the points spacing may not be ideal so the maximum may lie in between. Therefore I would like to use griddedInterpolant to find the maximum point. X and Y contain the points, Z the corresponding function value.
It should look something as:
F = griddedInterpolant(x_mat,y_mat,z_mat);
xmaxs = arrayfun(@(xx,yy)fminsearch(@(x,y)-F(x,y),xx,yy),[x_mat(ii) y_mat(ii)]);
Where I provide an initial guess ii. But this is not the right way of calling it for a 2D function I'm afraid...

John D'Errico on 6 Feb 2023
Edited: John D'Errico on 6 Feb 2023
You will NEED to use a spline based method of interpolation here, specifically spline or cubic. NOT makima, or pchip. Those interpolants will not generate functions with an extremum between the data points. And of course you certainly cannot use the default linear interpolant, as that will NEVER generate a solution that is not at a data point.
[X,Y] = ndgrid(0:0.5:6);
Z = cos(X+Y).*sin(X-Y);
G = griddedInterpolant(X,Y,Z,'spline');
fsurf(@(x,y) G(x,y),[0 6 0 6])
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments. We can clearly see several peaks. Of course, since I used a trig function here, they will lie at transcendental locations, so never exactly at a grid point.
[xymin,fval] = fminsearch(@(xy) -G(xy(1),xy(2)),[1,1])
xymin = 1×2
0.7930 2.3606
fval = -0.9945
To within the tolerances used by fminsearch, this should be a solution.
xymin/pi
ans = 1×2
0.2524 0.7514
And that would clearly be the point at [pi/4,3*pi/4]. If you want a better solution you need a finer grid spacing, as the spline has limited approximative capabilities with that course grid.
Albert Zurita on 7 Feb 2023
I am thinking that given the fact that I'm looking at very precise maximum determination the problem is very likely related with the tolerance options to the fminsearch function. Changing the default value of 1e-4 to a lower value seems to provide better results!

Torsten on 6 Feb 2023
Moved: Torsten on 6 Feb 2023
That doesn't make sense.
F is only an approximating function between the grid points - you don't know if the "real" underlying function behaves like this.
If you cannot supply the "real" function for F, better just choose the z with maximum value and the corresponding x and y.
Albert Zurita on 6 Feb 2023
Sorry. I rely on the grid points and the corresponding z values, so that f(x,y) = z is known and well sampled. However even if I can sample denser and do max(z) I would like to find (xi,yi) interplated points which would correspond to a better approximation/interpolation of the maximum. I found some answers in 1D and thought it could be extrapolated to 2D.

### Categories

Find more on Interpolation in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!