有没有一种方法可以绘制带有参数的 sympy 函数并对其进行数值积分?
Is there a method to plot and numerically integrate a sympy function with parameters?
我正在 Jupyter notebook 中进行一些符号计算,我想在继续其他符号计算之前绘制函数并计算数值积分。
我曾尝试对函数进行 lambdify,但只有重新输入函数定义才能得到绘图和积分。有没有办法直接用符号形式进行这些操作?如果没有,有没有办法用固定的输入参数 a 和 b 正确地 lambdify 函数?
from sympy import *
from sympy.utilities.lambdify import lambdify
import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as int
x,a,b = symbols('x a b')
u = log(exp(-b*x**2/2) + 1/a)
t = np.linspace(-5,5,100)
# Attempt to lambdify u:
f = (lambda x,a=1,b=3: lambdify((x,a,b),u, modules = ['numpy','sympy']))
y = [f(t[i]) for i in range(len(t))]
plt.plot(t,y)
int.quad(f,-5,5)
# Gives error:
Traceback (most recent call last):
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-86-75a94a575357>", line 2, in <module>
y = [f(t[i]) for i in range(len(t))]
File "<ipython-input-86-75a94a575357>", line 2, in <listcomp>
y = [f(t[i]) for i in range(len(t))]
File "<ipython-input-86-75a94a575357>", line 1, in <lambda>
f = (lambda x,a=1,b=3: lambdify((x,a,b),u, modules = ['numpy','sympy']))
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\sympy\utilities\lambdify.py", line 476, in lambdify
c = compile(funcstr, filename, 'exec')
File "<lambdifygenerated-10>", line 1
def _lambdifygenerated(-5.0, 1, 3):
^
SyntaxError: invalid syntax
# But explicitly typing u works:
f = (lambda x, a=1, b=3: log(a + exp(b*x**2/2)))
y = [f(t[i]) for i in range(len(t))]
plt.plot(t,y)
int.quad(f,-5,5)
(126.10732269388095,1.0767576643095748−07)
Lambdify 创建一个函数:您调用它一次以获取该函数,然后多次调用 that 函数。你可以这样做:
In [11]: a = Symbol('a')
In [12]: b = Symbol('b')
In [13]: u = log(exp(-b*x**2/2) + 1/a)
In [14]: flambda = lambdify((x,a,b),u, modules = ['numpy','sympy'])
In [16]: flambda(1, 1, 3)
Out[16]: 0.20141327798275246
In [17]: f = lambda x,a=1,b=3: flambda(x, a, b)
In [18]: f(1)
Out[18]: 0.20141327798275246
我正在 Jupyter notebook 中进行一些符号计算,我想在继续其他符号计算之前绘制函数并计算数值积分。
我曾尝试对函数进行 lambdify,但只有重新输入函数定义才能得到绘图和积分。有没有办法直接用符号形式进行这些操作?如果没有,有没有办法用固定的输入参数 a 和 b 正确地 lambdify 函数?
from sympy import *
from sympy.utilities.lambdify import lambdify
import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as int
x,a,b = symbols('x a b')
u = log(exp(-b*x**2/2) + 1/a)
t = np.linspace(-5,5,100)
# Attempt to lambdify u:
f = (lambda x,a=1,b=3: lambdify((x,a,b),u, modules = ['numpy','sympy']))
y = [f(t[i]) for i in range(len(t))]
plt.plot(t,y)
int.quad(f,-5,5)
# Gives error:
Traceback (most recent call last):
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-86-75a94a575357>", line 2, in <module>
y = [f(t[i]) for i in range(len(t))]
File "<ipython-input-86-75a94a575357>", line 2, in <listcomp>
y = [f(t[i]) for i in range(len(t))]
File "<ipython-input-86-75a94a575357>", line 1, in <lambda>
f = (lambda x,a=1,b=3: lambdify((x,a,b),u, modules = ['numpy','sympy']))
File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\sympy\utilities\lambdify.py", line 476, in lambdify
c = compile(funcstr, filename, 'exec')
File "<lambdifygenerated-10>", line 1
def _lambdifygenerated(-5.0, 1, 3):
^
SyntaxError: invalid syntax
# But explicitly typing u works:
f = (lambda x, a=1, b=3: log(a + exp(b*x**2/2)))
y = [f(t[i]) for i in range(len(t))]
plt.plot(t,y)
int.quad(f,-5,5)
(126.10732269388095,1.0767576643095748−07)
Lambdify 创建一个函数:您调用它一次以获取该函数,然后多次调用 that 函数。你可以这样做:
In [11]: a = Symbol('a')
In [12]: b = Symbol('b')
In [13]: u = log(exp(-b*x**2/2) + 1/a)
In [14]: flambda = lambdify((x,a,b),u, modules = ['numpy','sympy'])
In [16]: flambda(1, 1, 3)
Out[16]: 0.20141327798275246
In [17]: f = lambda x,a=1,b=3: flambda(x, a, b)
In [18]: f(1)
Out[18]: 0.20141327798275246