Z must be a matrix, not a scalar or vector.

26 visualizaciones (últimos 30 días)
Alexandra Roxana
Alexandra Roxana el 1 de Mzo. de 2023
Comentada: Star Strider el 21 de Mzo. de 2023
I want to have a 3D plot with a solution vector but I have the error that Z must be a matrix.
a=0;
b=4;
c=0;
d=6;
g=1;
dxs=0.2;
dxf=0.25;
dy=0.5;
NNAB=(2*g)/dxs+(b-a-2*g)/dxf-1;
NNAD=(d-c)/dy+1;
NTN=NNAD*NNAB;
sol=rand(3*NTN-4*NNAB,1);
omega2=sol(2*NTN-2*NNAB+1:3*NTN-4*NNAB);
figure(2)
[X,Y]=meshgrid(a:b,c:d);
surf(X,Y,omega2)
Error using surf
Z must be a matrix, not a scalar or vector.
  2 comentarios
Rik
Rik el 1 de Mzo. de 2023
What exactly is your question?
size(omega2)
ans = 1×2
187 1
To use surf, the final variable should contain a height for each index of X and Y. This is clearly not the case. Why exactly did you pick these functions?
Cameron
Cameron el 1 de Mzo. de 2023
Look at your X,Y, and omega2 variables. The size of all of them should be the same when using the surf function.

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 1 de Mzo. de 2023
It would be helpful to have the actual data rather than a ‘proxy problem’. The ‘Z’ vector should have the dame number of elements as the matrices. If so, it is then straightforward to interpolate them to form matrices, demonstrated here.
a=0;
b=4;
c=0;
d=6;
g=1;
dxs=0.2;
dxf=0.25;
dy=0.5;
NNAB=(2*g)/dxs+(b-a-2*g)/dxf-1;
NNAD=(d-c)/dy+1;
NTN=NNAD*NNAB;
sol=rand(3*NTN-4*NNAB,1);
omega2=sol(2*NTN-2*NNAB+1:3*NTN-4*NNAB);
omega2 = omega2(randperm(numel(omega2),35)); % Random Subset With The Same Number Of Elements As The Matrices
figure(2)
[X,Y]=meshgrid(a:b,c:d);
F = scatteredInterpolant(X(:),Y(:),omega2); % Create 'scatteredInterpolant' Object
omega2 = F(X,Y); % Interpolate
surf(X,Y,omega2)
colormap(turbo)
.
  25 comentarios
Alexandra Roxana
Alexandra Roxana el 21 de Mzo. de 2023
No problem, it was because of your idea of creating a matrix for plotting that I could come to this.
Thank you for your patience and time!
Star Strider
Star Strider el 21 de Mzo. de 2023
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by