Mark max/min points on a surface plot
19 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
2018-9-2
编辑:Walter Roberson
2018-9-2
maxval = max(Z(:));
i = Z == maxval;
3 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!