2012-07-13 5 views
6

Scipy의 파생 기능에 대한 질문이 있습니다. 나는 어제 밤 그것을 사용하고 이상한 대답을 얻었다. 나는 오늘 아침에 몇 가지 간단한 기능으로 다시 시도하고 몇 가지 정답과 잘못된 것을 얻었다. Scipy Derivative

In [1]: def poly1(x): 
...:  return x**2 

In [3]: derivative(poly1, 0) 
Out[3]: 0.0 

In [4]: def poly2(x): 
...: return (x-3)**2 

In [6]: derivative(poly2, 3) 
Out[6]: 0.0 

In [8]: def sin1(x): 
...:  return sin(x) 

In [14]: derivative(sin1, pi/2) 
Out[14]: 5.5511151231257827e-17 

In [15]: def poly3(x): 
....:  return 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2 

In [19]: derivative(poly3, -2) 
Out[19]: -39.0 

In [20]: derivative(poly3, 2) 
Out[20]: 121.0 

In [22]: derivative(poly3, 0) 
Out[22]: 17.0 

내가 손으로 poly3의 값을 확인하고 -2 = 17 = 95 (2) = 15 0 그래서이 기능을 잘못 사용하고, 또는 기능에 문제가 : 여기 내 테스트했다 . 감사합니다 사용

: derivative 설명서에 적혀 파이썬 2.7.3, IPython 0.12.1, NumPy와 1.6.1, Scipy 0.9.0, 리눅스 민트 13

답변

15

을 :

derivative(func, x0, dx=1.0, n=1, args=(), order=3) 
    Find the n-th derivative of a function at point x0. 

    Given a function, use a central difference formula with spacing `dx` to 
    compute the n-th derivative at `x0`. 

당신은 didn를 dx을 지정하지 않으므로 여기에서 기본값 인 1을 사용합니다. 예를 들어 :

In [7]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=1, order=5) 
Out[7]: -17.0 

수치 파생 상품을 촬영하면 항상 조금 귀찮은 :

In [1]: from scipy.misc import derivative 

In [2]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=1) 
Out[2]: -39.0 

In [3]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=0.5) 
Out[3]: -22.5 

In [4]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=0.1) 
Out[4]: -17.220000000000084 

In [5]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=0.01) 
Out[5]: -17.0022000000003 

In [6]: derivative(lambda x: 3*x**4 + 2*x**3 - 10*x**2 + 15*x - 2, -2, dx=1e-5) 
Out[6]: -17.000000001843318 

은 양자 택일로, 당신은 순서를 증가시킬 수있다.

+0

아, 감사합니다. 설명서를 읽고 너무 잘 이해하지 못했습니다. 다른 옵션의 작동 방식을 보여주기 위해 이와 같은 예제를 제공하면 좋을 것입니다. 다시 한 번 감사드립니다. – user1523697