Calculating Riemann Sums on Python w/ Numpy

Click For Summary

Discussion Overview

The discussion revolves around implementing Riemann sums in Python using NumPy, specifically focusing on calculating definite integrals with left and right endpoints. Participants share code snippets and seek assistance in modifying their existing code to achieve the desired functionality.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant shares a code snippet for calculating the left endpoint Riemann sum and confirms its accuracy by comparing it to a calculator.
  • Another participant suggests creating two separate functions for left and right endpoint calculations or modifying the existing function to include a parameter for the type of sum.
  • It is noted that the left endpoint sum calculates function values at specific intervals, while the right endpoint sum should calculate values at shifted intervals.
  • A participant mentions that changing the line to use `y=f(x+dx)` works for the right endpoint calculation.
  • Another participant proposes using `x=np.arange(a + dx,b,step=dx)` as an alternative approach for defining the range for the right endpoint sum.

Areas of Agreement / Disagreement

Participants generally agree on the need to adjust the code for right endpoint calculations, but there are multiple proposed methods for achieving this, indicating a lack of consensus on the best approach.

Contextual Notes

Some participants' suggestions depend on specific interpretations of how to implement the right endpoint calculation, which may lead to different results based on the chosen method.

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.
 

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
4K
Replies
1
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K