【问题标题】:Riemann sum in pythonpython中的黎曼和
【发布时间】:2013-09-26 16:11:22
【问题描述】:

我需要帮助编写一个程序,该程序将使用黎曼定义(左右规则)来计算 f(x)=sin(x)a=0b=2*pi 的积分。我可以手动完成几天,但我对如何用 python 编写代码一无所知。

【问题讨论】:

  • 你能告诉我们你到目前为止尝试了什么吗?代码示例?
  • 如果您可以手动完成,请执行您所做的每一步并将其转换为 python 代码。
  • 你看到这个问题了吗?它看起来非常相似。 stackoverflow.com/questions/17687756/…

标签: python integration


【解决方案1】:

你看过这段代码了吗:http://statmath.org/calculate_area.pdf

# Calcuate the area under a curve
#
# Example Function y = x^2
#
# This program integrates the function from x1 to x2
# x2 must be greater than x1, otherwise the program will print an error message.
#
x1 = float(input('x1='))
x2 = float (input('x2='))
if x1 > x2:
print('The calculated area will be negative')
# Compute delta_x for the integration interval
#
delta_x = ((x2-x1)/1000)
j = abs ((x2-x1)/delta_x)
i = int (j)
print('i =', i)
# initialize
n=0
A= 0.0
x = x1
# Begin Numerical Integration
while n < i:
delta_A = x**2 * delta_x
x = x + delta_x
A = A + delta_A
n = n+1
print('Area Under the Curve =', A)

【讨论】:

  • 这让我需要做的事情更加清晰。我只需要为每种方法(左和右)创建一个循环。
【解决方案2】:

根据我的经验,查看 wiki 中的方程式有助于我翻译成 python。以下是一些 wiki 页面:

Riemann definition

Fundamental theorem of calculus

Numerical integration

另外,python 的数学模块可以帮助你:

Python Math

检查完这些后,看看python语言中其他数学方程的一些例子,以了解如何集成一些数学函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    相关资源
    最近更新 更多