0

I need to make a python function where i can find the surface with the riemann sum. This is what i have , and with the feedback of my teacher i am very close to it, but it does not work as properly as i want. the teacher said also something about try-catch what means that i need to make an extra code to control the answer (if im not wrong) To find the surface the uppper and the lower limits are asked and how many rectangles you want under the line like in the program.

(edit) I have made a new program , could you guys check if this is correct.

import math

def f(x): return math.sqrt(x) #Function in the left!

a = int(input("What is the lowerlimit?:"))
b = int(input("What is the upperlimit?:"))
n = int(input("How many division intervals do you want?:"))

dx = (b-a)/n;

xi = 0;
sum = 0;
for i in range(n):
xi = xi+dx;
sum = sum + f(xi)
print("The surface under the line is ", (sum*dx))

#einde programma!






import math

def f(x):

return math.sqrt(x) #Function in the left!

def positiveinput(message): while True: try: c = int(input(message))

  if c <= 0:
      raise ValueError

    break

except ValueError:

print("Oops! That was no valid number. Try again...")

a = positiveinput("What is the lowerlimit?: ")

b = positiveinput("What is the upperlimit?: ")

c = positiveinput("How many division intervals do you want?: ")

a = int(input("What is the lowerlimit?:"))

b = int(input("What is the upperlimit?:"))

c = int(input("How many division intervals do you want?:"))

dx = float((b-a)/c)

xi = a

Sum = dx

for i in range(0,c):

xi = a - dx

Sum = Sum + f(xi)

print("The surface under the line is ", (sum*dx))

Emre Emre
  • 21
  • 3
  • 1
    I think your teacher means that you should check the input before converting to an int. Because if you enter `abc` instead of a number your program will crash – bb1950328 Jun 06 '20 at 08:07
  • "it does not work as properly as i want." Please explain - give an example of a situation where something wrong happens, explain what happens that is wrong, and what should happen instead. – Karl Knechtel Jun 06 '20 at 08:21
  • at a low interval there is too great of a deviation – Emre Emre Jun 06 '20 at 08:45
  • @EmreEmre See my answer, the reason you are going wrong at low lower limits is because you are not setting the correct lower limit. It should be `a` not `0` – tim-mccurrach Jun 06 '20 at 09:02

2 Answers2

1

There are a few issues with the code above:

1) Most importantly, You don't actually calculate the correct answer because you are assuming that the lower limit is equal to 0. Instead of writing xi=0 you should be writing xi=a!!! (Note that this will use the far end of each rectangle to calculate the height - if you want to use the lower end, and don't want to change any other code you will need to write xi = a - dx so you start on a. (Having said that, I wouldn't do it this way, but this is how to fix this without changing anything else).

2) Validation errors: There are a few things you should check:

  • That the values of a, b are valid numbers (note they shouldn't really have to be integers, just numbers). You can use float() to convert something to a number, just as you would use int() to convert to an integer.
  • that n is an integer and is not equal to 0, as that will raise an error, when you try and divide by n,
  • that n is not negative, as this will result in you getting the wrong value (with the code as it is).

Having said that, I would not write the code as it is. Using a for-loop and then incrementing your value is not a very pythonic thing to do. You might be interested to learn you can actually specify a lower bound and an upper bound using the range function. Experiment with:

for i in range(3,11,0.5):
   print(i)

See what happens. I'm not going to give you a full solution, as this is a homework assignment, and it will benefit you most to work it out yourself, but hopefully this points you these things will point you in the right direction.

tim-mccurrach
  • 6,395
  • 4
  • 23
  • 41
0

As @Sadap said, you can try something like this:

def positiveinput(message):
    while True:
      try:
        n = int(input(message))
        if n <= 0:
          raise ValueError
        break
      except ValueError:
        print("Oops!  That was no valid number.  Try again...")

a = positiveinput("What is the lowerlimit?:")
b = positiveinput("What is the upperlimit?:")
n = positiveinput("How many division intervals do you want?:")

Having this code as a guide, you can complete the list of errors verification that @tim-mccurrach have written in an answer to this post. Also you can check out this link where they make the Riemann Sum in a different way. For documentation about try-catch you can enter this link.

MrNobody33
  • 6,413
  • 7
  • 19
  • 1
    Though this works, this is not optimal in terms of user experience, as an error in the last input forces the user to start all over again. – Thierry Lathuille Jun 06 '20 at 08:23