Mesh and Contour not plotting correctly

2 次查看(过去 30 天)
This is in regards to F2 in the code below. I graphed the meshplot on wolfram as well as saw how one of my classmates did it. Their's look the same however mine looks different. Not sure why F2 isn't being plotted correctly. It should have a different shape with two minima at the center. MATLAB output is shown first (screen snip) along with the Wolfram alpha output (assumed correct).
Any help/suggestion is greatly appreciated.
x1 = -10:0.1:10;
x2 = -10:0.1:10;
[X1,X2] = meshgrid(x1,x2);
F1 = (X1.^2 + X2.^2 + (4*X1) - (2*X2));
F2 = ((5*X1.^2) + (X1.^4) - (9*(X1.^2)*X2) + 3*(X2.^2) + (2*X2.^4) + (0.25*X1));
subplot(2,2,1); mesh(X1,X2,F1); title('F_1');
subplot(2,2,2); mesh(X1,X2,F2); title('F_2');
subplot(2,2,3); contour(X1,X2,F1); title('F1 Contour');
subplot(2,2,4); contour(X1,X2,F2); title('F2 Contour');

回答(1 个)

Star Strider
Star Strider 2020-3-10
I slightly changed your code to make it a bit less confusing (creating ‘x’ and ‘y’ and ‘X’ and ‘Y’):
x = -10:0.1:10;
y = -10:0.1:10;
[X,Y] = meshgrid(x,y);
There is one error. You need to do element-wise multiplication:
F2 = ((5*X.^2) + (X.^4) - (9*(X.^2).*Y) + 3*(Y.^2) + (2*Y.^4) + (0.25*X));
↑ ← HERE
They now appear to be the same.

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by