matlab容差,绝对值容差 - MATLAB - MathWorks 中国

创建一个供交互测试的测试用例。

import matlab.unittest.TestCase

import matlab.unittest.constraints.IsEqualTo

import matlab.unittest.constraints.AbsoluteTolerance

import matlab.unittest.constraints.RelativeTolerance

testCase = TestCase.forInteractiveUse;

定义两个包含真空吸尘器的电磁属性的结构体。一个结构体 approxVacuumProps 包含真空吸尘器中光源的渗透率和速度的近似值。

approxVacuumProps.Permeability = 1.2566e-06; % Approximate

approxVacuumProps.Permitivity = 8.854187817*10^-12;

approxVacuumProps.LightSpeed = 2.9979e+08; % Approximate

baselineVacuumProps.Permeability = 4*pi*10^-7;

baselineVacuumProps.Permitivity = 8.854187817*10^-12;

baselineVacuumProps.LightSpeed = 1/sqrt(...

baselineVacuumProps.Permeability*baselineVacuumProps.Permitivity);

测试近似值和基准值之间的相对差是否处于 eps*1e11 内。

testCase.verifyThat(approxVacuumProps, IsEqualTo(baselineVacuumProps, ...

'Within', RelativeTolerance(eps*1e11)))

Verification failed.

---------------------

Framework Diagnostic:

---------------------

IsEqualTo failed.

--> Path to failure: .Permeability

--> NumericComparator failed.

--> The numeric values are not equal using "isequaln".

--> RelativeTolerance failed.

--> The error was not within relative tolerance.

--> Failure table:

Actual Expected Error RelativeError RelativeTolerance

__________ ____________________ _____________________ _____________________ ____________________

1.2566e-06 1.25663706143592e-06 -3.70614359173257e-11 -2.94925536216295e-05 2.22044604925031e-05

Actual double:

1.256600000000000e-06

Expected double:

1.256637061435917e-06

Actual struct:

Permeability: 1.256600000000000e-06

Permitivity: 8.854187816999999e-12

LightSpeed: 299790000

Expected struct:

Permeability: 1.256637061435917e-06

Permitivity: 8.854187816999999e-12

LightSpeed: 2.997924580105029e+08

测试失败,原因是渗透率的相对差不处于容差范围内。两个值之间的差较小,但数值接近于零,因此该差相对于其大小来说并不足够小,不满足该容差。

构造一个容差对象,以测试是否近似值和基准值之间的绝对差处于 1e-4 范围内。

testCase.verifyThat(approxVacuumProps, IsEqualTo(baselineVacuumProps, ...

'Within', AbsoluteTolerance(1e-4)))

Verification failed.

---------------------

Framework Diagnostic:

---------------------

IsEqualTo failed.

--> Path to failure: .LightSpeed

--> NumericComparator failed.

--> The numeric values are not equal using "isequaln".

--> AbsoluteTolerance failed.

--> The error was not within absolute tolerance.

--> Failure table:

Actual Expected Error RelativeError AbsoluteTolerance

_________ ________________ _________________ ____________________ _________________

299790000 299792458.010503 -2458.01050287485 -8.1990404935028e-06 0.0001

Actual double:

299790000

Expected double:

2.997924580105029e+08

Actual struct:

Permeability: 1.256600000000000e-06

Permitivity: 8.854187816999999e-12

LightSpeed: 299790000

Expected struct:

Permeability: 1.256637061435917e-06

Permitivity: 8.854187816999999e-12

LightSpeed: 2.997924580105029e+08

测试失败,原因是光速的绝对差不处于容差范围内。两个值之间的差相对于其大小来说较小,但要满足容差又太大。

构造容差对象的逻辑析取,以测试是否近似值和基准值之间的绝对差处于 1e-4 范围内或相对差处于 eps*1e11 范围内。测试使用此容差,这样接近于零的渗透率值满足绝对(下限)容差,较大的光速值满足相对误差。

testCase.verifyThat(approxVacuumProps, IsEqualTo(baselineVacuumProps, ...

'Within', RelativeTolerance(eps*1e11)| AbsoluteTolerance(1e-4)))

Verification passed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值