簡體   English   中英

python 的數值微分

[英]Numerical differentiation with python

我有這樣一個任務:

You need to write a function with the following signature: def derForward(f, I, h) This function should get as input function and segment as Python list of two elements --- ends of the segment. 在 function 中,您被要求將段划分為長度為 ℎ 的小段,從而得到一個網格。 您的 function 應該返回 dy --- 每個點 x 的前向差異(除了邊界,因為公式要求下一個值)。 您應該返回相同長度的 x 和 dy arrays。

我的答案是這樣的:

xx = [] #list of x values
frw = [] #list of forw. differencies of x

def derForward(f, I, h):
    x = np.arange(I[0], I[1], h)
    f = np.vectorize(f)
    for x_ in x[:-1]:
        dxdy = (f(x_+h)-f(x_))/ h
        xx.append(x_)
        frw.append(dxdy)
    x = np.array(xx)
    dy = np.array(frw)
    return x, dy

此 function 必須通過 3 次錯誤和准確度測試。 但現在它在測試 2 和 3 中失敗了。我不明白為什么我只能通過 1 次測試。 檢查器如下所示:

import numpy as np
from math import *


from time import time

def findif_check(derForward):
    count=0
    I=[0.001, 2*np.pi]
    f=lambda x: sin(x)
    h=0.01
    st=time()
    x, dy=derForward(f, I, h)
    dur=time()-st

    if x.shape[0]!=dy.shape[0]:
        print('FAILED: x and dy shape mismatch!')
    else:
        df=lambda x: cos(x)    
        err=np.max(np.abs(dy-np.vectorize(df)(x)))
        print('Test 1 |::|  err=', np.max(np.abs(dy-np.cos(x))), '  |::|  time: ', dur, 's')
        if err<2*0.0075:
            count+=1
            print('Test 1 |::|  accuracy OK') 
        else:
            print('Test 1 |::|  accuracy FAIL') 
        f=lambda x: x**x
        I=[0.001, 1]

        st=time()
        x, dy=derForward(f, I, h)
        dur=time()-st
        
        df=lambda x: x**x*(log(x)+1)
        err=np.max(np.abs(dy-np.vectorize(df)(x)))
        print('Test 2 |::|  err=', err, '  |::|  time: ', dur, 's')
        if err<2:
            count+=1
            print('Test 2 |::|  accuracy OK')
        else:
            print('Test 2 |::|  accuracy FAIL')

        f=lambda x: e**(-x**2)
        I=[0.001, 1]

        st=time()
        x, dy=derForward(f, I, h)
        dur=time()-st
        
        df=lambda x: -2*x*e**(-x**2)
        err=np.max(np.abs(dy-np.vectorize(df)(x)))
        print('Test 3 |::|  err=', err, '  |::|  time: ', dur, 's')
        if err<2*0.01:
            count+=1
            print('Test 3 |::|  accuracy OK')
        else:
            print('Test 3 |::|  accuracy FAIL')
    print('Passed: ', count, '/ 3')
   

因為在運行測試 2 和 3 時,變量xxfrw包含測試 1 的數據。

所以將xxfrw的定義移到def derForward(f, I, h):中,如下:

def derForward(f, I, h):
    xx = [] #list of x values
    frw = [] #list of forw. differencies of x

    x = np.arange(I[0], I[1], h)
    f = np.vectorize(f)
    // ... ... ... ...

然后所有3個測試都通過了:

Test 1 |::|  err= 0.004999976659897233   |::|  time:  0.04198646545410156 s
Test 1 |::|  accuracy OK
Test 2 |::|  err= 1.715675908387758   |::|  time:  0.007221221923828125 s
Test 2 |::|  accuracy OK
Test 3 |::|  err= 0.009999270029526776   |::|  time:  0.00669407844543457 s
Test 3 |::|  accuracy OK
Passed:  3 / 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM