random generation of 2 variables
Show older comments
Im trying to make a list of randomly generated values for radius and height for a cylinder with a given area. the equation is going to be 300=pi*h*r^2 and were trying to get every combination of r and h that would give us 300. I think this is possible to do, and any help would be greatly appreciated.
6 Comments
dpb
on 26 Sep 2018
" 300=pi*h*r^2 and were trying to get every combination of r and h that would give us 300"
With FP precision it is theoretically countable, but such a large number as to be totally impractical.
You better settle for some subset. :)
Bish Erbas
on 26 Sep 2018
You need to define precision/constraint requirements for this problem otherwise the answer would be infinite number of combinations.
dpb
on 26 Sep 2018
Not quite infinite, there are a countable number of representable FP numbers as alluded to above.
Bish Erbas
on 26 Sep 2018
I thought it would depend on the precision. In MATLAB yes max precision would be 64-bit therefore countable. But in real world it is infinite. For example:
300.05*(1/300.05)*300 = 300
300.06*(1/300.06)*300 = 300
300.006*(1/300.0006)*300 = 300
300.0006*(1/300.0006)*300 = 300
...
Guillaume
on 26 Sep 2018
In addition to the fact that there is an infinite (discounting the finite precision of FP numbers) number of (r, h) pairs that fulfill the equation, why should the generation be random if you want to get all the combinations.
Bish Erbas
on 26 Sep 2018
@Guaillaume excellent point.
Answers (3)
KSSV
on 26 Sep 2018
N = 1000 ;
r = linspace(0,100,N) ;
h = 300./(pi*r.^2) ;
Bruno Luong
on 26 Sep 2018
Edited: Bruno Luong
on 26 Sep 2018
Here is a solution that does not require any quantification, but need this FEX from R. Stafford. The generated h and r are >= 1 however (to get bounded for other values). The code can be adapted if the bounds are specified differently.
Of course want combination is a non-sense request for continuous random variables.
V = 300; % target volume
s = log(V)-log(pi);
n = 100; % number of random cylinders
A = randfixedsum(2,n,s,0,s); % FEX
%
h = exp(A(1,:));
r = exp(A(2,:)/2);
Image Analyst
on 26 Sep 2018
You could try this:
N = 1e7; % Generate 10 million (r, h) pairs.
minRadius = 0.2; % Min radius you want to allow.
maxRadius = 10; % Max radius you want to allow.
r = minRadius + (maxRadius-minRadius) * sort(rand(1, N));
h = 300./(pi*r.^2);
plot(r, h, 'b-', 'LineWidth', 2);
xlabel('Radius', 'FontSize', 15);
ylabel('Height', 'FontSize', 15);
title('Cylinder Height vs. Radius for a volume of 300', 'FontSize', 15);
grid on;

1 Comment
Bruno Luong
on 26 Sep 2018
Edited: Bruno Luong
on 26 Sep 2018
User just be aware about the distribution of random cylinders that are generated by such or such method.
Image Analyst's method and mine do not give the same distribution.
Using RS's FEX, it's a careful uniform conditional probability samples that are generated, usually it's occur most naturally and it has tendency to produce more "balanced" cylinder, meaning h ~ r, rather than the extreme long (h >> r) or fat cylinders (h << r).
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!