フィルターのクリア

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

6 ビュー (過去 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 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by