VOLUME OR REVO

Version 1.0.0 (1012 Bytes) by Kirubahari
CHECK IT
1 Download
Updated 2 Oct 2024

View License

% Clear workspace and command window
clc;
clearvars;
% Define symbolic variable
syms x;
% Input function and parameters
f_input = input('Enter the function f(x): ', 's'); % Get input as string
f = str2sym(f_input); % Convert string input to symbolic expression
fL = input('Enter the interval on which the function is defined: ');
yr = input('Enter the axis of rotation y = c (enter only c value): ');
iL = input('Enter the integration limits: ');
% Ensure f is a valid function
if ~isa(f, 'sym')
error('The input function must be a valid symbolic expression.');
end
% Calculate the volume of the solid of revolution using the disk method
Volume = pi * int((f - yr)^2, iL(1), iL(2));
disp(['Volume is: ', num2str(double(Volume))]);
% Convert symbolic function to a MATLAB function for plotting
fx = matlabFunction(f); % Change to matlabFunction
% Define ranges for plotting
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 = [min(fx(xvals)), max(fx(xvals))];
% Plot the function and region
figure('Position', [100, 200, 560, 420]);
% Subplot 1: Function plot and filled region
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 Region', 'Axis of Rotation', 'Location', 'Best');
title('Function y = f(x) and Region');
xlabel('x-axis');
ylabel('y-axis');
set(gca, 'XLim', xlim);
% Subplot 2: Rotated region
subplot(2, 1, 2);
hold on;
plot(xivals, fx(xivals), 'b-', 'LineWidth', 2);
fill([xivals, xivalsr], [fx(xivals), ones(size(xivalsr)) * 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, 'm-', 'LineWidth', 2);
plot([iL(1), iL(2)], [yr, yr], 'r-', 'LineWidth', 2);
title('Rotated Region in xy-Plane');
xlabel('x-axis');
ylabel('y-axis');
set(gca, 'XLim', xlim);
% Generate and visualize the solid of revolution
[X, Y, Z] = cylinder(fx(xivals) - yr, 100);
Z = iL(1) + Z .* (iL(2) - iL(1));
figure('Position', [700, 200, 560, 420]);
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);

Cite As

Kirubahari (2024). VOLUME OR REVO (https://www.mathworks.com/matlabcentral/fileexchange/173355-volume-or-revo), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2024b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0