Maximum of 2D data interpolation
16 views (last 30 days)
Show older comments
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...
0 Comments
Accepted Answer
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])
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])
To within the tolerances used by fminsearch, this should be a solution.
xymin/pi
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.
4 Comments
More Answers (1)
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.
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!