- Ensure that “fx” is correctly defined as a symbolic expression. Convert it from string using “str2sym” function.
How to find volume of a curve [f(x)] which is rotated along y-axis?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
We have a input function y = f(x) and we have to find the volume along x-axis and y-axis. (solid of revolution) The minimum and maximum values of x are given
I am getting the volume along x-axis but I am getting an error while finding the volume along the y-axis.
The values of f(x) and domain is : -
- x^(1/2), 0<=x<=4
- 3*cos(0.4*x+pi), -5<=x<=5
- sin(x)+3, -5<=x<=5
- x^3, 0<=x<=3
Here is my code
%Volume along x-axis
clc
clear all
syms x y
%fx = input('Enter the function : ')
xmin = input('Enter minimum value of x : ')
xmax = input('Enter maximum value of x : ')
intfx = int((fx)^2,xmin,xmax)
vx = pi*(intfx)
vx = double(vx)
sprintf('The volume along x-axis is %d',vx)
%Volume along y-axis
y_fx_equation_wrt_x = y == fx
x_fy_equation_wrt_y = solve(y_fx_equation_wrt_x, x)
fy = x_fy_equation_wrt_y
ymin = subs(fx,x,xmin)
ymax = subs(fx,x,xmax)
intfy = abs(int(fy^2,ymin,ymax))
vy = pi*(intfy)
vyy = double(vy)
sprintf('The volume along y-axis is %d',vyy)
In output 1 I get an error but on output 2 I don't.
What am I doing wrong?
Output 1
y = 3*cos(0.4*x+pi)
3*cos(0.4*x+pi)
fx =
-3*cos((2*x)/5)
Enter minimum value of x :
-5
xmin =
-5
Enter maximum value of x :
5
xmax =
5
intfx =
(45*sin(4))/4 + 45
vx =
pi*((45*sin(4))/4 + 45)
vx =
114.6241
ans =
'The volume along x-axis is 1.146241e+02'
y_fx_equation_wrt_x =
y == -3*cos((2*x)/5)
x_fy_equation_wrt_y =
(5*pi)/2 - (5*acos(y/3))/2
(5*pi)/2 + (5*acos(y/3))/2
fy =
(5*pi)/2 - (5*acos(y/3))/2
(5*pi)/2 + (5*acos(y/3))/2
ymin =
-3*cos(2)
ymax =
-3*cos(2)
Error using ^ (line 442)
Matrix must be square.
Error in Week6classwork1_2 (line 35)
intfy = abs(int(fy^2,ymin,ymax))
Output 2
y = x^(0.5)
Enter the function :
x^(0.5)
fx =
x^(1/2)
Enter minimum value of x :
0
xmin =
0
Enter maximum value of x :
4
xmax =
4
intfx =
8
vx =
8*pi
vx =
25.1327
ans =
'The volume along x-axis is 2.513274e+01'
y_fx_equation_wrt_x =
y == x^(1/2)
Warning: Solutions are only valid under certain conditions. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
x_fy_equation_wrt_y =
y^2
fy =
y^2
ymin =
0
ymax =
2
intfy =
32/5
vy =
(32*pi)/5
vyy =
20.1062
ans =
'The volume along y-axis is 2.010619e+01'
0 comentarios
Respuestas (1)
Ishaan
el 28 de En. de 2025
For each function “y = f(x)”, you're trying to calculate the volume around the x and y axes.
I notice 2 potential improvements in your code to achieve the same.
fx = str2sym(input('Enter the function : ', 's'));
2. When calculating the volume around the y-axis, you need to solve “y = f(x)” for “x”. This can return multiple solutions, especially for trigonometric functions. This leads to the error you indicated in “Output 1”. Use only the principal value to get a manageable solution.
x_fy_equation_wrt_y = solve(y_fx_equation_wrt_x, x, 'PrincipalValue', true);
After integrating these changes, the code is as follows.
%Volume along x-axis
clc
clear all
syms x y
fx = str2sym(input('Enter the function : ', 's'));
xmin = input('Enter minimum value of x : ')
xmax = input('Enter maximum value of x : ')
intfx = int((fx)^2,xmin,xmax)
vx = pi*(intfx)
vx = double(vx)
sprintf('The volume along x-axis is %d',vx)
%Volume along y-axis
y_fx_equation_wrt_x = y == fx
x_fy_equation_wrt_y = solve(y_fx_equation_wrt_x, x, 'PrincipalValue', true);
fy = x_fy_equation_wrt_y
ymin = subs(fx,x,xmin)
ymax = subs(fx,x,xmax)
intfy = abs(int(fy^2,ymin,ymax))
vy = pi*(intfy)
vyy = double(vy)
sprintf('The volume along y-axis is %d',vyy)
Now, the code works correctly for the 4 functions and the limits you provided.
Additionally, the warning you're encountering is due to the symbolic solver indicating that the solutions might only be valid under certain conditions. This often happens with trigonometric or non-polynomial functions. The warning suggests setting 'ReturnConditions' to true to get additional information about the conditions under which the solutions are valid. However, for simplicity, we can handle the principal branch and check the validity of the solutions.
Hope this helps.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!