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 CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by