python中的Riemann和

2024-05-19 12:02:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要帮助编写一个程序,该程序将使用Riemann定义(左规则和右规则)来计算f(x)=sin(x)a=0b=2*pi的积分。我可以用手工操作好几天,但是我不知道如何用python编写代码。


Tags: 代码程序定义规则pisinriemann手工操作
2条回答

你看了这段代码吗: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)

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

Riemann definition

Fundamental theorem of calculus

Numerical integration

此外,python的数学模块将帮助您:

Python Math

在检查完这些之后,看看python语言中其他数学公式的一些示例,了解如何集成一些数学函数。

相关问题 更多 >

    热门问题