尝试计算圆锥体的体积
我正在尝试使用 MATLAB 程序来近似圆锥体的体积,该程序使用 n 个半径递减的等高圆盘(圆柱体)。最大的圆盘位于底部,下一个圆盘位于其顶部,半径最小的圆盘位于顶部。我在获取输入时没有遇到任何问题,但我不知道如何使用光盘来计算体积。 for循环还是while循环?
顺便说一句,圆盘的体积是 pi*r*r*h,h 是高度,r 是半径
下一步,我需要计算所需的圆盘数量,以便误差百分比小于 1%
这就是我的全部到目前为止,它可以很好地计算有限数量的光盘的体积,但我不知道如何计算光盘的数量,以使错误百分比小于 1%。我将不胜感激的帮助。谢谢!
h=input('Enter height: ');
if (h<=0)
disp('Height must be positive!');
else
r=input('Enter base radius: ');
if (r<=0)
disp('Base radius must be positive!');
else
n=input('Enter number of discs: ');
ne=n;
while n>0 || n~=0
if n>0 && (mod(n,1))==0
Vcone=(1/3)*(pi*r*r*h);
VTotal=0;
for i=n:-1:1
rEachDisc=r*(((i-1)*h)/n)/h;
VEachDisc=(pi*rEachDisc*rEachDisc)*(h/n);
VTotal=VTotal+VEachDisc;
end
Vapp=VTotal+((pi*r*r)*(h/n));
eror=abs((Vcone-Vapp)/Vcone*100);
fprintf('For n=%g, approximated volume: %g ; error: %g%%\n',n,Vapp,eror)
else
disp('This is not a positive integer!');
end
n=input('Enter number of discs: ');
end
end
end
VTotal=0;
t=1;
Vcone=(1/3)*(pi*r*r*h);
for i=ne:-1:1
rEachDisc=r*(((i-1)*h)/ne)/h;
VEachDisc=(pi*rEachDisc*rEachDisc)*(h/ne);
VTotal=VTotal+VEachDisc;
Vapp=VTotal+((pi*r*r)*(h/ne));
eror=abs((Vcone-Vapp)/Vcone*100);
if eror==1
fprintf('We need at least %g discs to approximate the volume with less than 1%% error.',ne);
break
end
end
I'm trying to approximate the volume of a cone with a MATLAB program that uses n equalheight discs(cylinders) with decreasing radii. The biggest disc on the bottom, the next on top of it, and the one with smallest radii on the top. I have no troubles getting the inputs, but I can't figure out how to calculate the volume with using discs. For loop or while loop?
By the way the volume of a disc is pi*r*r*h, h is height, r is radius
Next step, i need to calculate number of disc needed so that error percentage is less than 1%
This is all i've written so far, it works great calculating the volume with finite number of disc, but i cant figure out how to calculate the number of discs so that error percentage is less than 1%. I would appreciate the help.Thanks!!
h=input('Enter height: ');
if (h<=0)
disp('Height must be positive!');
else
r=input('Enter base radius: ');
if (r<=0)
disp('Base radius must be positive!');
else
n=input('Enter number of discs: ');
ne=n;
while n>0 || n~=0
if n>0 && (mod(n,1))==0
Vcone=(1/3)*(pi*r*r*h);
VTotal=0;
for i=n:-1:1
rEachDisc=r*(((i-1)*h)/n)/h;
VEachDisc=(pi*rEachDisc*rEachDisc)*(h/n);
VTotal=VTotal+VEachDisc;
end
Vapp=VTotal+((pi*r*r)*(h/n));
eror=abs((Vcone-Vapp)/Vcone*100);
fprintf('For n=%g, approximated volume: %g ; error: %g%%\n',n,Vapp,eror)
else
disp('This is not a positive integer!');
end
n=input('Enter number of discs: ');
end
end
end
VTotal=0;
t=1;
Vcone=(1/3)*(pi*r*r*h);
for i=ne:-1:1
rEachDisc=r*(((i-1)*h)/ne)/h;
VEachDisc=(pi*rEachDisc*rEachDisc)*(h/ne);
VTotal=VTotal+VEachDisc;
Vapp=VTotal+((pi*r*r)*(h/ne));
eror=abs((Vcone-Vapp)/Vcone*100);
if eror==1
fprintf('We need at least %g discs to approximate the volume with less than 1%% error.',ne);
break
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上面的答案,特别是尤达的答案告诉你如何计算近似值。
要检测何时达到 1% 的误差,有两种选择:
V = pi/3 * r^2 * h
进行比较,其中r
是底半径。由于这基本上消除了任何近似的需要,因此这可能不是这个想法。在代码中:
现在
V
包含体积估计值和e
绝对误差上限,以及re
相对误差上限错误。The above answers, especially yoda's tell you how to compute approximations.
As to detect when you've reached the 1% error, there are two options:
V = pi/3 * r^2 * h
, wherer
is the base radius. Since this basically eliminates the need for any approximation, this is probably not the idea.In code:
Now
V
contains an estimate for the volume ande
an upper bound for the absolute error, andre
an upper bound for the relative error.使用 for 循环。
关键是要认识到圆锥体具有恒定的斜率,因此近似圆锥体中的每个圆盘的半径都将与最后一个圆盘的半径相差一个常数因子。 (或者等效地,它将与最后一个直径相差一个常数因子。这是同一件事。)
要计算每个圆盘的半径,您需要从前一个半径中减去一个固定数字。因此,每次循环时,您都需要修改半径变量 r。
Use a for loop.
The key is to realise that a cone has a constant slope, so each disc in your approximation of the cone is going to differ from the last one in radius by a constant factor. (Or equivalently, it's going to differ from the last one in diameter by a constant factor. It's the same thing.)
To calculate the radius of each disc, you need to subtract a fixed number from the previous radius. So each time round the loop you will need to modify the radius variable r.
如果将问题分解,编程就会更简单,而且根本不需要使用任何循环。 MATLAB 的优势在于数组/向量操作,您应该充分利用这一点。以下是您应该尝试的步骤:
N
块。将高度视为从0
到H
的向量,使用 linspace 函数将其分为N
个相等的段。H
和底半径R
),并使用它和高度向量,你可以得到半径向量,它给出了每个圆盘的半径。.
前缀对数组中的所有元素执行算术运算。因此,例如,如果您要将向量x
的所有元素提升到某个幂n
,您将编写x.^n
。由此,您可以一步计算每个磁盘的面积。不需要循环。sum
将面积相加磁盘的。祝你好运。
It's simpler to program if you break the problem down, and you don't have to use any loops at all. MATLAB's strength is in array/vector manipulation, and you should make full use of that. Here are the steps that you should try:
N
pieces. Treating the height as a vector from0
toH
, use the linspace function to divide it intoN
equal segments.H
and base radiusR
) and using this and the height vector, you can get the radius radius vector, which gives the radius of each disk..
prefix. So, for e.g., if you were to raise all elements of a vectorx
to some powern
, you'd writex.^n
. From this, you can calculate the area of each disk in a single step. No loops required.sum
to add up the areas of the disk.Good luck.