我想绘制函数x ^ -x及其衍生物。然而,衍生品变得乏味可编。因此,我想让Python为我找到衍生物,然后绘制它们然而我不断收到语法错误。这是我到目前为止所做的。
import math
import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
x,y,z = sp.symbols('x y z')
n=1 # order of derivative
def f(x):
g=z**(-z)
h=sp.diff(g,z,n)
q=h.subs(z,x)
return q
x1=np.arange(0,5,0.5)
plt.plot(x1,f(x1))
plt.show()
错误消息的完整引用如下:
SyntaxError Traceback (most recent call last)
C:\Users\Nathaniel\Anaconda3\lib\site-packages\sympy\core\sympify.py in
sympify(a, locals, convert_xor, strict, rational, evaluate)
321 a = a.replace('\n', '')
--> 322 expr = parse_expr(a, local_dict=locals,
transformations=transformations, evaluate=evaluate)
323 except (TokenError, SyntaxError) as exc:
C:\Users\Nathaniel\Anaconda3\lib\site-packages\sympy\parsing\sympy_parser.py
in parse_expr(s, local_dict, transformations, global_dict, evaluate)
893
--> 894 return eval_expr(code, local_dict, global_dict)
895
C:\Users\Nathaniel\Anaconda3\lib\site-packages\sympy\parsing\sympy_parser.py
in eval_expr(code, local_dict, global_dict)
806 expr = eval(
--> 807 code, global_dict, local_dict) # take local objects in
preference
808
SyntaxError: invalid syntax (<string>, line 1)
During handling of the above exception, another exception occurred:
SympifyError Traceback (most recent call last)
<ipython-input-2-13b1385304a2> in <module>()
13
14 x1=np.arange(0,5,0.5)
---> 15 plt.plot(x1,f(x1))
16 plt.show()
<ipython-input-2-13b1385304a2> in f(x)
9 g=z**(-z)
10 h=sp.diff(g,z,n)
---> 11 q=h.subs(z,x)
12 return q
13
C:\Users\Nathaniel\Anaconda3\lib\site-packages\sympy\core\basic.py in
subs(self, *args, **kwargs)
851 for i in range(len(sequence)):
852 o, n = sequence[i]
--> 853 so, sn = sympify(o), sympify(n)
854 if not isinstance(so, Basic):
855 if type(o) is str:
C:\Users\Nathaniel\Anaconda3\lib\site-packages\sympy\core\sympify.py in
sympify(a, locals, convert_xor, strict, rational, evaluate)
322 expr = parse_expr(a, local_dict=locals,
transformations=transformations, evaluate=evaluate)
323 except (TokenError, SyntaxError) as exc:
--> 324 raise SympifyError('could not parse %r' % a, exc)
325
326 return expr
SympifyError: Sympify of expression 'could not parse '[ 0. 0.5 1. 1.5
2. 2.5 3. 3.5 4. 4.5]'' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)
我如何让Python以快速有效的方式绘制它?
答案 0 :(得分:1)
sympy
中的差异函数似乎在mpath
library中。也许有其他方式,经验丰富的sympy
用户如何解决问题,但这是一个解决方案:
import matplotlib.pyplot as plt
from mpmath import diff
from cycler import cycler
#create cycler objects that matplotlib uses as linestyles
col = cycler("color", ["r", "b", "g"])
styl = cycler("linestyle", ["-", "-."])
plt.rc('axes', prop_cycle = styl * col)
#define function to differentiate
def g(z):
return z ** (-z)
#define the differentiation function
def f(xrange):
return [diff(lambda x: g(x), xr, n) for xr in xrange]
#I prefer linspace over arange because you don't run into float point problems
x1 = np.linspace(1, 5, 100)
#cycle through derivative order
for n in range(5):
plt.plot(x1, f(x1), label = "Order = {}".format(n))
plt.legend()
plt.show()
每个函数的线条样式可以通过使用cycler objects.来定义。当然还有其他方法来控制colour of the lines in matplotlib
。
输出: