volume of solid generated by revolving a curve about y axis or parallel to y axis

7 views (last 30 days)
I wrote a matlb code to find the voume of a solid generated by revolving it about x axis or parallel to x axis but i need to modify that code to revolve the curve about y axis or parallel to y axis can anyone help me modify the code.
%MATLAB code to find the volume of a solid generated by revolving a curve about the x-axis or parallel to x-axis
clc
clear vars
syms x;
f=input("Enter the function: ");
fL=input("Enter the interval on which the function is defined [a b]: ");
yr=input("Enter the axis of rotation y=c (enter only c value): ");
iL=input("Enter the integration limits [a b]: ");
Volume=pi*int((f-yr)^2,iL(1),iL(2));
disp(["Volume is: ",num2str(double(Volume))])
fx=inline(vectorize(f));
xvals=linspace(fL(1),fL(2),201);
xvalsr=fliplr(xvals);
xivals=linspace(iL(1),iL(2),201);
xivalsr=fliplr(xivals);
xlim=[fL(1) fL(2)+0.5];
ylim=fx(xlim);
figure("Position",[100 200 560 420])
subplot(2,1,1)
hold on;
plot(xvals,fx(xvals),'-b','LineWidth',2);
fill([xvals xvalsr],[fx(xvals) ones(size(xvalsr))*yr],[0.8 0.8 0.8],"FaceAlpha",0.8)
plot([fL(1) fL(2)],[yr yr],'-r',"LineWidth",2);
legend('Function Plot','Filled TRegion','Axis of ROtation','Location','Best');
title('Function y=f(x) and Region');
set(gca,'XLim',xlim)
xlabel('x-axis');
ylabel('y axis');
subplot(2,1,2)
hold on;
plot(xivals,fx(xivals),'-b','LineWidth',2);
fill([xivals xivalsr],[fx(xivals) ones(size(xivals))*yr],[0.8 0.8 0.8],'FaceAlpha',0.8)
fill([xivals xivalsr],[ones(size(xivals))*yr -fx(xivalsr+2*yr)],[1 0.8 0.8],'FaceAlpha',0.8)
plot(xivals, -fx(xivals)+2*yr,'-r','LineWidth',2);
plot([iL(1) iL(2)],[yr yr],'-r','LineWidth',2);
title('Rotated Region in xy-Plane');
set(gca,'XLim',xlim)
xlabel('x-axis');
ylabel('y-axis');
[X,Y,Z]=cylinder(fx(xivals)-yr,100);
figure('Position',[700 200 560 420])
z=iL(1)+Z.*(iL(2)-iL(1))
surf(Z,Y+yr,X,'EdgeColor','none','FaceColor','flat','FaceAlpha',0.6);
hold on;
plot([iL(1) iL(2)],[yr yr],'-r','LineWidth',2);
xlabel('X-Axis');
ylabel('Y-Axis');
zlabel('Z-Axis');
view(22,11);
  2 Comments
tunir
tunir on 4 Nov 2023
z=iL(1)+Z.*(iL(2)-iL(1))
can someone explain why we wrote this line??
figure('Position',[700 200 560 420])
and also why are taking these specific values?
DGM
DGM on 4 Nov 2023
cylinder() creates a unit cylinder from a generating curve that defines its radius. Since we're after a surface of revolution on a specific z-interval, that particular line of code scales and offsets the Z data from [0 1] to the specified limits [0 pi/2]. It's a denormalization.
As to why those specific values are used for window position, I'm not sure that the values matter that much except that they position the two figures in such a way that they don't obscure each other.

Sign in to comment.

Accepted Answer

DGM
DGM on 3 Nov 2021
Same story, just transpose everything.
syms y;
% what is the value of requiring the user to manually re-enter data
% every single time they run a script?
% f=input('Enter the function: ');
% fL=input('Enter interval on which the function is defined: ');
% yr=input('Enter the axis of rotation x=c (enter only c value): ');
% iL=input('Enter the integration limits[a b]: ');
% it's a script.
% if you want flexibility, write a function or something
f = cos(2*y);
fL = [-2 2];
xr = 2;
iL = [0 pi/2];
Volume=pi*int((f-xr)^2,iL(1),iL(2));
disp(['Volume is: ',num2str(double(Volume))])
Volume is: 22.2066
fx=inline(vectorize(f));
yvals=linspace(fL(1),fL(2),201);
yvalsr=fliplr(yvals);
yivals=linspace(iL(1),iL(2),201);
yivalsr=fliplr(yivals);
ylim=[fL(1) fL(2)+0.5];
xlim=fx(ylim);
figure('Position',[100 200 560 420])
subplot(2,1,1)
hold on;
plot(fx(yvals),yvals,'-b','LineWidth',2);
fill([fx(yvals) ones(size(yvalsr))*xr],[yvals yvalsr],[0.8 0.8 0.8],'FaceAlpha',0.8)
plot([xr xr],[fL(1) fL(2)],'-r','LineWidth',2);
legend('Function Plot','Filled Region','Axis of Rotation','Location','Best');
title('Function x=f(y) and Region');
set(gca,'YLim',ylim)
xlabel('x-axis');
ylabel('y-axis');
subplot(2,1,2)
hold on;
plot(fx(yivals),yivals,'-b','LineWidth',2);
fill([fx(yivals) ones(size(yivalsr))*xr],[yivals yivalsr],[0.8 0.8 0.8],'FaceAlpha',0.8)
fill([ones(size(yivalsr))*xr ones(size(yivals))*xr-fx(yivalsr)+xr],[yivals yivalsr],[1 0.8 0.8],'FaceAlpha',0.8)
plot(-fx(yivals)+2*xr,yivals,'-m','LineWidth',2);
plot([xr xr],[iL(1) iL(2)],'-r','LineWidth',2);
title('Rotated Region in xy-Plane');
set(gca,'YLim',ylim)
xlabel('x-axis');
ylabel('y-axis');
[X,Y,Z]=cylinder(fx(yivals)-xr,100);
figure('Position',[700 200 560 420])
Z=iL(1)+Z.*(iL(2)-iL(1));
surf(Y+xr,Z,X,'EdgeColor','none','FaceColor','flat','FaceAlpha',0.6);
hold on;
plot([xr xr],[iL(1) iL(2)],'-r','LineWidth',2);
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
view(-50,11);
idk that I didn't miss anything.

More Answers (1)

Chals
Chals on 10 Dec 2022
The code is so useful, but would it be possible to do a rotation like this in some way?
Something unrelated but everyone must know: rentry.co/owot3

Community Treasure Hunt

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

Start Hunting!