www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/approxSinCosTest.m
%% 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