2013-09-26 68 views
-1

我需要幫助編寫一個程序,該程序將使用黎曼定義(左和右規則)來計算f(x)=sin(x)a=0b=2*pi的積分。我可以手工做幾天,但我沒有想法如何使用python進行編碼。Riemann sum in python

+1

,你能否告訴我們到目前爲止你已經嘗試了什麼?代碼示例? – amitparikh

+4

如果您可以手動完成,請執行您所做的每一步並將其轉換爲Python代碼。 – dornhege

+0

你有沒有看到這個問題?它看起來非常相似。 http://stackoverflow.com/questions/17687756/numerical-integration-with-riemann-sum-python – sk8asd123

回答

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) 
+0

這使得我需要做得更清楚。我只需要爲每個方法創建一個循環(左和右)。 –