简体   繁体   English

黎曼和 python

[英]Riemann sum python

I need to make a python function where i can find the surface with the riemann sum.我需要制作一个 python function ,在那里我可以找到带有黎曼和的曲面。 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.老师还说了一些关于 try-catch 的事情,这意味着我需要编写一个额外的代码来控制答案(如果我没记错的话)要找到表面,询问上限和下限以及在线下想要多少个矩形,例如该程序。

(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):定义 f(x):

return math.sqrt(x) #Function in the left! return math.sqrt(x) #函数在左边!

def positiveinput(message): while True: try: c = int(input(message)) 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...") print("糟糕。那不是有效的号码。再试一次...")

a = positiveinput("What is the lowerlimit?: ") a = positiveinput("下限是多少?:")

b = positiveinput("What is the upperlimit?: ") b = positiveinput("上限是多少?:")

c = positiveinput("How many division intervals do you want?: ") c = positiveinput("你想要多少个除法间隔?:")

a = int(input("What is the lowerlimit?:")) a = int(input("下限是多少?:"))

b = int(input("What is the upperlimit?:")) b = int(input("上限是多少?:"))

c = int(input("How many division intervals do you want?:")) c = int(input("你想要多少个除法间隔?:"))

dx = float((ba)/c) dx = 浮点数((ba)/c)

xi = a xi = 一个

Sum = dx总和 = dx

for i in range(0,c):对于范围内的 i(0,c):

xi = a - dx xi = a - dx

Sum = Sum + f(xi)总和 = 总和 + f(xi)

print("The surface under the line is ", (sum*dx)) print("线下的曲面是", (sum*dx))

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 ,!! 1)最重要的是,您实际上并没有计算出正确的答案,因为您假设下限等于 0。而不是写xi=0你应该写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). (请注意,这将使用每个矩形的远端来计算高度 - 如果您想使用下端,并且不想更改任何其他代码,则需要编写xi = a - dx以便您开始a . (话虽如此,我不会这样做,但这是在不更改任何其他内容的情况下解决此问题的方法)。

2) Validation errors: There are a few things you should check: 2)验证错误:您应该检查几件事:

  • That the values of a , b are valid numbers (note they shouldn't really have to be integers, just numbers). a , b的值是有效数字(注意它们不应该是整数,只是数字)。 You can use float() to convert something to a number, just as you would use int() to convert to an integer.您可以使用float()将某些内容转换为数字,就像使用int()转换为 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 ,n是 integer 并且不等于 0,因为这会引发错误,当您尝试除以n时,
  • that n is not negative, as this will result in you getting the wrong value (with the code as it is).n不是负数,因为这将导致您得到错误的值(使用代码原样)。

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.使用 for 循环然后增加你的值并不是一件非常 Pythonic 的事情。 You might be interested to learn you can actually specify a lower bound and an upper bound using the range function.您可能有兴趣了解您实际上可以使用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.我不会给你一个完整的解决方案,因为这是一个家庭作业,你自己解决它对你最有好处,但希望这能为你指明这些事情会为你指明正确的方向。

As @Sadap said, you can try something like this:正如@Sadap 所说,您可以尝试这样的事情:

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.以此代码为指导,您可以完成@tim-mccurrach 在此帖子的答案中编写的错误验证列表。 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 .有关try-catch的文档,您可以输入此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM