我正在编写一个计算旋转体积的程序。其中第一步是计算积分。我使用
以下是我目前的进展:
scipy.integrate
进行计算,但我无法找到最好的方法来让方程(例如x=x ** 2
)在命令行中输入。我最初计划添加一个参数“相对于:x | y”,然后将函数作为lambda表达式传递。不幸的是,argparse
不会接受lambda作为参数类型,并且尝试使用字符串构造lambda表达式(f = lambda x:args.equation
)只会返回一个字符串(可以理解)。以下是我目前的进展:
import sys
import argparse
import math
from scipy import integrate
parser = argparse.ArgumentParser(description='Find the volume of the solid of rotation defined')
parser.add_argument('equation', help='continous function')
parser.add_argument('a', type=float, help='bound \'a\'')
parser.add_argument('b', type=float, help='bound \'b\'')
parser.add_argument('-axis', metavar='x|y', help='axis of revolution')
args = parser.parse_args()
def volume(func, a, b, axis=None):
integral = integrate.quad(func, a, b)
return scipy.py * integral
print volume(args.equation, args.a, args.b)
非常感谢您的建议和帮助。
谢谢。