Plot distribution inside a region - matlab

I have a distribution of points inside a circle.
So, I draw a circular grid inside that circle. I want to find the number of points inside each cell of the circular gird.
Is there a way to implement that easily. or maybe drawing a grid is not necessary?
My goal is plotting the distribution.
Any help is highly appreciated.
Thanks in advance.

If X,Y are the coordinates of the points in your circle, the distances from the center can be obtained with
(edit: T/H #horchler)
d = sqrt(sum([X(:)-X0 Y(:)-Y0].^2,2));
where X0, Y0 are the coordinates of the center of the circle.
You can then compute a radial distribution using hist:
figure, hist(d)
or if you just want the distribution and bins
[distr bins] = hist(d);

By "circular Grid" I understand a grid in azimuth and modulus. I suggest you convert to polar coordinates:
z = x + j*y; % x, y are vectors woth x, y coordinates of the points
az = angle(z); % note that this gives azimuth in radians
mod = abs(z);
and then apply some kind ot 2D histogram to az and mod, for example using this function. (Please note it is a user-contributed file. I haven't tested it myself).

Related

Quiver plot in X-Z plane instead of X-Y plane

In MATLAB, quiver will plot a vector field on the x-y plane by default. Is there a way to rotate the image so that is lies on the x-z plane?
I have tried creating a 3D matrix and using commands like streamslice and quiver3. However I have a large number of data points that are irregularly spaced so this results in some inaccurate interpolation and matrices that are too big.
It seems the easiest option would be to use a command like rotate, but that doesn't seem to work with quiver.
You should just use quiver3 and add zero-valued (or whatever constant y you want) inputs in the dimensions that you don't care about
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;
Q = quiver3(x, zeros(size(x)), y, u, zeros(size(u)), v);
axis equal

MATLAB: Area and centre of mass of a plotted figure

I have made a function that allows a user to draw several points and interpolate those points. I want the function to calculate the centre of mass using this vector :
I think I should therefore first calculate the area of the figure (I drew this example to illustrate the function output). I want to do this using Green's theorem
However since I'm quite a beginner in MATLAB I'm stuck at how to implement this formula in order to find the centre of mass. I'm also not sure how to get the data as my output so far is only the x- and y cordinates of the points.
function draw
fig = figure();
hax = axes('Parent', fig);
axis(hax, 'manual')
[x,y] = getpts();
M = [x y]
T = cumsum(sqrt([0,diff(x')].^2 + [0,diff(y')].^2));
T_i = linspace(T(1),T(end),1000);
X_i = interp1(T,x',T_i,'cubic');
Y_i = interp1(T,y',T_i,'cubic');
plot(x,y,'b.',X_i,Y_i,'r-')
end
The Center Of Mass for a 2D coordinate system should just be the mean of the interpolated x-coordinates and y-coordinates. The Interpolation should give you evenly spaced coordinates which you can use to your advantage. So simply add to your existing function:
CenterOfMass= [mean(X_i),mean(Y_i)]
plot(x,y,'b.',X_i,Y_i,'r-')
hold on
plot(CenterOfMass(1),CenterOfMass(2),'ro')
should give you the center of mass assuming that all points are weighted equally.

Plotting 3xN matrix(N number of 3D points) on same graph using matlab

I've a 3xN matrix W where N is 50
W(1,1) is x coordinate of a point
W(2,1) is y coordinate of same point
W(3,1) is z coordinate of same point
Similarly:
W(1,2) is x coordinate of another point
W(2,2) is y coordinate of same point
W(3,2) is z coordinate of same point
....
Now I want to 3d plot all these 3d points on same figure using matlab. How can I plot all these
points on same figure?
Is it possible to plot this matrix using a single function call(in matlab)?
I know that plot3 can be used but it can be used for one graph at a time.
So plot3(v(1,1),v(1,2),v(1,3)); is just a single point. But how do I plot all N points?
Is there an easier and better method?
I guess you can use plot3(w(1,:),w(2,:),w(3,:)).

isosurface function MATLAB usage

Hi can any one give me a simple example of how to use the isosurface function in MATLAB.
The example given if you type help isosurface is quite confusing. Searching on google did not help as no one gives simple examples anywhere. All of them use predefined functions like
flow.
For starters, suppose i have points (x,y,z) where z=0 and at each point I define a constant
function f(x,y,z)=6. So if I use the isosurface function on the isovalue 6 I would like MATLAB to give me a 3d plot with the XY plane highlighted in some colour, say green.
I don't quite understand your example, but here's how you use isosurface to draw a sphere:
%# create coordinates
[xx,yy,zz] = meshgrid(-15:15,-15:15,-15:15);
%# calculate distance from center of the cube
rr = sqrt(xx.^2 + yy.^2 + zz.^2);
%# create the isosurface by thresholding at a iso-value of 10
isosurface(xx,yy,zz,rr,10);
%# make sure it will look like a sphere
axis equal
The example you gave is very uninteresting, in fact maybe even problematic.
By collapsing all points to z=0, you no longer can/need to use ISOSURFACE, and CONTOUR should be called instead. Even then, a constant function f(X,Y)=6 won't show anything either...
Since #Jonas already showed how to use ISOSURFACE, here is an example for the CONTOUR function:
%# create a function to apply to all X/Y coordinates
[X,Y] = meshgrid(-2:0.1:2,-1:0.1:1);
f = #(X,Y) X.^3 -2*Y.^2 -3*X;
%# plot the function surface
subplot(121), surfc(X,Y,f(X,Y))
axis equal, daspect([1 1 3])
%# plot the iso-contour corresponding to where f=-1
subplot(122), contour(X,Y,f(X,Y),[-1 -1]),
axis square, title('Contour where f(X,Y)=-1')

Creating a cylinder with axis centered differently

I know Matlab has a function called cylinder to create the points for a cylinder when number of points along the circumference, and the radius length. What if I don't want a unit cylinder, and also don't want it to center at the default axis (for example along z-axis)? What would be the easiest approach to create such a cylinder? Thanks in advance.
The previous answer is fine, but you can get matlab to do more of the work for you (because the results of cylinder separate x,y,z components you need to work a little to do the matrix multiplication for the rotation). To have the center of base of the cylinder at [x0 y0 z0], scaled by [xf yf xf] (use xf=yf unless you want an elliptic cylinder), use:
[x y z] = cylinder;
h=mesh(x*xf+x0,y*yf+y0,z*zf+z0)
If you also want to rotate it so it isn't aligned along the z-axis, use rotate. For example, to rotate about the x-axis by 90 degrees, so it's aligned along the y-axis, use:
rotate(h,[1 0 0],90)
Multiply the points by your favourite combination of a scaling matrix, a translation matrix, and a rotation matrix.