|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import random
- import math
- class Function:
- global dx
- dx = 0.000001
- def __init__(self, cor_law="pow(x,2)"):
- self.cor_law = cor_law
- def getValue(self,x):
- try:
- y = eval(self.cor_law)
- return y
- except:
- return "函数在该点处没意义"
- def derivate(self,x0):
- try:
- y0 = self.getValue(x0)
- x1 = x0 + dx
- y1 = self.getValue(x1)
- return (y1 - y0)/(x1 - x0)
- except:
- return "函数不可导"
- def integral(self,up,down):
- try:
- s = 0
- if up > down:
- while down < up:
- yi = self.getValue(down)
- s += yi*dx
- down += dx
- else:
- while up < down:
- yi = self.getValue(up)
- s -= yi*dx
- up += dx
- return s
- except:
- return "函数不可积"
- if __name__ == "__main__":
- fun = Function(cor_law='math.sin(x)')
- print(fun.getValue(3.1415926))
- print(fun.derivate(0))
- print(fun.integral(3.14159, 0))
复制代码 |
|