Discrepancies between two methods of the left rectangle integral method for the identity function

28 views Asked by At

I have written what I think are exactly the same program for computing the integral of the identity function:

#first method
def area1(a,b,N):
    S=0
    p=(b-a)/N
    for i in range (N):
        x=a+p*i
        S=S+p*(x)
        print(S)
    return S
   
   
#second method
def area2(a,b,N):
    s=0
    p=(b-a)/N
    x=a
    for i in range(N):
        s=s+p*x
        x=x+p
        print(s)
    return s

In both cases we are using the left rectangle method to compute this integral.

However, after trying both programs with N=100 (with Pydroid 3, on my phone) I find an extremely tiny difference of about 0.0000000001 between the two methods.

I would like to know if this difference can be explained by the fact that not exactly the same operations are performed in both programs (in the program 1, one addition and one multiplication and in program 2, just one addition) ?

Note, I am not counting the S= S+p*x operation in this counting.


I expected both program to retrieve the same result, but they didn't.

I think it's because of the number of operations performed in each program (there's a difference of 1 operation between the two).

However, I don't know by which mechanism this difference in number of operations affects the results of the programs.

0

There are 0 answers