Mark max/min points on a surface plot

17 ビュー (過去 30 日間)
Xinyi Liu
Xinyi Liu 2018 年 9 月 2 日
コメント済み: Xinyi Liu 2018 年 9 月 2 日
I'm asked to create a surface plot of the following function
z=(4x^2+y^2+x-1)e^(-x^2-y^2)
The function z has 2 maximum points and 1 minimum point. How to mark these points on the graph??Waiting for your responses!
My current code:
[X,Y] = meshgrid(-1.5:0.02:1.5,-1.5:0.02:1.5);
Z = (4.*X.^2+Y.^2+X-1).*exp(-X.^2-Y.^2);
hold on
[~,i] = max(z(:));
h = scatter3(x(i),y(i),z(i),'filled');
h.SizeData = 150;
[~,i] = min(z(:));
h = scatter3(x(i),y(i),z(i),'filled');
h.SizeData = 150;
hold off
>> figure
>> surf(X,Y,Z)
But it turns out that I cannot find two maximum points and the locations found using these codes are incorrect.
Please help!!!
  2 件のコメント
Zeng Zhi Tee
Zeng Zhi Tee 2018 年 9 月 2 日
so the final code is what?
Walter Roberson
Walter Roberson 2018 年 9 月 2 日
Zeng Zhi Tee, this is homework, so each student should work out the final code on their own.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 9 月 2 日
編集済み: Walter Roberson 2018 年 9 月 2 日
maxval = max(Z(:));
i = Z == maxval;
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 9 月 2 日
Yes, there are two local maxima. The code I posted assumed that when you posted about two maxima that it was due to there being two locations with the same global maxima.
To find the local minima, examine Z and observe that it is symmetric with respect to Y since Y only appears in the form of Y^2. Therefore the extrema are going to occur at Y = 0. So substitute Y = 0 into Z. Then you can differentiate the result, and solve. It will be a cubic times an exponential term that you are solving, so the solution is the roots of the cubic. You can use root() for that to get the exact roots. Then you would examine the Z at the X vector value on other side of the true root in order to find the quantized location that has the maxima.
Xinyi Liu
Xinyi Liu 2018 年 9 月 2 日
I see.Thank you!I have figured out how to approach the question.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by