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.
總結
以上是生活随笔為你收集整理的matlab容差,绝对值容差 - MATLAB- MathWorks 中国的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql题库随机抽取试题_Python
- 下一篇: UseCase