calculate the volume obtained by rotating a function around the X-axis

4 views (last 30 days)
hello everyone! i hope someone can help me because i haven't been able to solve this problem for days.
I need to calculate the volume obtained by rotating a function around the X-axis. I don't know the function, but I know some points that belong to the function. I have tried interpolating them with spline, but I cannot then find the function that describes the curve that is created.
I have also tried polyfit by dividing the function into lines, but then I cannot even calculate the volume of the solid and graph it.
I leave below the codes I used for the spline and for polyfit and the volume calculation.
Please let me know if you have any ideas on a working method
% spline
x=[0 0.7095 1.419 2.1285 2.838 3.5475 4.257 4.9666 5.6761 6.3856 7.0951 7.8046 8.5141 9.2236 9.9331 10.6426 11.3521 12.0616 12.7711 13.4807 14.1902 14.8997 15.6092 16.3187 17.0282 17.7377 18.4472 19.1567 19.8662 20.5757 21.2852 21.9947 22.7043 23.4138 24.1233 24.8328 25.5423 26.2518 26.9613 27.6708 28.3803];
y=[6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.2491 6.4815 6.8997 7.8145 11.0227 18.0865 19.6034 20.582 21.53 22.4735 23.4214 24.3694 25.3173 26.2653 27.2132 28.1612 29.1091 30.0571 31.005 31.953 32.9009 33.0405 30.0322 26.6984 21.1624 6.82];
xx = 0: 0.7095: 28.3803;
yy = spline(x,y,xx);
plot(x,y,'o',xx,yy)
S= spline (x,y)
% polyfit
>> a1= [0 0.7095 1.419 2.1285 2.838 3.5475 4.257 4.9666 5.6761 6.3856 7.0951 7.8046 8.5141 9.2236 9.9331]';
>> d1= [6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24]';
>> c1=polyfit(a1,d1,1);
mv1=[a1 a1.^0];
cp1=mv1\d1
>> x1= (0:0.7095: 9.9331);
>> y1=polyval(c1,x1);
>> a2=[9.9331 10.6426 11.3521 12.0616 12.7711 13.4807 14.1902 14.8997 15.6092 16.3187]';
>> d2=[6.24 6.2491 6.4815 6.8997 7.8145 11.0227 18.0865 19.6034 20.582 21.5297]';
>> c2=polyfit(a2,d2,9);
mv2=[ a2.^10 a2.^9 a2.^8 a2.^7 a2.^6 a2.^5 a2.^4 a2.^3 a2.^2 a2 a2.^0];
cp2=mv2\d2
>> x2= (9.9331:0.7095: 16.3187);
>> y2=polyval(c2,x2);
>> a3=[16.3187 17.0282 17.7377 18.4472 19.1567 19.8662 20.5757 21.2852 21.9947 22.7043 23.4138 24.1233 24.8328]';
>> d3=[21.5297 22.4773 23.4249 24.3725 25.3201 26.2678 27.2154 28.163 29.1106 30.0583 31.0059 31.9535 32.9011]';
>> c3=polyfit(a3,d3,1);
mv3=[a3 a3.^0];
cp3=mv3\d3
>> x3= (16.3187:0.7095:24.8328);
>> y3=polyval(c3,x3);
>> a4=[24.8328 25.5423 26.2518 26.9613 27.6708 28.3803]';
>> d4=[32.9011 33.0405 30.0322 26.6984 21.1624 6.82]';
>> c4=polyfit(a4,d4,5);
mv4=[a4.^6 a4.^5 a4.^4 a4.^3 a4.^2 a4 a4.^0];
cp4=mv4\d4
>> x4= (24.8328:0.7095: 28.3803);
>> y4=polyval(c4,x4);
f1= '(6.2400*x1.^0).^2';n=50;for k=1:3,n=2*n;
x1=linspace(0, 9.9331,n+1);h=x1(2)-x1(1);
y1=eval(f1);s1(k)=4*sum(y1(2:2:n));
s1(k)=s1(k)+2*sum(y1(3:2:n-1));s1(k)=h*(s1(k)+y1(1)+y1(n+1))/3;end,s=s1*pi;s';
f2= '(0.0007*x2.^8-0.0111*x2.^7+0.0995*x2.^6-0.4719*x2.^5+0.9249*x2.^4).^2';
n=50;for k=1:3,n=2*n;
x2=linspace(9.9331, 16.3187,n+1);h=x2(2)-x2(1);
y2=eval(f2);s(k)=4*sum(y2(2:2:n));
s2(k)=s(k)+2*sum(y2(3:2:n-1));s(k)=h*(s(k)+y2(1)+y2(n+1))/3;end,s2=s*pi;s';
f3= '(1.3356*x3.^1 -0.2655*x3.^0).^2' ;
n=50;for k=1:3,n=2*n;
x3=linspace(16.3187, 24.8328,n+1);h=x3(2)-x3(1);
y3=eval(f3);s(k)=4*sum(y3(2:2:n));
s(k)=s(k)+2*sum(y3(3:2:n-1));s(k)=h*(s(k)+y3(1)+y3(n+1))/3;end,s3=s*pi;s';
f4= ' (1.0e+04*0.001*x4.^4 -1.0e+04*0.0306*x4.^3 + 1.0e+04*0.4563*x4.^2 - 1.0e+04*2.6840*x4.^1).^2' ;
n=25;for k=1:3,n=2*n;
x4=linspace(24.8328, 28.3803,n+1);h=x4(2)-x4(1);
y4=eval(f4);s(k)=4*sum(y4(2:2:n));
s(k)=s(k)+2*sum(y4(3:2:n-1));s(k)=h*(s(k)+y4(1)+y4(n+1))/3;end,s4=s*pi;s';
>> S= s1+s2+s2+s4

