Error with plotting function
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Annelotte
el 21 de En. de 2023
Editada: Star Strider
el 21 de En. de 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 comentarios
Respuesta aceptada
Star Strider
el 21 de En. de 2023
Editada: Star Strider
el 21 de En. de 2023
You will need to use element-wise operators with vector arguments. See Array vs. Matrix Operations for details.
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!