(某些功能)未使用SymPy Lambdify定义 [英] (Some function) is not defined with SymPy Lambdify

查看:89
本文介绍了(某些功能)未使用SymPy Lambdify定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在编写一个评估泰勒级数的脚本.但是,我希望它能够评估所有类型的功能.因此,例如,我尝试使用功能acot(x).

So I'm writing a script that evaluates Taylor Series. However, I want it to evaluate for all types of functions. So I tried, for example, using the function acot(x).

x = sy.Symbol('x')
f = acot(x)
...
func = taylor(f,0,3)
taylor_lambda = sy.lambdify(x, func, 'numpy')

以上内容无例外运行(例如,除非我使用acsch,但它不会运行).

The above runs without an exception (except if I use acsch, for example, and it does not run).

但是当它到达这一行时:

But then when it reaches this line:

plt.plot(x1,taylor_lambda(x1),label='taylor approximation')

我得到:

NameError: name 'acot' is not defined

我尝试在lambdify调用中将numpy替换为sympy,但这似乎具有象征意义.某些情况(更罕见的功能)正在发生这种情况,而其他情况则没有.谢谢!

I tried to replace numpy with sympy in the lambdify call but this seems to evaluate symbolically. This is happening with some (more rare functions) but not for others. Thank you!

我的导入如下:

import sympy as sy
import numpy as np
from sympy.functions import *
from sympy import pi, E,acot
import matplotlib.pyplot as plt
import math

推荐答案

我之前确实遇到过类似的问题,并且设法解决了这些问题.

I did have similar problems before and have managed to solve them.Your line

plt.plot(x1,taylor_lambda(x1),label='taylor approximation')

看起来不错.我正在给我一个较旧的代码,可以正常工作,您可以进行比较.

looks OK.I am giving one my older code that works fine,you can just compare.

from sympy.abc import x
from sympy import sin, series
from sympy.utilities.lambdify import lambdify

import numpy as np
import matplotlib.pyplot as plt


func = sin(x)/x
taylor = series(func, n=6).removeO()

evalfunc = lambdify(x, func, modules=['numpy'])
evaltaylor = lambdify(x, taylor, modules=['numpy'])

t = np.linspace(-7.5, 7.5 , 100)
plt.plot(t, evalfunc(t), 'b', label='sin(x)/x')
plt.plot(t, evaltaylor(t), 'r', label='Taylor')
plt.legend(loc='best')
plt.show()

这篇关于(某些功能)未使用SymPy Lambdify定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