Calculating Riemann Sums on Python w/ Numpy

In summary, the conversation discussed writing two codes in Python for calculating the definite integral using a Riemann sum with left and right endpoints. The code provided for the left endpoint was confirmed to work, and the conversation focused on finding a solution for the right endpoint. Suggestions were made to either create a separate function for the right endpoint or modify the existing code by changing the line that calculates the values of f to include an additional parameter. The solution was found by changing the line to y=f(x+dx). Another suggestion was to use x=np.arange(a+dx, b, step=dx) instead.
  • #1
ver_mathstats
260
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
  • #2
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.
 
  • #3
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).
 
  • #4
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)
 
  • #5
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.
 

1. What is a Riemann Sum?

A Riemann Sum is a method for approximating the area under a curve by dividing it into smaller rectangles and summing the areas of those rectangles. It is used in calculus to solve problems involving integration.

2. Why use Python and Numpy for calculating Riemann Sums?

Python is a popular programming language that is known for its ease of use and readability, making it a great choice for scientific calculations. Numpy is a powerful library for scientific computing that provides efficient data structures and functions for numerical operations.

3. How do I calculate a Riemann Sum using Python and Numpy?

To calculate a Riemann Sum in Python using Numpy, you will need to define the function that represents the curve you are trying to find the area under. Then, you can use the numpy.trapz() function to calculate the area under the curve using the Riemann Sum method.

4. What are the benefits of using Numpy's trapz() function for Riemann Sums?

The numpy.trapz() function is optimized for calculating Riemann Sums, making it faster and more accurate than using a simple for loop. It also allows for easy integration with other numpy functions and data structures.

5. Are there any limitations to using Python and Numpy for Riemann Sums?

While Python and Numpy are powerful tools for scientific computing, they may not be suitable for extremely complex calculations or for handling large datasets. Additionally, the accuracy of the Riemann Sum method may be limited by the number of rectangles used in the approximation.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
Back
Top