Cody

Problem 44492. Approximate the cosine function

Solution 2703027

Submitted on 16 Jul 2020 by Jerry Wright
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
filetext = fileread('myCos.m'); assert(isempty(strfind(filetext, 'regexp')),'regexp hacks are forbidden')

2   Pass
filetext = fileread('myCos.m'); trigUsed = any(strfind(filetext, 'cos')) || any(strfind(filetext, 'sin')) ||... any(strfind(filetext, 'exp')); assert(~trigUsed, 'Cannot use MATLAB trigonometric functions')

3   Pass
x = 0; assert(abs(myCos(x)-cos(x)) < 0.0001)

y = 1 sgn = -1 y = 1 sgn = 1 y = 1 sgn = -1 y = 1 sgn = 1 y = 1 sgn = -1 y = 1 sgn = 1 y = 1 sgn = -1 y = 1 sgn = 1 y = 1 sgn = -1 y = 1 sgn = 1 y = 1 sgn = -1 y = 1 sgn = 1 y = 1 sgn = -1

4   Pass
x = pi; assert(abs(myCos(x)-cos(x)) < 0.0001)

y = 1 sgn = -1 y = -3.9348 sgn = 1 y = 0.1239 sgn = -1 y = -1.2114 sgn = 1 y = -0.9760 sgn = -1 y = -1.0018 sgn = 1 y = -0.9999 sgn = -1 y = -1.0000 sgn = 1 y = -1.0000 sgn = -1 y = -1.0000 sgn = 1 y = -1.0000 sgn = -1 y = -1.0000 sgn = 1 y = -1.0000 sgn = -1

5   Pass
x = pi/2; assert(abs(myCos(x)-cos(x)) < 0.0001)

y = 1 sgn = -1 y = -0.2337 sgn = 1 y = 0.0200 sgn = -1 y = -8.9452e-04 sgn = 1 y = 2.4737e-05 sgn = -1 y = -4.6477e-07 sgn = 1 y = 6.3215e-09 sgn = -1 y = -6.5134e-11 sgn = 1 y = 5.2606e-13 sgn = -1 y = -3.3769e-15 sgn = 1 y = 6.0818e-17 sgn = -1 y = 4.2458e-17 sgn = 1 y = 4.2540e-17 sgn = -1

6   Pass
x = 5*pi/3; assert(abs(myCos(x)-cos(x)) < 0.0001)

y = 1 sgn = -1 y = -12.7078 sgn = 1 y = 18.6094 sgn = -1 y = -10.0099 sgn = 1 y = 4.0011 sgn = -1 y = -0.2669 sgn = 1 y = 0.6195 sgn = -1 y = 0.4860 sgn = 1 y = 0.5013 sgn = -1 y = 0.4999 sgn = 1 y = 0.5000 sgn = -1 y = 0.5000 sgn = 1 y = 0.5000 sgn = -1

Suggested Problems

More from this Author25

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!