How to use the "cylinder function" of Matlab to create a surface of revolution? - matlab

I have been trying to develop a GUIDE program with MatLab that plots a surface of revolution but I just got wrong answers.
Here's what I tried:
b = str2double(get(handles.editB, 'string'));
Incremento = str2double(get(handles.editIncrement, 'string'));
x = a:Incremento:b;
y = a:Incremento:b;
helperFunction = get(handles.editFunction, 'string' );
myFunction = eval(helperFunction);
[X,Y,Z] = cylinder(myFunction);
surf(Y,X,Z);
title('Surface of Revolution');
In addition I have to mention that the previous code plots surfaces of revolution of a function as if the function were the inverse function. For example: I wanna try to plot the surface of revolution of x^2 then the program would output the surface of revolution of sqrt(x).

I think the answers you get are correct, but your expectations of this function are wrong. According to MATLAB documents:
[X,Y,Z] = cylinder(r) returns the x-, y-, and z-coordinates of a cylinder using r to define a profile curve. cylinder treats each element in r as a radius at equally spaced heights along the unit height of the cylinder. The cylinder has 20 equally spaced points around its circumference.
In other words, this function considers the first and last elements of the vector r as cylinder radii at heights 0 and 1, respectively, and the other elements as cylinder radii at equally spaced heights in this interval. The following figure may explain this better:

Related

plot a 3D matrix of concentrations in matlab with slice

I have a 3D matrix C=51x51x11 dimensions, obtained from a function in a separate script, the x,y,z represent length, depth and height, and the value represent a concentration per x,y,z point. I want to create a slice crossing x and another crossing y showing the difference in concentration by color. I have tried using ngrid and meshgrid but didn't work. may i have some help with this please?
Use slice()
C = randi(1,[51,51,11]);
x= 25; y = 25; z = 5;
sl = slice(C,x,y,z);
Using slice inside a function to make it easy to view in 3d:
function eslice(V,sx,sy,sz)
slice(V,sx,sy,sz)
shading interp
axis equal
axis vis3d
end
This is from my personal library, enjoy.

How to plot a 3D surface with a circle in it?

I have a rational polynomial function. I find zeros of numerator and denominator of it. Now I want to draw this function and I do it with meshgrid and mesh command in matlab. How can I draw a circle in this shape? I add my result figure at first and second figure is an image that I want to be like that( draw red circle).
Create x and y for your circle:
r = 1;
theta = 0:0.1:2*pi;
x = r*cos(theta);
y = r*sin(theta);
Get the value of your function at the x and y's and plot a line in 3D with the values:
z = f(x,y);
plot3(x,y,z);
The final result may have some artefacts where the line crosses in and out of the surface. If you are not so concerned about the accuracy in the plot add a very small value to z to "lift" it above the surface.

Integrating Velocity Over a Complex 2-D Surface

I'm using matlab to calculate the the average velocity in the cross section of a pipe by integrating discrete velocity points over a surface. The points are scattered in a random pattern that form a circle (almost).
I used scatteredinterpolant to create a function relating x and y to v (velocity) in order to create a grid of interpolated values.
F = scatteredInterpolant(x, y, v,'linear');
vq = F(xq,yq); % where xq and yq are a set of query points
The problem I am now having is trying to calculate the the surface area of this function, but only in this circular portion that contains all the scatter points.
The first way I went about this was using the quad2d function.
int = quad2d(#(x,y) F(x,y), min(x), max(x), min(y), max(y), 'MaxFunEvals', 100000);
However this gives is incorrect as it takes the area over a rectangle and a circle.
Now I can probably define the surface area with a circle but in the future I will have to work with more complex shapes so I want to use points that define the boundary of the scatter points.
I'm doing this through triangulation, using the following command.
DT = delaunayTriangulation(x,y);
However I have no idea how I can incorporate these points into a quad2d function. I was hoping someone might have a suggestion or possibly another method I could use in calculating the area over these complex surfaces.
Thanks!
You could assume your function to be piecewise linear on your integration area and then integrate it using a midpoint quadrature rule:
For every triangle you compute the midpoint value as the mean of the nodal values and multiply by the triangle's area. You sum it all up to get your integral.
function int = integrate(T, values)
meanOnTriangle = mean(values(T.ConnectivityList),2);
int = sum(getElementAreas(T).*meanOnTriangle);
end
function areas = getElementAreas(T)
X = #(d) T.Points(T.ConnectivityList(:,d),:);
d21 = X(2)-X(1);
d31 = X(3)-X(1);
areas = abs(1/2*(d21(:,1).*d31(:,2)-d21(:,2).*d31(:,1)));
end
As your goal is the average velocity, you want to compute the following quantity:
averageVelocity = integrate(DT,v)/sum(getElementAreas(DT));

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) .

Why not spherical plot? How to plot 3D-polar-plot in Matlab?

[r,t] = meshgrid(linspace(0,2*pi,361),linspace(0,pi,361));
[x,y]=pol2cart(sin(t)*cos(r),sin(t)*sin(r));
%[x,y]=pol2cart(r,t);
surf(x,y);
I played with this addon but trying to find an default function to for this. How can I do the 3D-polar-plot?
I am trying to help this guy to vizualise different integrals here.
There are several problems in your code:
You are already converting spherical coordinates to cartesian coordinates with the sin(theta)*cos(phi) and sin(theta)*sin(phi) bit. Why are you calling pol2cart on this (moreover, we're not working in polar coordinates!)?
As natan points out, there is no third dimension (i.e. z) in your plot. For unity radius, r can be omitted in the spherical domain, where it is completely defined by theta and phi, but in the cartesian domain, you have all three x, y and z. The formula for z is z = cos(theta) (for unit radius).
You didn't read the documentation for surf, which says:
surf(Z,C) plots the height of Z, a single-valued function defined over a geometrically rectangular grid, and uses matrix C, assumed to be the same size as Z, to color the surface.
In other words, your surf(x,y) line merely plots the matrix x and colors it using y as a colormap.
Here's the above code with the mistakes fixed and plotted correctly:
[f,t] = meshgrid(linspace(0,2*pi,361),linspace(0,pi,361));
x = sin(t)*cos(f);
y = sin(t)*sin(f);
z = cos(t);
surf(x,y,z)