繁体   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