How to fix number of points of a cone using Matlab

1 Ansicht (letzte 30 Tage)
M.S. Khan
M.S. Khan am 18 Aug. 2020
Kommentiert: M.S. Khan am 21 Aug. 2020
The code below is great which generates a cone of infinite points. Could anyone help me how to fix the number of points. For example, if i want only 10, 000 points or 20,000 point on the cone. How can i fix on the cone using code below. Thanks for support and help. Regards.
r1=input('enter radius');
r=r1:-0.5:0;
h=input('enter ht');
% --------------------
xvec = floor(-r1):ceil(r1);
yvec = xvec;
hvec = 0:ceil(h);
[X, Y, H] = ndgrid(xvec, yvec, 0:floor(h));
r_at_H = r1 * (1 - H/h);
is_in_cone = abs(X) <= r1 & abs(Y) <= r1 & H <= h & sqrt(X.^2+Y.^2) <= r_at_H;
Xc = X(is_in_cone);
Yc = Y(is_in_cone);
Hc = H(is_in_cone);
pointsize = 20;
figure(2)
scatter3(Xc, Yc, Hc, pointsize, 'filled')

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 19 Aug. 2020
Bearbeitet: Bruno Luong am 20 Aug. 2020
Grid pattern
The volume of the cone of base radius R and height h
Vcone = pi*R^2*h/3
Assuming you dsicretize by small volume (dx x dx x dx), you 'll get
N ~ Vcone/dx^3. So if you want an approximative of N with grid pattern:
% Cone parameters
R = 10;
h = 10;
N = 10000;
dx = (pi*R^2*h/3/N)^(1/3);
rvec = linspace(-R,R,ceil(2*R/dx));
hvec = linspace(0,h,ceil(h/dx));
[X,Y,Z] = ndgrid(rvec,rvec,hvec);
is_in_cone = (X.^2+Y.^2) <= (R/h*(h-Z)).^2; % Edit bug fix
Xc = X(is_in_cone);
Yc = Y(is_in_cone);
Zc = Z(is_in_cone);
figure(3)
pointsize = 3,
scatter3(Xc, Yc, Zc, pointsize, 'filled')
axis equal
numel(Xc) % should be close to N
  5 Kommentare
Bruno Luong
Bruno Luong am 20 Aug. 2020
Ops, you are right I edit and fix the BUG
M.S. Khan
M.S. Khan am 21 Aug. 2020
Thanks for late reply. Out of station. You are very helpful as usual.
Regards,

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 18 Aug. 2020
r1=input('enter radius ');
m = input('enter number of points you want along radius') ;
r = linspace(r1,0,m);
h = input('enter ht');
m = input('enter number of points you want along height') ;
% --------------------
xvec = floor(-r1):ceil(r1);
yvec = xvec;
hvec = 0:ceil(h);
[X, Y, H] = ndgrid(xvec, yvec, linspace(0,floor(h),m));
r_at_H = r1 * (1 - H/h);
is_in_cone = abs(X) <= r1 & abs(Y) <= r1 & H <= h & sqrt(X.^2+Y.^2) <= r_at_H;
Xc = X(is_in_cone);
Yc = Y(is_in_cone);
Hc = H(is_in_cone);
pointsize = 20;
figure(2)
scatter3(Xc, Yc, Hc, pointsize, 'filled')
  4 Kommentare
M.S. Khan
M.S. Khan am 19 Aug. 2020
HI KSSV, thanks for your kind reply. Where will we use colon i.e. ':' Could you please given an example. I am trying too.
KSSV
KSSV am 19 Aug. 2020
You have used in the following lines:
xvec = floor(-r1):ceil(r1);
hvec = 0:ceil(h);
[X, Y, H] = ndgrid(xvec, yvec, 0:floor(h));
Replace them with linspace by specifying the number of points you want.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by