Python 如何通过以下测试

Python 如何通过以下测试,python,Python,我试图计算以下函数的一阶和二阶导数,pytest检查我的计算误差 我试图改变h的值以通过pytest,但我无法做到这一点 def error_5_1_11(x): def f(x): return x ** 3 - 0.3 * x ** 2 - 8.56 * x + 8.448 def f_1(f, x, h=10E-10): return (f(x + h) - f(x - h)) / (2 * h) def f_2(f, x, h=10E-4):

我试图计算以下函数的一阶和二阶导数,pytest检查我的计算误差

我试图改变h的值以通过pytest,但我无法做到这一点

def error_5_1_11(x):

    def f(x): return x ** 3 - 0.3 * x ** 2 - 8.56 * x + 8.448

    def f_1(f, x, h=10E-10):
        return (f(x + h) - f(x - h)) / (2 * h)

    def f_2(f, x, h=10E-4):
        return (f(x + h) - 2 * f(x) + f(x - h)) / h ** 2

    return f_1(f, x)-(-8.56), f_2(f, x)-(-0.6)


import pytest
from error import *
from numpy import allclose, array
from numpy import allclose

def test_error():
    (a, b) = error_5_1_11(0)
    assert(abs(a) < 10E-13 and abs(b) < 10E-14)
def错误_5_1_11(x):
def f(x):返回x**3-0.3*x**2-8.56*x+8.448
def f_1(f,x,h=10E-10):
返回(f(x+h)-f(x-h))/(2*h)
def f_2(f,x,h=10E-4):
返回(f(x+h)-2*f(x)+f(x-h))/h**2
返回f_1(f,x)-(8.56),f_2(f,x)-(0.6)
导入pytest
从错误导入*
从numpy导入allclose,数组
从numpy导入allclose
def测试_错误():
(a,b)=错误5_1_11(0)
断言(abs(a)<10E-13和abs(b)<10E-14)

为了通过下面的pytest,我需要做哪些修改,谢谢。

一个简单的修复方法是在主函数上添加关键字参数

def错误(x,h1=10E-10,h2=10E-4): def f(x): 返回x**3-0.3*x**2-8.56*x+8.448 定义f_1(f,x,h=h1): 返回(f(x+h)-f(x-h))/(2*h) def f_2(f,x,h=h2): 返回(f(x+h)-2*f(x)+f(x-h))/h**2 返回f_1(f,x)-(8.56),f_2(f,x)-(0.6) 导入pytest 从错误导入* 从numpy导入allclose,数组 def测试_错误(): (a,b)=错误_5_1_11(0,h1=10E-13,h2=10E-14) 断言所有(abs(a)<10E-13和abs(b)<10E-14)
一个简单的修复方法是在主函数上添加关键字和参数

def错误(x,h1=10E-10,h2=10E-4): def f(x): 返回x**3-0.3*x**2-8.56*x+8.448 定义f_1(f,x,h=h1): 返回(f(x+h)-f(x-h))/(2*h) def f_2(f,x,h=h2): 返回(f(x+h)-2*f(x)+f(x-h))/h**2 返回f_1(f,x)-(8.56),f_2(f,x)-(0.6) 导入pytest 从错误导入* 从numpy导入allclose,数组 def测试_错误(): (a,b)=错误_5_1_11(0,h1=10E-13,h2=10E-14) 断言所有(abs(a)<10E-13和abs(b)<10E-14)
对不起。它仍然没有通过pytest。@ZimingWang我已经用
all
更新了代码,告诉我这是不是另一个问题,我不应该对测试文件做任何更改。对不起,我没提,对不起。它仍然没有通过pytest。@ZimingWang我已经用
all
更新了代码,告诉我这是不是另一个问题,我不应该对测试文件做任何更改。很抱歉,我没有提到这件事。