Mark max/min points on a surface plot
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 2 Sep 2018
Zeng Zhi Tee, this is homework, so each student should work out the final code on their own.
Réponses (1)
Walter Roberson
le 2 Sep 2018
Modifié(e) : Walter Roberson
le 2 Sep 2018
maxval = max(Z(:));
i = Z == maxval;
3 commentaires
Walter Roberson
le 2 Sep 2018
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.
Voir également
Catégories
En savoir plus sur Surface and Mesh Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!