Question: Fix this python code to work on the ti inspire for Riemann sums (it works normally): import math def user_function(x, function_string): return

Fix this python code to work on the ti inspire for Riemann sums (it works normally):
 

import math

def user_function(x, function_string):
    return eval(function_string)

def left_rule(f, a, b, n, function_string):
    h = (b - a) / n
    sum = 0
    for i in range(n):
        sum += f(a + i * h, function_string)
    return sum * h

def right_rule(f, a, b, n, function_string):
    h = (b - a) / n
    sum = 0
    for i in range(1, n + 1):
        sum += f(a + i * h, function_string)
    return sum * h

def midpoint_rule(f, a, b, n, function_string):
    h = (b - a) / n
    sum = 0
    for i in range(n):
        sum += f(a + (i + 0.5) * h, function_string)
    return sum * h

def trapezoidal_rule(f, a, b, n, function_string):
    h = (b - a) / n
    sum = 0.5 * (f(a, function_string) + f(b, function_string))
    for i in range(1, n):
        sum += f(a + i * h, function_string)
    return sum * h

def infinite_riemann_sum(f, a, b, n, function_string, epsilon=1e-6):
    prev_sum = None
    current_sum = trapezoidal_rule(f, a, b, n, function_string)
    while prev_sum is None or abs(current_sum - prev_sum) > epsilon:
        prev_sum = current_sum
        n *= 2
        current_sum = trapezoidal_rule(f, a, b, n, function_string)
    return current_sum

a = float(input("Enter the value of a: "))
b = float(input("Enter the value of b: "))
n = int(input("Enter the value of n: "))
function_string = input("Enter the function using 'x' as the variable (e.g., math.sin(x)): ")

print("Left Riemann Sum: ", left_rule(user_function, a, b, n, function_string))
print("Right Riemann Sum: ", right_rule(user_function, a, b, n, function_string))
print("Midpoint Rule: ", midpoint_rule(user_function, a, b, n, function_string))
print("Trapezoidal Rule: ", trapezoidal_rule(user_function, a, b, n, function_string))
print("Infinite Riemann Sum: ", infinite_riemann_sum(user_function, a, b, n, function_string))
 

 

The inspire returns this error when I run code:

 

#Running Riemann.py
from Riemann import *
Enter the value of a: 0
Enter the value of b: 3
Enter the value of n: 2
Enter the function using 'x' as the variable (e.g.,
math.sin(x)): x
Traceback (most recent call last):
  File "", line 2, in
  File "/Users/omsanan/Library/Preferences/Tex
as Instruments/TI-Nspire CX CAS Student Soft
ware/python/doc2/Riemann.py", line 48, in ule>
  File "/Users/omsanan/Library/Preferences/Tex
as Instruments/TI-Nspire CX CAS Student Soft
ware/python/doc2/Riemann.py", line 10, in left_r
ule
  File "/Users/omsanan/Library/Preferences/Tex
as Instruments/TI-Nspire CX CAS Student Soft
ware/python/doc2/Riemann.py", line 4, in user_f
unction
  File "", line 2
SyntaxError: invalid syntax

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!