我做了一个计算器,可以近似任何给定的函数作为输入。他们后来想用它来计算一个积分,但写下:
function = str(input("The function that must be expanded and integrated: "))
它不打印数字,而是打印值。这是我的代码:
from sympy.functions import sin,cos,tan
from sympy.abc import x
from sympy import *
from sympy import series
from math import *
function = str(input("The function that must be expanded and integrated: "))
x0 = int(input("Point of development: "))
n = int(input("Amount of expressions: "))
print(series(function, x, x0, n))
N = int(input("Amount of summs (Bigger number is more accurate but takes longer time): "))
a = int(input("Integrate from: "))
b = int(input("Integrate to: "))
# We will use the midpoint method to integrate the function
def integrate(N, a, b):
def f(x):
return series(function, x, x0, n)
value=0
value=2
for n in range(1, N+1):
value += f(a+((n-(1/2))*((b-a)/N)))
value2 = ((b-a)/N)*value
return value2
print("...................")
print("Here is your answer: ")
print(integrate(N, a, b))
我想,这是因为我的输入是一个字符串。但是,我不能选择输入为整数,因为exp(-x**2)
不是整数。如果是这样的话,我怎么能在计算器中输入任何函数后仍然得到一个值呢
您的代码中存在一些重要问题:
f(x)
中的全局变量和参数x
也是如此。如果要在同一范围内使用全局变量和局部变量,请不要使用相同的名称李>f(x)
的返回值是一个sympy
表达式,而不是一个值,这就是为什么您得到的是输出李>经过一些重构并使用^{} 和^{} 之后:
从{}集成到{}的{}的一些输出在{}扩展。分析结果为
8/3=2.6666666...
您可以使用^{} to "convert a SymPy expression into a function that allows for fast numeric evaluation" 。对于
N=1000
的情况,加速是显著的相关问题 更多 >
编程相关推荐