Accepted Answer

Karim
Karim on 8 Jul 2022
Edited: Karim on 8 Jul 2022
Hello, since at first glance there are no internal voids you can use the convex hull function to determine the volume numerically. See below for the method.
If you want more precision, you can interchange the coarse grid by the fine grid based on the spline interpolation or you can evaluate the 3D grid based on a smaller angle. The method belows discretizes over a 1 degree angle, you can reduce the angle trivially.
Hope it helps.
% set up 2D line
x=[0 0.7095 1.419 2.1285 2.838 3.5475 4.257 4.9666 5.6761 6.3856 7.0951 7.8046 8.5141 9.2236 9.9331 10.6426 11.3521 12.0616 12.7711 13.4807 14.1902 14.8997 15.6092 16.3187 17.0282 17.7377 18.4472 19.1567 19.8662 20.5757 21.2852 21.9947 22.7043 23.4138 24.1233 24.8328 25.5423 26.2518 26.9613 27.6708 28.3803];
y=[6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.24 6.2491 6.4815 6.8997 7.8145 11.0227 18.0865 19.6034 20.582 21.53 22.4735 23.4214 24.3694 25.3173 26.2653 27.2132 28.1612 29.1091 30.0571 31.005 31.953 32.9009 33.0405 30.0322 26.6984 21.1624 6.82];
xx = 0: 0.7095: 28.3803;
yy = spline(x,y,xx);
figure
plot(x,y,'o',xx,yy)
% create the 3d shape
numSlice = 360; % assume 1 slice per 1 degree
numSlicePoints = numel(x);
Grid3D = zeros(numSlicePoints*numSlice, 3);
GridSlice = zeros(numSlicePoints, 3);
GridSlice(1:numSlicePoints,1) = x;
GridSlice(1:numSlicePoints,2) = y;
RotX = @(x) [1 0 0; 0 cos(x) -sin(x); 0 sin(x) cos(x)];
for i = 1:numSlice
currIdx = (i-1)*numSlicePoints+1 : i*numSlicePoints;
currAngle = i * (2*pi/numSlice);
Grid3D(currIdx,:) = (RotX(currAngle) * GridSlice')';
end
figure
scatter3(Grid3D(:,1),Grid3D(:,2),Grid3D(:,3),'r','filled')
grid on
axis equal
view([10 40])
title('3D grid')
% compute volume using convex hull (per x-axis slice)
Volume = 0;
for i = 1:(numSlicePoints-1)
currIdx = Grid3D(:,1) == x(i) | Grid3D(:,1) == x(i+1);
[k,SliceVolume] = convhull(Grid3D(currIdx,:));
Volume = Volume + SliceVolume;
end
fprintf('Total internal volume of the 3D shape is %.2f\n',Volume)
Total internal volume of the 3D shape is 33263.49

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by