r/pythonhelp • • Oct 11 '22

trying to make a Reimann sum program

Like I said the last time I posted here, I am new to python, so I am struggling more than I expected. I have experience in HTML, CSS, C++, C, C#, some Java, and some JavaScript.

# A function that is in charge of prompting the user for any input they
# give. It receives an argument representing the required data, prompts
# the user for that data, and then returns it to the calling statement.
def limits ():
    a = int(input("What is the value of \"a\":"))
    b = int(input("What is the value of \"b\":"))
    return a, b
# A function that is in charge of printing out the introductory
# statement(s) for the program.
def introduction():
    print("This progrram will calculate the integral of the function f(x) = 3x^3 - 2x^2 between user defined limits: a and b")
# A function that prints out the statement about the accuracy of delta.
def accuracy():
    print("The accuracy of this calculation depends on the value of delta that you use.")
    
# A function to evaluate the given mathematical formula at a given
# point. It takes a numerical argument that represents x, and returns
# the result of f(x).
def f(x):
    y = (3*(x**3))-(2(x**2))
    return y
# A function that calculates an approximation of the integral of f(x)
# using the riemann sum. It takes three arguments that represent the
# lower limit, the upper limit, and the delta value. It then returns the
# integral approximation as a result to the calling statement.
def integral(a, b, delta):
        integral = delta*(sum(a,b))

########################### MAIN ##################################
# In the space below, use the functions defined above to solve the
# problem.
###################################################################

# Print the introductory statements of the program
intro = introduction()
# Prompt the user for both the lower and upper limits
a , b = limits()

# Print the statements about delta
accuracy = accuracy()
# Promt the user for the delta value
delta = float(input("What is the value of \"delta\":"))
# Calculate the integral approximation
Area = integral(a, b, delta)
# Print out the result.
print(Area)

when I try to run this, my code gives me an error that reads:

Exception has occurred: TypeError
'int' object is not iterable
  File "C:\Users\rayro\OneDrive\Documents\Fall22\CSC 130\03 Riemann Sum.py", line 33, in integral
    integral = delta*(sum(a,b))
  File "C:\Users\rayro\OneDrive\Documents\Fall22\CSC 130\03 Riemann Sum.py", line 50, in <module>
    Area = integral(a, b, delta)

Edit: I went back and changed (a,b) in the integral variable definition to (f(a), f(b)). this is my new error code

Exception has occurred: TypeError
'int' object is not callable
  File "C:\Users\rayro\OneDrive\Documents\Fall22\CSC 130\03 Riemann Sum.py", line 26, in f
    y = (3*(x**3))-(2(x**2))
  File "C:\Users\rayro\OneDrive\Documents\Fall22\CSC 130\03 Riemann Sum.py", line 33, in integral
    integral = delta*(sum(f(a),f(b)))
  File "C:\Users\rayro\OneDrive\Documents\Fall22\CSC 130\03 Riemann Sum.py", line 50, in <module>
    Area = integral(a, b, delta)
3 Upvotes

3 comments sorted by

2

u/Goobyalus Oct 11 '22

Wow, it's rare to get properly formatted code and complete error output. I commend you.


It points out this line:

        integral = delta*(sum(a,b))

because sum's arguments are as follows:

sum(iterable, /, start=0)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers

So you've passed in a as iterable, and b as start. If we wanted to get the sum of a and b, we could package them into an iterable like a tuple (sum((a, b))) or a list (sum([a, b])) so that we're calling sum((a, b), start=0). Or simply a + b.

1

u/rayroy1103 Oct 11 '22

So I was trying to sum all the values between f(a) to f(b), as you would when finding an area under a curve.

2

u/Goobyalus Oct 11 '22

To do that you would need

  1. Each x value to put into the function

  2. Each y value that comes from putting these x values into the function

  3. The sum of all of these y values

sum doesn't know about f(x), it only sums up the elements of the collection that is given to it.