MATLAB - 3D Contour Plot (Hydrogen 2p Orbitals) - matlab

I've got the following code which works for plotting an x-y slice of the Hydrogen 2pz orbital:
%probability densities
pd_psi_210 = #(r,theta,phi) exp(-r).*(r.^2).*(cos(theta).^2)/(32*pi);
%configuring the range
[x y z] = meshgrid(-10:.1:10,-10:.1:10,-2:.1:2);
[THETA,PHI,R] = cart2sph(x,y,z);
%create array of probability density magnitudes
psi_210_vals = pd_psi_210(R,THETA,PHI);
%plotting
imagesc(psi_210_vals(:,:,1)); %x-y plane
I'd like to plot the 3d contour plot of the orbital. I've tried this (and it doesn't seem to get me what I wanted):
isosurface(psi_210_vals(:,:,:)); %3D contour
How can I get this to work?

You just have to specify the underlying grid, and the level you want. For example:
>> isosurface(-10:.1:10, -10:.1:10, -2:.1:2, psi_210_vals, 0.001);
>> axis equal

Related

Creating meshgrid of scattered Cartesian data for plotting on a sphere

I have a set of n=8000 cartesian coordinates X,Y and Z as vectors and also a vector V of same size which I want to use as values to create a heatmap on a sphere.
I saw this link (visualization of scattered data over a sphere surface MATLAB), but I don't understand how I convert this set of data into a meshgrid for plotting using surf.
Almost every example I saw uses meshgrids.
Right now, I am doing by plotting a sphere and then use scatter3 to plot my points as big balls and try to smooth them later. I looks like this:
I would like to get the figure as the plotting of the example in that link, where he uses:
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
surf(x,y,z,c);
colormap([1 1 0; 0 1 1])
axis equal
EDIT:
(Sorry for taking so long to reply, the corona crises kept away from work)
What I am actually doing is:
for i=1:numel(pop0n)
ori(i,:)=ori(i,:)/norm(ori(i,:));
end
x = ori(:,1);
y = ori(:,2);
z = ori(:,3);
%// plot
m=100;
[aa,bb,cc] = sphere(m);
surf(aa,bb,cc,ones(m+1,m+1)*min(pop0n))
hold on
colormap jet;
scatter3(x,y,z,400,pop0n/norm(pop0n),'filled');
colorbar
shading interp
The array 'ori' is 8000x3, and contains the x, y and z coordinates of the points I want to plot and pop0n is a 8000 sized vector with the intensities of each coordinate.
My main question is how do I transform my x, y, z and pop0n, that are vectors, into 2D arrays (meshgrid) to use surf?
Because I cannot simply do surf(x,y,z,pop0n) if they are vectors.
Thanks in advance
As David suggested, griddata does the job.
What I did was:
for i=1:numel(pop0n)
ori(i,:)=ori(i,:)/norm(ori(i,:));
end
x = ori(:,1);
y = ori(:,2);
z = ori(:,3);
%// plot
m=100;
[aa,bb,cc] = sphere(m);
v = griddata(x,y,z,pop0n,aa,bb,cc,'nearest');
surf(aa,bb,cc,v)
colormap jet;
colorbar
shading interp

plot 2D intensity figure in 3D coordinate system

I have a 2D intensity plot, as in this example:
[xx yy] = meshgrid(0:0.1:1, 0:0.1:1);
figure(1)
imagesc(sin(xx)) %(x,y)-plot at z=0
Now, as I have noted in the comment, this plot is in the xy-plane and I have taken z=0. I'd like to plot this in a 3D coordinate system as a function of x and y, but taking z=0. I tried with plot3 in this way, plot3(xx,yy,cos(yy)), but that only plots lines and gives them a curvature which I am not interested in. I'd like only a plane at z=0.
You can use surf to accomplish this. We use the xx and yy meshgrid outputs as the x and y positisions, a matrix of zeros as the z value and sin(xx) as the color. We also remove the edges by setting the EdgeColor to none.
surf(xx, yy, zeros(size(xx)), sin(xx), 'EdgeColor', 'none')

MATLAB - Isosurface function, irregular coordinates (Input grid is not a valid MESHGRID)

I have a data file from a physics model that shows the density of a certain material around a object. The data file is in spherical coordinates and is reshaped to its original dimensions (Theta,Phi,R and density). Sph2cart is used to transform from spherical coordinates to cartesian (x,y,z) coordinates. To visualize the different densities around the object I found the isosurface function in MATLAB (example: http://www.mathworks.com/matlabcentral/answers/110977-3d-density-plot-multiple-isosurfaces-on-the-same-plot).
This is the code that I currently have:
input = importdata( 'denNa20.dat' );
input(1,:)=[];
theta = reshape(input(:,3),200,20,40);
phi = reshape(pi/2 - input(:,2),200,20,40);
r = reshape(input(:,1),200,20,40);
density = reshape(input(:,4),200,20,40);
[x,y,z] = sph2cart(theta,phi,r);
% This has ofcourse the complete wrong dimensions but then it works
% [x,y,z] = meshgrid(1:1:20,1:1:200,1:1:40);
p = patch(isosurface(y,x,z,density,0.00001));
isonormals(x,y,z,density,p);
set(p,'FaceColor','red','EdgeColor','none','FaceAlpha',0.15);
daspect([1 1 1])
view(3)
grid on
camlight; lighting phong
When I run the code I receive the following errors:
Error using interp3 (line 146)
Input grid is not a valid MESHGRID.
Error in isonormals (line 75)
n(:,1)=interp3(x, y, z, nx, verts(:,1), verts(:,2), verts(:,3));
Error in data_import_plot (line 16)
isonormals(x,y,z,density,p);
If I create my own meshgrid with very easy x,y,z coordinates (check % in code) it works. I don't know what I am doing wrong. I hope some one can help me.
Cheers,
Jeroen
P.S. if you want you can download the data file from this link https://www.dropbox.com/s/msphgmg2oyi91cx/denNa20.dat?dl=0
I found out that the problem was that isosurface does not accept irregular x,y and z coordinates. This is what I found to be the solution. I used scatteredInterpolant to interpolate the density and then created my own meshgrid.
input = importdata( 'denNa20.dat' );
input(1,:)=[];
[x,y,z] = sph2cart(input(:,3),pi/2 - input(:,2),input(:,1));
density = input(:,4);
F = scatteredInterpolant(x,y,z,density);
xRange = linspace(min(x),max(x),75);
yRange = linspace(min(y),max(y),75);
zRange = linspace(min(z),max(z),75);
[x,y,z] = meshgrid(xRange,yRange,zRange);
density = F(x,y,z);
axis([-5000,5000,-5000,5000,-5000,5000])
p = patch(isosurface(x,y,z,density,0.00001));
isonormals(x,y,z,density,p);
set(p,'FaceColor','red','EdgeColor','none','FaceAlpha',0.50);
daspect([1 1 1])
view(3)
grid on
camlight; lighting phong

How to create a 2d color map plot from x and y coordinates and a vector of values?

I'm trying to plot a 2 dimensional signal on a specific plane in a 3d model. I have the matrix:
xyzp (nx3)
that contains all the points which are closest to the plane (e.g. when the plane is in the z direction, all the z coordinates are fairly similar).
and I have a vector:
signal (nx1)
that contains a value for each point in xyzp.
when I use:
"surf([xyzp(:,[1,2]),signal)" or "mesh([xyzp(:,[1,2]),signal])"
The plot I get doesn't look at all like the intersection of the plane with the model from any angle (I expected "view(2)" to show the signal in the Z direction), so I assume I didn't use the plot function correctly.
Can anyone show me an example? For instance - A circle on an xy plane with some random signal indicated by color
surf and mesh can be used when the points form a rectangular grid on the xy plane.
In the general case (points are arbitrarily placed), you can use scatter3. For purposes of illustration, consider the following example xyzp and signal:
[x y] = ndgrid(-1:.01:1);
x = x+.3*y; %// example values which do not form a rectangular grid
z = x+y; %// example z as a function of x, y
xyzp = [x(:) y(:) z(:)];
signal = z(:)+x(:)-y(:); %// example values
Then
scatter3(xyzp(:,1), xyzp(:,2), xyzp(:,3), 1, signal, '.');
produces the following figure.
Since scatter3 plots each point separately, the picture is not as smooth as it would be with surf. But this seems hard to improve if the coordinates do not a have any "structure" (as surf requires) .

How can I plot a 3d vector fast in matlab?

I'm working on a project which includes use of accelerometer and gyroscope to get the orientation an object. I can pass the 3d orientation vector from arduino to matlab via serial communication.
I want to plot the vector in matlab to make real time analysis. I'm using quiver3d and drawnow functions in a loop to plot the vector but quiver3d function is very slow so I can see the orientation of object after like 20 seconds later.
Is there any way to plot 3d vectors faster?
Thanks.
quiver plot may be too much for plotting only one vector in 3-D. You can achieve a similar plot by using a simple plot3 such as the one plotted below.
In this plot, the origin of the vector is the blue dot, and the direction is given by the red line.
The code
%v is the direction of the vector (3 cartesian coordinates)
v = sort(randn(100,3));
v = bsxfun(#rdivide,v,sqrt(sum(v.^2,2)));
%xyz the origin of the vector
ind = linspace(-pi,pi,100);
x = cos(ind);
y = sin(ind);
z = ind;
%the plotting function
figure
for ii = 1:numel(ind)
plot3(x(ii),y(ii),z(ii),'bo'); %origin in blue
set(gca,'XLim', [-3 3], 'YLim', [-3 3], 'ZLim', [-3 3]);
hold on;
hl = plot3( linspace(x(ii), x(ii)+v(ii,1),10), ...
linspace(y(ii), y(ii)+v(ii,2),10), ...
linspace(z(ii), z(ii)+v(ii,3),10), ...
'r'); %direction in red
view(80,10);
pause(0.1);
%clf
end