Mesh and Contour not plotting correctly

조회 수: 2 (최근 30일)
Ameen Yar Khan
Ameen Yar Khan 2020년 3월 10일
답변: Star Strider 2020년 3월 10일
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