Maximum of 2D data interpolation
    13 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Albert Zurita
 el 6 de Feb. de 2023
  
    
    
    
    
    Comentada: Albert Zurita
 el 7 de Feb. de 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 comentarios
Respuesta aceptada
  John D'Errico
      
      
 el 6 de Feb. de 2023
        
      Editada: John D'Errico
      
      
 el 6 de Feb. de 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 comentarios
Más respuestas (1)
  Torsten
      
      
 el 6 de Feb. de 2023
        
      Movida: Torsten
      
      
 el 6 de Feb. de 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.
Ver también
Categorías
				Más información sobre Interpolation en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



