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

7 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2019-2-21
[~, 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+');

请先登录,再进行评论。

采纳的回答

Asad Mirza
Asad Mirza 2019-2-20
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 个评论
Faris Hajdarpasic
Faris Hajdarpasic 2019-2-20
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 2019-2-21
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)

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by