rightriemannsum.py

Created by tracy-pratt-1

Created on August 19, 2025

1.12 KB


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+1] * width

    print("Right 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+1], width)
    text(x_min+0.5,y_max-0.5,"RR="+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.