重温抛物线下的面积
我无法从上一个问题开始,因为我不是该网站的会员,所以当我返回时我无法对其发表评论。 这是我的问题:
要找到由图形 y=x^2 和区间 [a,b] 上的 x 轴界定的区域的面积,我们可以通过绘制多个“薄”矩形并取它们的面积之和。让我们将 [a,b] 分成 n 个相同宽度的较小区间 h=b-1/n。在每个间隔上都有一个高度为 y=r 的矩形,其中 r 是 x 轴上该小间隔的中间。该矩形的面积为 hy。编写一个 Python 函数,以 a、b 和 n 作为参数,并使用上述方法返回抛物线 y=x^2 下区域的近似面积。如果您可以解释一下您的程序为何有效,那将会有所帮助。
感谢有用的成员,我找到了以下程序(请编辑该程序,因为我无法/不知道如何
def parabola(x):
y = x*x
return y
def approx_area(fn, a, b, n):
"""
Approximate the area under fn in the interval [a,b]
by adding the area of n rectangular slices.
"""
a = float(a)
b = float(b)
area = 0.0
for slice in range(n):
left = a + (b-a)*slice/n
right = a + (b-a)*(slice+1)/n
mid = (left + right)*0.5
height = fn(mid)
width = right - left
area += height * width
return area
print "Area is", approx_area(parabola, -1.0, 1.0, 500)
但是,我需要将其放在一个完整的函数下。关于如何做到这一点有什么想法吗?
I was unable to go from the previous question because I was not a member of the website so I was unable to comment on it when I returned.
Here is my question:
To find the area of a region bounded by the graph y=x^2 and the x-axis on the interval [a,b] we can approximate the region by drawing a number of "thin" rectangles and taking the sum of their areas. Let us divide [a,b] into n smaller intervals of the same widght h=b-1/n. On each interval there is a rectangle with the height y=r where r is the middle of that small interval on the x-axis. The area of that rectangle is hy. Write a Python function that takes a,b, and n as parameters and returns the approximate area of the region under the parabola y=x^2 using the above method. If you could please explain as to why your program works that would be helpful.
Thanks to helpful members, I found the following program(please edit the program for i am unable/ don't know how to
def parabola(x):
y = x*x
return y
def approx_area(fn, a, b, n):
"""
Approximate the area under fn in the interval [a,b]
by adding the area of n rectangular slices.
"""
a = float(a)
b = float(b)
area = 0.0
for slice in range(n):
left = a + (b-a)*slice/n
right = a + (b-a)*(slice+1)/n
mid = (left + right)*0.5
height = fn(mid)
width = right - left
area += height * width
return area
print "Area is", approx_area(parabola, -1.0, 1.0, 500)
However, I would need to place this under one entire function. Any ideas on how I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,通过将函数更改为 y = x 并尝试一些已知的输入值,我得出的结论是它工作正常:
如果您希望将所有内容都放在一个函数中,只需摆脱 parabola() ,从
approx_area()
函数(并调用)中删除第一个参数,然后将:更改为:
如:
请注意,我通常不会为家庭作业提供这么多明确的帮助,但是,由于您自己完成了大部分工作,因此只需轻轻一推即可将您推过界限。
我会警告您不要按原样提交此代码,因为简单的网络搜索很容易在这里找到它,并且您的成绩可能会因此受到影响。
检查它,彻底了解它是如何工作的,然后尝试自己重新编码,而不查看此源代码。相信我,这比盲目抄袭对你的职业生涯有更大的帮助。
为了让您理解此方法背后的理论,请考虑函数
y = x
的切片:顶部的中点 y 坐标(以及高度)为
(5 + 7) / 2
,或6
,宽度为2
,因此面积为12
。现在,这实际上是实际区域,但这只是因为我们使用的公式所致。对于非线性公式,由于顶部“线”的性质,会出现不准确的情况。具体来说,在你的情况下,抛物线是弯曲的。
但这些不准确性会变得越来越少,并且您使用的切片越来越薄,因为当您缩短任何线时,它都会趋向于直线(线性)。对于上述情况,如果将其分为两片,则面积将为
5.5 x 1
和6.5 x 1
,总共为12
。如果你的线不是直的,两片的答案会比一片的答案更接近现实。对于你的抛物线(但是从
x = 0 .. 1
开始,为了让我的生活更轻松,只需将x = -1 .. 1
的所有内容加倍,因为它围绕 y- 对称轴),单片解决方案中最坏的情况。在这种情况下,中点位于x = 0.5, y = 0.25
,当您将y
乘以1
的宽度时,您获得0.25
的面积。对于两个切片(宽度 =
0.5
),中点位于:因此估计面积为
0.3125
。对于四个切片(宽度 =
0.25
),中点位于:因此估计面积为
0.328125
。对于八个切片(宽度 =
0.125
),中点位于:因此估计面积为
0.332031248
。正如您所看到的,这越来越接近
1/3
的实际面积(我知道这一点是因为我了解微积分,见下文)。希望这将帮助您理解您所拥有的代码。
如果您真的想知道这是如何工作的,您需要研究微积分,特别是积分和微分。这些方法可以采用一个公式并为您提供另一个公式来计算直线的斜率和直线下方的面积。
但是,除非您要经常使用它并且需要真正的(数学)准确性,否则您可能可以只使用您正在学习的近似方法。
Okay, by changing the function to
y = x
and trying some known input values, I conclude that it works fine:If you want it all in one function, just get rid of
parabola()
, remove the first parameter from theapprox_area()
function (and call), then change:to:
as in:
Note that I wouldn't normally give this much explicit help for homework but, since you've done the bulk of the work yourself, it's only a small nudge to push you across the line.
I would warn you against handing in this code as-is since a simple web search will easily find it here and your grades may suffer for that.
Examine it, understand how it works thoroughly, then try to re-code it yourself without looking at this source. That will assist you far more in your career than just blind copying, trust me.
And just so you understand the theory behind this method, consider the slice of the function
y = x
:The midpoint y co-ordinate (and also the height) of the top is
(5 + 7) / 2
, or6
, and the width is2
so the area is12
.Now this is in fact the actual area but that's only because of the formula we're using. For a non-linear formula, there will be inaccuracies because of the nature of the "line" at the top. Specifically, in your case, a parabola is curved.
But these inaccuracies get less and less and you use thinner and thinner slices since any line tends towards a straight line (linear) as you shorten it. For the case above, if you divided that into two slices, the areas would be
5.5 x 1
and6.5 x 1
for a total of12
. If you line weren't straight, the two-slice answer would be closer to reality than the one-slice answer.For your parabola (but from
x = 0 .. 1
to make my life easier, just double everything forx = -1 .. 1
since it's symmetrical around the y-axis), the worst case in a one-slice solution. In that case, the midpoint is atx = 0.5, y = 0.25
and, when you multiply thaty
by the width of1
, you get an area of0.25
.With two slices (width =
0.5
), the midpoints are at:So the area estimate there is
0.3125
.With four slices (width =
0.25
), the midpoints are at:So the area estimate there is
0.328125
.With eight slices (width =
0.125
), the midpoints are at:So the area estimate there is
0.332031248
.As you can see, this is becoming closer and closer to the actual area of
1/3
(I know this since I know calculus, see below).Hopefully, that will assist you in understanding the code you have.
If you really want to know how this works, you need to look into calculus, specifically integration and differentiation. These methods can take a formula and give you another formula for calculating the slope of a line and the area under the line.
But, unless you're going to be using it a lot and need real (mathematical) accuracy, you can probably just use the approximation methods you're learning about.
http://en.wikipedia.org/wiki/Integral 上也有一个很好的可视化说明#Formal_definitions
我们查看 a 和 b 之间的抛物线部分,并将其划分为一组垂直矩形切片,使得每个矩形的顶部中心正好位于抛物线上。
这使得每个矩形的一个角“悬在”抛物线上方,而另一个角太低,留下未填充的空间;所以抛物线下的面积等于矩形的面积加上一点,减去一点。但我们如何比较这些位呢?矩形的面积是不是太大了,或者不够?
如果我们在矩形的顶部中心画一条与抛物线相切的线,我们就可以“剪掉”重叠的部分,翻过来,加到另一边;请注意,这不会改变矩形(现在是梯形)的总面积。
我们现在可以看到抛物线下方两侧都留有一点空间,因此梯形的面积略小于抛物线下方的面积。现在,我们可以将梯形顶部视为沿着抛物线底部形成一堆直线段(“线性分段近似”);并且线段下方的面积几乎与我们正在寻找的实际面积相同(但总是略小于)。
那么我们如何最小化“略小于”的量,让我们计算的面积更加准确呢?一种方法是使用曲线近似件代替直线;这会导致样条曲线(贝塞尔曲线、NURBS 等)。另一种方法是使用大量较短的线段,以“提高分辨率”。微积分将这个想法发挥到极致(双关语),使用无限数量的无限短的片段。
There is also a good visualization of this at http://en.wikipedia.org/wiki/Integral#Formal_definitions
We look at the section of the parabola between a and b, and we divide it into a set of vertical rectangular slices such that the top-center of each rectangle is exactly on the parabola.
This leaves one corner of each rectangle "hanging over" the parabola, and the other too low, leaving unfilled space; so the area under the parabola is equal to the area of the rectangle, plus a bit, minus a bit. But how do we compare the bits? Is the area of the rectangle a bit too much, or not quite enough?
If we draw a line tangent to the parabola at the top-center of the rectangle, we can "cut off" the overlapping bit, flip it over, and add it to the other side; note that this does not change the total area of the rectangle (now a trapezoid).
We can now see that there is a little bit of space left on either side under the parabola, so the area of the trapezoid is slightly less than the area under the parabola. We can now think of the trapezoid-tops as forming a bunch of straight-line segments (a "linear piecewise approximation") along the bottom of the parabola; and the area under the segments is almost the same as (but always slightly less than) the actual area we are seeking.
So how do we minimize the "slightly less than" amount, to make our calculated area more accurate? One way is to use curved approximation-pieces instead of straight lines; this leads into splines (Bezier curves, NURBS, etc). Another way is to use a larger number of shorter line-pieces, to "increase the resolution". Calculus takes this idea to the limit (pun intended), using an infinite number of infinitely short pieces.