How to plot a surface of vector z in a 2D grid made of vectors x and y?
21 views (last 30 days)
Show older comments
I have a vector z which consists of function values defined on a 2D plane. I do not know the function though. I have tried interpolation with griddata and reshape to get z into a matrix but nothing seems to work. Perhaps I'm doing something fundamentally wrong so that's it's impossible to get the result I want? Help is much appreciated!
N = 100; % Amount of grid points
L = 1; % System extends from -L/2 to L/2
h = L/(N-1); % Grid step size
x = h*(0:N-1) - L/2; % Coordinates of grid points
y=x;
coeff = -1/((pi)^2*h^2) ;
coeff = ones(100,1)*coeff;
B=[coeff coeff -4*coeff coeff coeff];
H=spdiags(B,[-3 -1 0 1 3],N,N);
[V,D]=eigs(H,N);
z=V(:,100);
[X,Y] = meshgrid(x,y) ;
Z = reshape(z,size(X)) ;
figure
surf(X,Y,Z)
EDIT: As far as I know my problem lied in the construction of the matrix 'H'. It does not yield eigenvectors that are a lengthy enough to define a surface over the entire grid. For this you need a rank 3 tensor I think. Though I use the kronecker tensor product 'kron' to get all the same spacial information into a second order tensor. This way the eigenvectors are of correct size to 'reshape' them over the grid.
%---Set up the Hamiltonian operator matrix
coeff = -h_bar^2/(2*mass*h^2) ;
I=speye(N,N);
o=ones(N,1);
t=-2*ones(N,1);
D=spdiags([o t o] ,[-1 0 1],N,N);
H=kron(D,I).*coeff+kron(I,D).*coeff;
5 Comments
Star Strider
on 30 Nov 2019
My pleasure!
It does contain information in 3D, however the ‘z’ vector is not randomly distributed with respect to the ‘x’ and ‘y’ coordinates, as griddata requires.
I am not certain what you are doing. I am not certain you can create a surface from ‘z’.
Answers (1)
Star Strider
on 30 Nov 2019
I am not certain what you want the plots to look like, since I am not at all familiar with what you are doing.
Try this:
figure
mesh(X,Y,D)
figure
mesh(X,Y,V)
See if one of those does what you want.
3 Comments
Star Strider
on 1 Dec 2019
The problem is that ‘V(:,99)’ would only plot one column vector (as you noted), not a surface. You are doing something that iis vary far from my areas of expertise, so if you have an paper or online reference explaning (in very basic terms) what you are doing, I might be able to help. (Otherwise, I will eventualy delete my Answer as not actually being one, and hope that someone who understands what you are doing sees this and responds.)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!