# How to plot a surface of vector z in a 2D grid made of vectors x and y?

3 ビュー (過去 30 日間)
Ivan Verstraeten 2019 年 11 月 30 日
Edited: Ivan Verstraeten 2019 年 12 月 1 日
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 件のコメント

KALYAN ACHARJYA 2019 年 11 月 30 日
Both, there are 2D or 3D vectors and all matrices are vectors.
Star Strider 2019 年 11 月 30 日
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’.
Ivan Verstraeten 2019 年 11 月 30 日
I'm trying to plot the eigenvectors of a particle in a 2D potential well; the eigenvectors of the hamiltonian 'H'.

サインイン to comment.

### 件の回答 (1)

Star Strider 2019 年 11 月 30 日
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 件のコメント

Ivan Verstraeten 2019 年 12 月 1 日
It's in the alley but I only want to plot one columnvector of 'V'. It should (I'm hoping) be a surface in 3D space. Something like the image below (for V(:,99)). (This is not made with the code I posted before ofcourse) Star Strider 2019 年 12 月 1 日
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.)
Ivan Verstraeten 2019 年 12 月 1 日
Here is some background:
But only read it if you have the time for it and/or really want to :) , because I think my problem can be narrowed down to 'plotting a surface of a column vector' (if it's possible with what I have). I was actually following the answer to basically the same question:https://nl.mathworks.com/matlabcentral/answers/412639-creating-surface-plot-from-a-matrix-with-3-columns
But the 'reshape' command gives the error: 'To RESHAPE the number of elements must not change'.

サインイン to comment.

サインイン してこの質問に回答します。