How to find max and min of fuction of 2 independent variables?

3 views (last 30 days)
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')
  8 Comments
Walter Roberson
Walter Roberson on 21 Feb 2019
[~, location_of_max] = max(Z(:));
[~, location_of_min] = min(Z(:));
x_at_max = X(location_of_max);
y_at_max = Y(location_of_max);
z_at_max = Z(location_of_max);
x_at_min = X(location_of_min);
y_at_min = Y(location_of_min);
z_at_min = Z(location_of_min)
plot3(x_at_max, y_at_max, z_at_max, 'go', x_at_min, y_at_min, z_at_min,'r+');

Sign in to comment.

Accepted Answer

Asad Mirza
Asad Mirza on 20 Feb 2019
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
  6 Comments
Faris Hajdarpasic
Faris Hajdarpasic on 20 Feb 2019
One more question. There is multiple red and green dots. And what if I want only one maximum and only one minimum(the highest/lowest one). How to achieve that?
Asad Mirza
Asad Mirza on 21 Feb 2019
That's just using max() and min() on the resulting vector output of imregionalmax:
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));
[~, ZGlobalMaxInd]=max(Z(MaxVals));
MinVals = find(imregionalmax(Zupsidedown));
[~, ZGlobalMinInd]=min(Zupsidedown(MinVals));
% plot3(X(MaxVals),Y(MaxVals),Z(MaxVals),'r.','MarkerSize',30)
% hold on
% plot3(X(MinVals),Y(MinVals),Z(MinVals),'g.','MarkerSize',30)
plot3(X(ZGlobalMaxInd),Y(ZGlobalMaxInd),Z(ZGlobalMaxInd),'r.','MarkerSize',30)
hold on
plot3(X(ZGlobalMinInd),Y(ZGlobalMinInd),Z(ZGlobalMinInd),'g.','MarkerSize',30)
mesh(X,Y,Z)
grid on
xlabel('.x.')
ylabel('.y.')
zlabel('.z.')
title('mesh')
view(45,12)

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing 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!