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

7 visualizaciones (últimos 30 días)
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 comentarios
Faris Hajdarpasic
Faris Hajdarpasic el 20 de Feb. de 2019
Editada: Faris Hajdarpasic el 20 de Feb. de 2019
But what if i want it on plot3?
Walter Roberson
Walter Roberson el 21 de Feb. de 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+');

Iniciar sesión para comentar.

Respuesta aceptada

Asad Mirza
Asad Mirza el 20 de Feb. de 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 comentarios
Faris Hajdarpasic
Faris Hajdarpasic el 20 de Feb. de 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 el 21 de Feb. de 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)

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by