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

조회 수: 7 (최근 30일)
Faris Hajdarpasic
Faris Hajdarpasic 2019년 2월 20일
댓글: Walter Roberson 2019년 2월 21일
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
Faris Hajdarpasic
Faris Hajdarpasic 2019년 2월 20일
편집: Faris Hajdarpasic 2019년 2월 20일
But what if i want it on plot3?
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