Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为所有订单优化我的N阶导数程序?_Python_Python 2.7_Scientific Computing_Numerical Analysis_Differentiation - Fatal编程技术网

Python 如何为所有订单优化我的N阶导数程序?

Python 如何为所有订单优化我的N阶导数程序?,python,python-2.7,scientific-computing,numerical-analysis,differentiation,Python,Python 2.7,Scientific Computing,Numerical Analysis,Differentiation,我正试图写一个程序来打印任何函数的n阶导数。我使用的方法是通过反转Vandermonde矩阵来计算函数的Lagrange多项式的系数,然后微分多项式(这比微分原始函数更容易,因为我只处理系数并使用x^n的导数公式) 但是,对于不同的函数和不同阶导数,我必须改变h和插值多项式的阶数;否则它在数值上就会变得不稳定。通常,对于简单函数(如sin、cos、tan、exp等),步长为0.1到11阶导数,步长为1到20阶导数,加上a n1=2*(导数阶数)+1,可以给出可接受的结果。如何优化所有函数和所有阶

我正试图写一个程序来打印任何函数的n阶导数。我使用的方法是通过反转Vandermonde矩阵来计算函数的Lagrange多项式的系数,然后微分多项式(这比微分原始函数更容易,因为我只处理系数并使用x^n的导数公式)

但是,对于不同的函数和不同阶导数,我必须改变h和插值多项式的阶数;否则它在数值上就会变得不稳定。通常,对于简单函数(如sin、cos、tan、exp等),步长为0.1到11阶导数,步长为1到20阶导数,加上a n1=2*(导数阶数)+1,可以给出可接受的结果。如何优化所有函数和所有阶导数的h和n1值

(虽然大多数人不喜欢eval,但这里使用eval,因此可以将输入函数作为输入)

f=原始输入(“输入函数:”)
n=输入(“输入导数的顺序:”)
a=输入(“输入要找到导数的值:”)
n1=输入(“输入你想要求导数的点数:”)
h=0.1#步长
N=(n1-1)//2
X=[]#X值
x=a-N*h

在循环(x,2)时,我会看一看数字配方,第。第229页第5.7节()。有一篇关于数值计算导数的好文章。这是一种不同于你的方法。我会看看数字配方,第二节。第229页第5.7节()。有一篇关于数值计算导数的好文章。但这和你的方法不同。
f=raw_input("Enter the function:")
n=input("enter the order of derivative:")
a=input("enter the value at which the derivative is to be found:")
n1=input("enter the number of points by which u want to find the derivative:")
h=0.1#stepsize
N=(n1-1)//2

X=[]#x values
x=a-N*h
while round(x,2)<=a+N*h:
    X.append(x)
    x+=h

Y=[]#y values
for elem in X:
    x=elem
    Y.append(eval(f))

def v(x):#returns vandermonde matrix
    X1=[[0.0 for i in range(len(x))]for j in range(len(x))]
    for i in range(0,len(x)):
        for j in range(0,len(x)):
            X1[i][j]=(x[i]**j)
    return X1

from scipy.linalg import solve

def deriv(c,n):#returns derivative of a polynomial
    for i in range(n):
        b=[]
        for j in range(0,len(c)):
            b.append(c[j]*j)
        del b[0]
        if b==[]:
            break
        else:
            c=b
    if b!=[]:
        return b
    else:
        return [0]

def eve(n):#evaluates the polynomial at that point
    sum=0
    for i in range(0,len(deriv(solve(v(X),Y),n))):#solve(v(X),Y) gives the coefficients of the lagrange polynomial
        sum+=(a**i)*deriv(solve(v(X),Y),n)[i]
    return sum

print eve(n)