Volume of solids of revolution: the cone

[This article was first published on The Beginner Programmer, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Immagine 001

Have you ever wondered where do all those formulas to calculate the volume of solids like a cone, a cylinder, a sphere ecc… come from? In fact they come from a simple formula and from a clever basic idea. Imagine you have a function f

Immagine 4

Intuitively you could approximate the volume by dividing your interval into a number n of small intervals of the same size and sum up the volume of the cylinders of radius f(x) and height clip_image002[11], then

clip_image002[7]

if we use an infinite number of steps n then our sum becomes an integral:

clip_image002[5]

Now that we have the intuition, we  can go on to the code. I coded an example in R and one in Matlab. The one in Matlab is shorter since it deals with the integration on its own using symbolic computation while in R I decided to code a simple approximation algorithm based on the first formula I posted above, although you could easily calculate the indefinite integral of the function foo and then use it to calculate the exact volume.

Here is the Matlab example, quick, fast and neat:

% Solid of revolution %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms x
syms y
% Cone height
h = 10;
% Our generator function
y1 = @(x) 1/2*x;
% Our generator function raised to the 2nd power
y2 = @(x) (1/2*x)^2;
% Volume of the cone (radius is implicitly 5)
v = pi * int(y2,x,0,h)

Note that f(10)=5, thus the radius of our cone is 5. Furthermore, for the sake of simplicity I’ve assumed that the tip is in 0,0. This code outputs:

% Output
% v =
%
% (250*pi)/3

Here is the R code. Of course you could have made it shorter and more neat but I like coding in R so I made something more out of it. The R program below approximates the volume using the first approach described above.

# Solid of rotation: a cone
#-------------------------------------------------------------------------------
# Our function to generate the solid of rotation
foo <- function(x)
{
return(0.5*x)
}
# This function integrates f^2 over the given [a,b] interval
# Assuming we're using only the positive half of the real line.
integrate <- function(f,a,b,dx=0.001)
{
if(a > b)
{
print('Please make sure b > a then enter again.')
return(0)
}else if(a == b)
{
return(0)
}
partial_sum = 0
while(a < b)
{
partial_sum <- partial_sum + max(f(a)^2,f(a+dx)^2)*dx
a <- a + dx
}
return(partial_sum)
}
# Formula for the volume of a cone and comparison with the estimatation
cone_volume <- function(r,h,compare=T)
{
v <- pi*h*r^2/3
if(!compare)
{
return(v)
}else
{
v2 <- pi*integrate(foo,0,h)
error <- abs(v - v2)/v*100
print(paste('We missed of:',error,'%'))
string_tp <- c(v,v2)
names(string_tp) <- c('Real volume','estimated volume')
print(string_tp)
}
}
# Let's approximate the volume
cone_volume(5,10)

Finally, you can get a 3D cone as the one at the top of this page just by using the RGL package in R and the demo scripts.

To leave a comment for the author, please follow the link and comment on their blog: The Beginner Programmer.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)