三行搞定拉格朗日多项式
本帖最后由 mayaview 于 2013-12-16 14:41 编辑我看Mathematica版有人用Mathematica写拉格朗日多项式,我来个Sympy的版本:
def LagrangPoly(x,order,i,xi=None):
if xi==None:
xi=symbols('x:%d'%(order+1))
index = range(order+1)
index.pop(i)
return prod([(x-xi)/(xi-xi) for j in index])功能还行,动态生成变量x=symbols('x')LagrangPoly(x,5,0)
(x–x1)(x–x2)(x–x3)(x–x4)(x–x5)(x0–x1)(x0–x2)(x0−x3)(x0–x4)(x0–x5)
也可以做数值行的,比如下面可以做二次单元的形函数:x=symbols('x')simplify(LagrangPoly(x,2,0,[-1,0,1])),simplify(LagrangPoly(x,2,1,[-1,0,1])),simplify(LagrangPoly(x,2,2,[-1,0,1]))
(12x(x−1),–x2+1,12x(x+1))
{:{03}:}
页:
[1]