MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Test: Small angle approximation

You can include equations and images in your live script to help document the test. Create
the following test for the small angle approximation. Typically, when you compare
floating-point values, you specify a tolerance for the comparison.

The rightTri function should return values consistent with the small angle
approximation, such that sin(θ)≈θ.

angles = rightTri([1 1500]);
smallAngleInRadians = (pi/180)*angles(1); % convert to radians
approx = sin(smallAngleInRadians);
assert(abs(approx-smallAngleInRadians) <= 1e-10, 'Problem with small angle approximation')

Test: Sum of Angles


k

ak= 180∘

You can have multiple assert statements in the same test. However, if the first assertion
fails, the MATLAB does not evaluate the remaining statements.

The sum of all angles of the resulting right triangle should always be 180 degrees.

angles = rightTri([7 9]);
assert(sum(angles) == 180)

angles = rightTri([4 4]);
assert(sum(angles) == 180)

angles = rightTri([2 2*sqrt(3)]);
assert(sum(angles) == 180)

Test: 30-60-90 triangle

33 Unit Testing

Free download pdf