Azzera filtri
Azzera filtri

Error with plotting function

2 visualizzazioni (ultimi 30 giorni)
Annelotte
Annelotte il 21 Gen 2023
Modificato: Star Strider il 21 Gen 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 '.*'.

Risposta accettata

Star Strider
Star Strider il 21 Gen 2023
Modificato: Star Strider il 21 Gen 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.
.

Più risposte (0)

Categorie

Scopri di più su Physics in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by