测试脚本怎么写 matlab,使用局部函数编写基于脚本的测试

本文介绍了如何在MATLAB中实现approxSinCos函数,用泰勒级数逼近角的正弦和余弦,并创建了详细的测试脚本,包括对0弧度、2π、π/4和特定角度与MATLAB内建函数的比较,确保精度。通过assertWithAbsTol和assertWithRelTol辅助函数验证函数的准确性。
摘要由CSDN通过智能技术生成

创建要测试的 approxSinCos 函数

在您的当前 MATLAB 文件夹下的 approxSinCos.m 文件中创建以下函数。此函数接受以弧度为单位的角,并使用泰勒级数来近似计算角的正弦和余弦。

function [sinA,cosA] = approxSinCos(x)

% For a given angle in radians, approximate the sine and cosine of the angle

% using Taylor series.

sinA = x;

cosA = 1;

altSign = -1;

for n = 3:2:26

sinA = sinA + altSign*(x^n)/factorial(n);

cosA = cosA + altSign*(x^(n-1))/factorial(n-1);

altSign = -altSign;

end

创建测试脚本

在您的当前 MATLAB 文件夹中,创建一个新脚本 approxSinCosTest.m。

注意:在脚本中包括函数需要安装 MATLAB® R2016b 或更高版本。

%% Test 0rad

% Test expected values of 0

[sinApprox,cosApprox] = approxSinCos(0);

assertWithAbsTol(sinApprox,0)

assertWithRelTol(cosApprox,1)

%% Test 2pi

% Test expected values of 2pi

[sinApprox,cosApprox] = approxSinCos(2*pi);

assertWithAbsTol(sinApprox,0)

assertWithRelTol(cosApprox,1)

%% Test pi over 4 equality

% Test sine and cosine of pi/4 are equal

[sinApprox,cosApprox] = approxSinCos(pi/4);

assertWithRelTol(sinApprox,cosApprox,'sine and cosine should be equal')

%% Test matches MATLAB fcn

% Test values of 2pi/3 match MATLAB output for the sin and cos functions

x = 2*pi/3;

[sinApprox,cosApprox] = approxSinCos(x);

assertWithRelTol(sinApprox,sin(x),'sin does not match')

assertWithRelTol(cosApprox,cos(x),'cos does not match')

function assertWithAbsTol(actVal,expVal,varargin)

% Helper function to assert equality within an absolute tolerance.

% Takes two values and an optional message and compares

% them within an absolute tolerance of 1e-6.

tol = 1e-6;

tf = abs(actVal-expVal) <= tol;

assert(tf, varargin{:});

end

function assertWithRelTol(actVal,expVal,varargin)

% Helper function to assert equality within a relative tolerance.

% Takes two values and an optional message and compares

% them within a relative tolerance of 0.1%.

relTol = 0.001;

tf = abs(expVal - actVal) <= relTol.*abs(expVal);

assert(tf, varargin{:});

end

每个单元测试都使用 assert 来检查 approxSinCos 函数的不同输出。通常,当您比较浮点值时,需要指定比较的容差。局部函数 assertWithAbsTol 和 assertWithRelTol 为辅助函数,可计算实际值和预期值在指定的绝对容差或相对容差范围内是否相等。

Test 0rad 用于测试 0 弧度的角的计算值和预期值是否位于绝对容差 1e-6 或相对容差 0.1% 范围内。通常,您应使用绝对容差来比较接近 0 的值。

Test 2pi 用于测试 c17796e064615a6b83dd878422e2f073.png 弧度的角计算值和预期值在 1e-6 的绝对容差或 0.1% 的相对容差范围内是否相等。

Test pi over 4 equality 用于测试 70e7c174c39d80961fdb354adf955f72.png 的正弦和余弦在 0.1% 的相对容差范围内是否相等。

Test matches MATLAB fcn 用于测试 c6ea1be9c019d89ddf77af8163d882ca.png 的正弦和余弦计算值在 0.1% 的相对容差范围内是否等于 sin 和 cos 函数中的值。

运行测试

执行 runtests 函数以运行 approxSinCosTest.m 中的四个测试。runtests 函数会分别执行每个测试。如遇测试失败,MATLAB 仍将运行剩余的测试。如果您将 approxSinCosTest 作为脚本执行,而不是通过使用 runtests 来执行,MATLAB 会在遇到失败断言时停止执行整个脚本。此外,当您使用 runtests 函数运行测试时,MATLAB 可提供包含有用信息的测试诊断。

results = runtests('approxSinCosTest');

Running approxSinCosTest

....

Done approxSinCosTest

__________

成功通过所有测试。

创建测试结果表格。

rt = table(results)

rt =

4x6 table

Name Passed Failed Incomplete Duration Details

_________________________________________ ______ ______ __________ ________ ____________

{'approxSinCosTest/Test0rad' } true false false 0.45581 {1x1 struct}

{'approxSinCosTest/Test2pi' } true false false 0.028881 {1x1 struct}

{'approxSinCosTest/TestPiOver4Equality' } true false false 0.026974 {1x1 struct}

{'approxSinCosTest/TestMatchesMATLABFcn'} true false false 0.053493 {1x1 struct}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值