Error with plotting function

조회 수: 4 (최근 30일)
Annelotte
Annelotte 2023년 1월 21일
편집: Star Strider 2023년 1월 21일
I want to plot z = (1.5-x1+x1*x2)^2 +(2.25-x1+x1*(x1^2))^2+(2.625-x1+x1*(x2)^3)^2 with -5<x1<5 and -5<x2<5.
I tried:
x1 = -5:5;
x2 = -5:5;
z = (1.5-x1+x1*x2).^2 +(2.25-x1+x1*(x1.^2)).^2+(2.625-x1+x1*(x2).^3).^2;
plot(x1,x2,z);
What am I doing wrong? It gives error: Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.

채택된 답변

Star Strider
Star Strider 2023년 1월 21일
편집: Star Strider 2023년 1월 21일
You will need to use element-wise operators with vector arguments. See Array vs. Matrix Operations for details.
Use surf for the plot.
The fsurf function can also be used plot the resulting surface —
z = @(x1,x2) (1.5-x1+x1.*x2).^2 +(2.25-x1+x1.*(x1.^2))^2+(2.625-x1+x1.*(x2).^3).^2;
x1 = -5:5;
x2 = -5:5;
[X1,X2] = ndgrid(x1,x2);
figure
surf(X1, X2, z(X1,X2))
grid
xlabel('X_1')
ylabel('X_2')
zlabel('z')
syms x1 x2
z = (1.5-x1+x1*x2)^2 +(2.25-x1+x1*(x1^2))^2+(2.625-x1+x1*(x2)^3)^2;
figure
grid
fsurf(z, [-5 5 -5 5])
xlabel('X_1')
ylabel('X_2')
zlabel('z')
EDIT — Corrected typographical errors.
.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Physics에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by