magnetar
magnetar

Reputation: 6577

Translating a function integrating another function from BASIC into Python

The book Calculus and Pizza by Clifford Pickover has a few code examples here and there, all written in some dialect of BASIC.

I wrote a Python version of the code example covering integration. His BASIC example goes like:

10 REM Integration
20 DEF FNY(X) = X*X*X
30 A = 0
40 B = 1
50 N = 10
55 R = 0
60 H = (B-A)/N
70 FOR X = A TO B - H/2 STEP H
80     R = R + FNY(X)
90 NEXT X
100 R = R * H
110 PRINT *INTEGRATION ESTIMATE*: R

I changed a few things here and there, allowing the user to specify the interval over which to take the integral, specify the function to be integrated as a lambda, and so forth. I knew right off the bat that the for loop wouldn't work as I have written it below. I'm just wondering if there's some direct or idiomatic translation of the BASIC for to a Python for.

def simpleintegration():
    f = eval(input("specify the function as a lambda\n:%"))
    a = int(input("take the integral from x = a = ...\n:%"))
    b = int(input("to x = b = ...\n:%"))
    n = 10
    r = 0
    h = (b-a)/n
    for x in range(a,b-h/2,h):
        r = r + f(x)
    r = r * h
    print(r)

Upvotes: 0

Views: 116

Answers (3)

Sahal Khalani
Sahal Khalani

Reputation: 1

You can do as below, just changing iteration of 'i' in for loop.

def simpleintegration():
    f = eval(input("specify the function as a lambda\n:%"))
    a = int(input("take the integral from x = a = ...\n:%"))
    b = int(input("to x = b = ...\n:%"))
    n = 10
    r = 0
    h = (b-a)/n
    for x = a to b-h/2 step h:
        r = r + f(x)
    r = r * h
    print(r)

Upvotes: 0

Lutz Lehmann
Lutz Lehmann

Reputation: 26040

The formula is computing the Riemann sums using the values at the left end of the subdivision intervals. Thus the last used value for X should be B-H.

Due to floating point errors, stepping from A by H can give a last value that is off by some small amount, thus B-H is not a good bound (in the BASIC code) and B-H/2 is used to stop before X reaches B.

The Python code should work in the presented form for the same reasons, since the bound B-H/2 is unreachable, thus the range should stop with B-H or a value close by.

Using a slight modification you can actually compute the trapezoidal approximation, where you initialize with R=f(A)/2, step X from A+H to including B-H adding f(X) to R and then finish by adding f(B)/2 (which could already be done in the initialization). As before, the approximation of the integral is then R*H.

Upvotes: 0

John Y
John Y

Reputation: 14529

Your translation isn't far off. The only difference between the for loop in other languages and Python's "loop-over-a-range" pattern is that the "stop" value is usually inclusive in other languages, but is exclusive in Python.

Thus, in most other languages, a loop including a and b looks like

for i = a to b step c
    ' Do stuff
next i

In Python, it would be

for i in range(a, b + 1, c):
    # Do stuff

Upvotes: 1

Related Questions