Mark max/min points on a surface plot

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 Comments

so the final code is what?
Zeng Zhi Tee, this is homework, so each student should work out the final code on their own.

Sign in to comment.

Answers (1)

maxval = max(Z(:));
i = Z == maxval;

3 Comments

Notice that the questions indicate two maximum points. Why are there two?Local maximum?
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.
I see.Thank you!I have figured out how to approach the question.

Sign in to comment.

Asked:

on 2 Sep 2018

Commented:

on 2 Sep 2018

Community Treasure Hunt

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

Start Hunting!