尝试计算圆锥体的体积

发布于 2024-10-31 04:28:35 字数 1588 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

上面的答案,特别是尤达的答案告诉你如何计算近似值。

要检测何时达到 1% 的误差,有两种选择:

  • 与精确公式 V = pi/3 * r^2 * h 进行比较,其中 r 是底半径。由于这基本上消除了任何近似的需要,因此这可能不是这个想法。
  • 考虑以下思想实验:
    • 如果你拿一堆包含整个圆锥体的圆盘(即最大的圆盘应以底半径为半径),则体积必须小于体积之和光盘的数量。
    • 如果整个圆锥体中包含一堆圆盘(“将半径移动一步”),圆锥体的体积必须大于圆盘体积之和.
    • 因此,误差始终小于两个堆栈之间的差异。如果您使用第一个作为近似值,则误差小于最大的光盘。

在代码中:

r = h*(1:n)/n;
V = pi * r.^2 * h / n;
V = sum(V);
e = V(1);
re = e/(V-e);

现在 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:

  • Compare with the exact formula V = pi/3 * r^2 * h, where r is the base radius. Since this basically eliminates the need for any approximation, this is probably not the idea.
  • Consider the following thought experiment:
    • If you take a stack of discs which contains the whole cone (that is, the largest disc should have the base radius as a radius), the volume must be less than the sum of the volumes of the discs.
    • If you take a stack of discs contained in the whole cone ("shift the radii by one step"), the volume of the cone must be more than the sum of the volumes of the discs.
    • Hence, the error is always less than the difference between the two stacks. If you use the first one as your approximation, the error is less than the largest disc.

In code:

r = h*(1:n)/n;
V = pi * r.^2 * h / n;
V = sum(V);
e = V(1);
re = e/(V-e);

Now V contains an estimate for the volume and e an upper bound for the absolute error, and re an upper bound for the relative error.

[旋木] 2024-11-07 04:28:35

使用 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.

万水千山粽是情ミ 2024-11-07 04:28:35

如果将问题分解,编程就会更简单,而且根本不需要使用任何循环。 MATLAB 的优势在于数组/向量操作,您应该充分利用这一点。以下是您应该尝试的步骤:

  1. 首先,您需要将圆锥体分成 N 块。将高度视为从 0H 的向量,使用 linspace 函数将其分为 N 个相等的段。
  2. 正如罗宾所提到的,斜率是恒定的。因此,找到这个斜率(从高度 H 和底半径 R),并使用它和高度向量,你可以得到半径向量,它给出了每个圆盘的半径。
  3. 您知道如何计算单个磁盘的面积。在 MATLAB 中,您可以使用 . 前缀对数组中的所有元素执行算术运算。因此,例如,如果您要将向量 x 的所有元素提升到某个幂 n,您将编写 x.^n 。由此,您可以一步计算每个磁盘的面积。不需要循环。
  4. 使用 sum 将面积相加磁盘的。
  5. 最后,要获得体积,您需要将总面积乘以圆盘的厚度。您可以从高度向量轻松获得这一点。

祝你好运。

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:

  1. First you'll need to partition the cone into N pieces. Treating the height as a vector from 0 to H, use the linspace function to divide it into N equal segments.
  2. As Robin mentioned, the slope is constant. So find this slope (from height H and base radius R) and using this and the height vector, you can get the radius radius vector, which gives the radius of each disk.
  3. You know how to calculate the area of a single disk. In MATLAB, you can perform arithmetic operations on all the elements in an array using the . prefix. So, for e.g., if you were to raise all elements of a vector x to some power n, you'd write x.^n. From this, you can calculate the area of each disk in a single step. No loops required.
  4. Use sum to add up the areas of the disk.
  5. Finally to get the volume, you'll need to multiply this total area by the the thickness of a disk. This you can easily get from the height vector.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文