MATLAB: How to find max and min of fuction of 2 independent variables

MATLABmatlab function

My question is how can I find minimum and maximum of this function, and then tag them with 'o' in function graph?
This is my code so far:
function funkcija(intervalpox,intervalpoy,korak,crtanje)
x=0:korak:intervalpox;
y=0:korak:intervalpoy;
[X,Y] = meshgrid(x,y);
Z = (sin(sqrt(X.^2 + Y.^2)) ./ (sqrt(X.^2 + Y.^2)));
mesh(X,Y,Z)
grid on
xlabel('.x.')
ylabel('.y.')
zlabel('.z.')
title('mesh')

Best Answer

You could use a similar formulation as found here.
What it boils down to is using imregionalmax on your Z matrix to find the local maximums.
MaxVals = find(imregionalmax(Z));
plot3(X(MaxVals),Y(MaxVals),Z(MaxVals),'ro','MarkerSize',30)
Now this will find you your local maximums but to find the minimums you could just flip Z upside down and then run imregionalmax again.
Zupsidedown=-Z;
MinVals = find(imregionalmax(Zupsidedown));
plot3(X(MinVals),Y(MinVals),Z(MinVals),'go','MarkerSize',30)
This will allow you to find the local max and mins across the entire surface.
clear;clc;close all
korak=.1;
intervalpox=10;
intervalpoy=10;
x=korak:korak:intervalpox;
y=korak:korak:intervalpoy;
[X,Y] = meshgrid(x,y);
Z = (sin(sqrt(X.^2 + Y.^2)) ./ (sqrt(X.^2 + Y.^2)));
Zupsidedown=-Z;
MaxVals = find(imregionalmax(Z));
MinVals = find(imregionalmax(Zupsidedown));
plot3(X(MaxVals),Y(MaxVals),Z(MaxVals),'r.','MarkerSize',30)
hold on
plot3(X(MinVals),Y(MinVals),Z(MinVals),'g.','MarkerSize',30)
mesh(X,Y,Z)
grid on
xlabel('.x.')
ylabel('.y.')
zlabel('.z.')
title('mesh')
localmaxmin.jpg