# Error with plotting function

2 Ansichten (letzte 30 Tage)
Annelotte am 21 Jan. 2023
Bearbeitet: Star Strider am 21 Jan. 2023
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 '.*'.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Star Strider am 21 Jan. 2023
Bearbeitet: Star Strider am 21 Jan. 2023
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 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Physics finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by