-1

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

4

2 回答 2

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)
于 2013-09-26T16:31:23.993 回答
0

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

黎曼定义

微积分基本定理

数值积分

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

Python数学

在检查完这些之后,再看一些 Python 语言中其他数学方程的例子,以了解如何整合一些数学函数。

于 2013-09-26T16:25:17.673 回答