Python Calculating Riemann Sums on Python w/ Numpy

Click For Summary
The discussion centers on implementing numerical integration using Riemann sums in Python, specifically for left and right endpoints. The user successfully created a function for left endpoint integration but seeks guidance on modifying it for right endpoint integration. Key points include the suggestion to create a separate function for right endpoints or to add a parameter to the existing function to handle both cases. The user learns that for right endpoint calculations, the function should evaluate at shifted x-values, specifically using `x=np.arange(a + dx, b, step=dx)`. This adjustment allows the function to correctly compute the integral using right endpoints, confirming the effectiveness of the solution.
ver_mathstats
Messages
258
Reaction score
21
Code:
import numpy as np

def num_int(f,a,b,n):
    dx=(b-a)/n
    x=np.arange(a,b,step=dx)
    y=f(x)
    return y.sum()*dx

def rational_func(x):
    return 1/(1+x**2)

print(num_int(rational_func,2,5,10))

Here is my code for the left endpoint, I know this code works because I compared it to an actual calculator the two values and they ended up being the same, however I am struggling to figure out how to do the right endpoint code, I'm not exactly sure what to change.

Thank you.
 
Technology news on Phys.org
ver_mathstats said:
Homework Statement:: Construct two codes using python for the definite integral using a Riemann sum with left endpoints and right endpoints.
Relevant Equations:: [a,b]=[2,5]
f(x)=1/(1+x^2)

Code:
import numpy as np

def num_int(f,a,b,n):
    dx=(b-a)/n
    x=np.arange(a,b,step=dx)
    y=f(x)
    return y.sum()*dx

def rational_func(x):
    return 1/(1+x**2)

print(num_int(rational_func,2,5,10))

Here is my code for the left endpoint, I know this code works because I compared it to an actual calculator the two values and they ended up being the same, however I am struggling to figure out how to do the right endpoint code, I'm not exactly sure what to change.

I would write the code with two integration functions: num_int_left() -- which you have -- and num_int_right() -- which is almost identical to the code you have, with only a small change in one line.

Alternatively, you could have a single function with an additional parameter that indicates whether you want left sums or right sums.

Thank you.
Using the values you entered, your left endpoint Riemann sum calculates the values of f at 2, 2.3, 2.6, 2.9. 3.2, 3.5, 3.8, 4.1, 4.4, and 4.7. For the right endpoint Riemann sum, you want the code to calculate the values at 2.3, 2.6, ..., 4.7, and 5.0. Should be easy enough to figure out how to do that.
 
Mark44 said:
Using the values you entered, your left endpoint Riemann sum calculates the values of f at 2, 2.3, 2.6, 2.9. 3.2, 3.5, 3.8, 4.1, 4.4, and 4.7. For the right endpoint Riemann sum, you want the code to calculate the values at 2.3, 2.6, ..., 4.7, and 5.0. Should be easy enough to figure out how to do that.
Thank you for the help, it works when I change the line y=f(x) to y=f(x+dx).
 
ver_mathstats said:
Thank you for the help, it works when I change the line y=f(x) to y=f(x+dx).
That works, but what I was thinking of was x=np.arange(a + dx,b,step=dx)
 
Mark44 said:
That works, but what I was thinking of was x=np.arange(a + dx,b,step=dx)
Oh okay I understand, thank you.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K