leftriemannsum.py

Created by tracy-pratt-1

Created on November 13, 2025

1.12 KB

This script runs automatically. If loaded to your calculator you not import it to your shell automatically.

Calculates a Leftriemannsum of a set of points. First input your x-values in a list Example: 1,2,3,4 Then input your y-values in a list Example: 1,4,9,16

See sum, and graph.


from math import *
from cmath import *
from matplotlib.pyplot import *

# --- Input Phase ---
print("Separated using commas")
print("Enter x-values:")
x_vals = [float(i) for i in input().split(",")]

print("Enter y-values:")
y_vals = [float(i) for i in input().split(",")]

n = len(x_vals)
if len(y_vals) != n:
    print("Error: x and y must have same length")
else:
    # --- Compute Left Riemann Sum ---
    total = 0
    for i in range(n-1):
        width = x_vals[i+1] - x_vals[i]
        total += y_vals[i] * width

    print("Left Riemann Sum =", total)

    # --- Wait before plotting ---
    input("Press Enter to see graph...")

    # --- Plotting ---
    x_min = int(min(x_vals)-2)
    x_max = int(max(x_vals)+2)
    y_min = int(min(y_vals)-2)
    y_max = int(max(y_vals)+2)
    
    # Plot the (x,y) points
    axis((x_min,x_max,y_min,y_max))

    # Draw left Riemann sum rectangles
    for i in range(n-1):
        width = x_vals[i+1]-x_vals[i]
        bar(x_vals[i]+width/2, y_vals[i], width)
    text(x_min+0.5,y_max-0.5,"LR="+str(round(total,2)))
    plot(x_vals, y_vals, color='red',zorder=2)

    show()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.