I have a function expressed in spherical coordinates:
f(r,theta,phi) = 4*exp(-r)*cos(theta)*sin(phi)
I'd like to plot this in MATLAB in these ways:
R3
R2 Contour Plot (x-y plane or x-z plane or y-z plane)
Is there a straightforward way to do this?
Just do the conversion and plot in Cartesian coordiantes:
f = #(r, theta, phi) 4*exp(-r).*cos(theta).*sin(phi)
[XX YY ZZ] = meshgrid(x_range, y_range, z_range)
% R = sqrt(XX.^2 + YY.^2 + ZZ.^2)
% Th = acos(XX./YY)
% Phi = acos(ZZ./R)
% This is faster. . . and significantly more correct. See the comments below.
[Th,Phi,R] = cart2sph(XX,YY,ZZ)
fvals = f(R, Th, Phi)
I like isosurface to visualize 3D data like this. For the 2D slice through Z=0 you could use imagesc(fvals(:,:,N)) or contour(fvals(:,:,N))
You can use sph2cart() to convert the coordinates, then use plot()/plot3() to plot the function.
Related
I am stuck with an apparently simple problem. I have to revolve of 360° a 2D curve around an axis, to obtain a 3D plot. Say, I want to do it with this sine function:
z = sin(r);
theta = 0:pi/20:2*pi;
xx = bsxfun(#times,r',cos(theta));
yy = bsxfun(#times,r',sin(theta));
zz = repmat(z',1,length(theta));
surf(xx,yy,zz)
axis equal
I now want to visualize the numerical values of the Z plane, stored in a matrix. I would normally do it this way:
ch=get(gca,'children')
X=get(ch,'Xdata')
Y=get(ch,'Ydata')
Z=get(ch,'Zdata')
If I visualize Z with
imagesc(Z)
I don't obtain the actual values of Z of the plot, but the "un-revolved" projection. I suspect that this is related to the way I generate the curve, and from the fact I don't have a function of the type
zz = f(xx,yy)
Is there any way I can obtain the grid values of xx and yy, as well as the values of zz at each gridpoint?
Thank you for your help.
Instead of bsxfun you can use meshgrid:
% The two parameters needed for the parametric equation
h = linspace(0,2) ;
th = 0:pi/20:2*pi ;
[R,T] = meshgrid(h,th) ;
% The parametric equation
% f(x) Rotation along Z
% ↓ ↓
X = sin(R) .* cos(T) ;
Y = sin(R) .* sin(T) ;
% Z = h
Z = R ;
surf(X,Y,Z,'EdgeColor',"none")
xlabel('X')
ylabel('Y')
zlabel('Z')
Which produce:
And if you want to extract the contour on the X plane (X = 0) you can use contour:
contour(Y,Z,X,[0,0])
Which produce:
I have the equations of two surfaces and I want to plot both of them and highlight their intersection
My code looks like this
t=linspace(-1,1,100);
x=t;
y=t;
z=cos(t.^8+12);
plot3(x,y,z,'g-','linewidth',3)
hold on
[x,y]=meshgrid(-2:2,-2:2);
surf(x,y,z)
And that gives me the plot for the surface z = f(x,y) but I can not figure out how to plot the plane x=y
Subject to editing upon further question details.
Here might be an interesting starting point. Below is a script that uses the patch() function to create the rectangle plane x = y. The patch function takes corner coordinates of a rectangle to plot in the 3D case (x,y,z) coordinates. Unfortunately, I was unable to get the surface plot from the above code. So the intersection discussion is a story for another time.
Function Call:
patch(X_Coordinates,Y_Coordinates,Z_Coordinates,Colour);
%***************************************************%
%3D line plot%
%***************************************************%
x = linspace(-1,1,100);
y = linspace(-1,1,100);
z = cos(x.^8+12);
plot3(x,y,z,'g-','linewidth',3)
%***************************************************%
%3D surface plot%
%***************************************************%
hold on
[x,y] = meshgrid(-2:0.01:2,-2:0.01:2);
z = cos(x.^8+12);
% surf(x,y,z);
%***************************************************%
%Plotting the xy-plane%
%***************************************************%
Plane_Top = 255;
Plane_Bottom = -255;
Plane_Width = 4;
%Using plane attributes to set patch points%
X = [-Plane_Width/2 -Plane_Width/2 Plane_Width/2 Plane_Width/2];
Y = [-Plane_Width/2 -Plane_Width/2 Plane_Width/2 Plane_Width/2];
Z = [Plane_Bottom Plane_Top Plane_Top Plane_Bottom];
%Plotting characteristics%
Colour = [252/255 148/255 3/255];
patch(X,Y,Z,Colour);
view(3);
grid on;
xlabel("X-Axis"); ylabel("Y-Axis");
title("Plotting the XY-Plane and Line Function");
Using MATLAB version: R2019b
I have a problem and maybe you will be able to help me. Like in the title i have cross section data of a symmetric lens - coordinates s=-100:1:100 and height y - and I would like to create 3D plot the whole lens (x,y,z). Is there any build in function that helps with that? Thanks for help in advance!
If I'm understanding correctly, you have a 1-D array that you'd effectively like to 'sweep' around a circle to produce a 3-D plot. Here is an example of how to do that
% Some dummy data
Npts = 100;
z = sin(linspace(0, pi, Npts));
Nreps = 100; % How many times to repeat around circle
% Create polar meshgrid and convert to Cartesian
[r, theta] = meshgrid( ...
linspace(-length(z)/2, length(z)/2, Npts), ...
linspace(0, pi, Nreps));
[X, Y] = pol2cart(theta, r);
% Copy data Nreps times
Z = repmat(z, Nreps, 1);
% Plot!
surf(X, Y, Z)
Without more specs (such as if your y is a 2D matrix or a 1D array), it's not possible to give you the exact answer. However here is how you draw a surface in Matlab:
% create a meshgrid used as the independent variables of your surface
[sx, sy] = meshgrid(-100:100);
% if you have your own 2D matrix, ignore this line.
% if you have a formula, replace this line with your own formula
y = cos(sqrt(((sx/100).^2+(sy/100).^2)/2)*(pi/2));
% draw the surface
surf(sx, sy, y);
To have the opposite side as well, draw another surf on the same figure:
hold on;
surf(sx, sy, -y);
I have a dataset that describes a point cloud of a 3D cylinder (xx,yy,zz,C):
and I would like to make a surface plot from this dataset, similar to this
In order to do this I thought I could interpolate my scattered data using TriScatteredInterp onto a regular grid and then plot it using surf:
F = TriScatteredInterp(xx,yy,zz);
max_x = max(xx); min_x = min(xx);
max_y = max(yy); min_y = min(yy);
max_z = max(zz); min_z = min(zz);
xi = min_x:abs(stepSize):max_x;
yi = min_y:abs(stepSize):max_y;
zi = min_z:abs(stepSize):max_z;
[qx,qy] = meshgrid(xi,yi);
qz = F(qx,qy);
F = TriScatteredInterp(xx,yy,C);
qc = F(qx,qy);
figure
surf(qx,qy,qz,qc);
axis image
This works really well for convex and concave objects but ends in this for the cylinder:
Can anybody help me as to how to achieve a nicer plot?
Have you tried Delaunay triangulation?
http://www.mathworks.com/help/matlab/ref/delaunay.html
load seamount
tri = delaunay(x,y);
trisurf(tri,x,y,z);
There is also TriScatteredInterp
http://www.mathworks.com/help/matlab/ref/triscatteredinterp.html
ti = -2:.25:2;
[qx,qy] = meshgrid(ti,ti);
qz = F(qx,qy);
mesh(qx,qy,qz);
hold on;
plot3(x,y,z,'o');
I think what you are loking for is the Convex hull function. See its documentation.
K = convhull(X,Y,Z) returns the 3-D convex hull of the points (X,Y,Z),
where X, Y, and Z are column vectors. K is a triangulation
representing the boundary of the convex hull. K is of size mtri-by-3,
where mtri is the number of triangular facets. That is, each row of K
is a triangle defined in terms of the point indices.
Example in 2D
xx = -1:.05:1; yy = abs(sqrt(xx));
[x,y] = pol2cart(xx,yy);
k = convhull(x,y);
plot(x(k),y(k),'r-',x,y,'b+')
Use plot to plot the output of convhull in 2-D. Use trisurf or trimesh to plot the output of convhull in 3-D.
A cylinder is the collection of all points equidistant to a line. So you know that your xx, yy and zz data have one thing in common, and that is that they all should lie at an equal distance to the line of symmetry. You can use that to generate a new cylinder (line of symmetry taken to be z-axis in this example):
% best-fitting radius
% NOTE: only works if z-axis is cylinder's line of symmetry
R = mean( sqrt(xx.^2+yy.^2) );
% generate some cylinder
[x y z] = cylinder(ones(numel(xx),1));
% adjust z-range and set best-fitting radius
z = z * (max(zz(:))-min(zz(:))) + min(zz(:));
x=x*R;
y=y*R;
% plot cylinder
surf(x,y,z)
TriScatteredInterp is good for fitting 2D surfaces of the form z = f(x,y), where f is a single-valued function. It won't work to fit a point cloud like you have.
Since you're dealing with a cylinder, which is, in essence, a 2D surface, you can still use TriScatterdInterp if you convert to polar coordinates, and, say, fit radius as a function of angle and height--something like:
% convert to polar coordinates:
theta = atan2(yy,xx);
h = zz;
r = sqrt(xx.^2+yy.^2);
% fit radius as a function of theta and h
RFit = TriScatteredInterp(theta(:),h(:),r(:));
% define interpolation points
stepSize = 0.1;
ti = min(theta):abs(stepSize):max(theta);
hi = min(h):abs(stepSize):max(h);
[qx,qy] = meshgrid(ti,hi);
% find r values at points:
rfit = reshape(RFit(qx(:),qy(:)),size(qx));
% plot
surf(rfit.*cos(qx),rfit.*sin(qx),qy)
I need help plotting a spiral helix on a cone. For the helix:
x = tsin(6t)
y = tcos(6t)
z = t/3
...and this helix lies on the cone:
z = sqrt(x^2+y^2)/3
I need to plot the mesh plot of the cone and the 3D line plot of the helix on the same
chart.
I think you want a surface plot of the cone first. Try
[X Y] = meshgrid(-1:.01:1);
Z = sqrt(X.^2 + Y.^2)/3;
Then, plot this surface with the surf function, and set some sort of shading and transparency
surf(X,Y,Z), caxis([-1 1]), shading flat, alpha(.5);
This should make a cone shape (you can play with the colors).
Now for the helix, define the vectors as you did
t = 0:.01:1;
x = t.*cos(6*t);
y = t.*sin(6*t);
z = t/3;
Then do
hold on;
This makes it so any other plotting you do will appear on the same figure.
Then finally,
plot3(x,y,z);