我有一个功能
def u(x, t):
return math.erf((x + 1) / (2 * (k * t) ** (1 / 2)))
**如果我错了,请纠正我。在
我有以下所有必要的进口货物:
^{pr2}$以及定义符号
x, k, t = symbols('x k t')
这样做非常好:
def f(x):
return x ** 4
diff(f(x))
返回正确答案
4x^3
然而,这
diff(u(x, t))
或者这个
diff(u(x, t), t)
返回如下错误
TypeError Traceback (most recent call last) in () ----> 1 diff(u(x, t))
in u(x, t) 1 def u(x, t): ----> 2 return math.erf((x + 1) / (2 * (k * t) ** (1 / 2)))
C:\Anaconda\lib\site-packages\sympy\core\expr.py in float(self) 223 if result.is_number and result.as_real_imag()1: 224 raise TypeError("can't convert complex to float") --> 225 raise TypeError("can't convert expression to float") 226 227 def complex(self):
TypeError: can't convert expression to float
在Matlab中,我可以轻松做到:
syms x;
syms k;
syms t;
u = erf((x + 1)/(2 * sqrt(k * t)));
LHS = simplify(diff(u, t))
RHS = k * simplify(diff(u, x, 2))
我的问题是,如何区分和/或简化Python中多个变量的数学函数?在
您需要使用
sympy.erf
,而不是math.erf
:像这样使用
sympy
:你必须指定要区分的变量。在
相关问题 更多 >
编程相关推荐