Cody

# Problem 44492. Approximate the cosine function

Solution 1416263

Submitted on 14 Jan 2018 by nadav regev
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)

s = 1 k = 2 v = 1 s = 2 k = 4 v = 1 0 y = 1

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

s = 1 k = 2 v = 1 s = 2 k = 4 v = 1.0000 -4.9348 s = 3 k = 6 v = 1.0000 -4.9348 4.0587 s = 4 k = 8 v = 1.0000 -4.9348 4.0587 -1.3353 s = 5 k = 10 v = 1.0000 -4.9348 4.0587 -1.3353 0.2353 s = 6 k = 12 v = 1.0000 -4.9348 4.0587 -1.3353 0.2353 -0.0258 s = 7 k = 14 v = 1.0000 -4.9348 4.0587 -1.3353 0.2353 -0.0258 0.0019 s = 8 k = 16 v = 1.0000 -4.9348 4.0587 -1.3353 0.2353 -0.0258 0.0019 -0.0001 s = 9 k = 18 v = 1.0000 -4.9348 4.0587 -1.3353 0.2353 -0.0258 0.0019 -0.0001 0.0000 y = -1.0000

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

s = 1 k = 2 v = 1 s = 2 k = 4 v = 1.0000 -1.2337 s = 3 k = 6 v = 1.0000 -1.2337 0.2537 s = 4 k = 8 v = 1.0000 -1.2337 0.2537 -0.0209 s = 5 k = 10 v = 1.0000 -1.2337 0.2537 -0.0209 0.0009 s = 6 k = 12 v = 1.0000 -1.2337 0.2537 -0.0209 0.0009 -0.0000 y = -4.6477e-07

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

s = 1 k = 2 v = 1 s = 2 k = 4 v = 1.0000 -13.7078 s = 3 k = 6 v = 1.0000 -13.7078 31.3172 s = 4 k = 8 v = 1.0000 -13.7078 31.3172 -28.6193 s = 5 k = 10 v = 1.0000 -13.7078 31.3172 -28.6193 14.0110 s = 6 k = 12 v = 1.0000 -13.7078 31.3172 -28.6193 14.0110 -4.2680 s = 7 k = 14 v = 1.0000 -13.7078 31.3172 -28.6193 14.0110 -4.2680 0.8864 s = 8 k = 16 v = 1.0000 -13.7078 31.3172 -28.6193 14.0110 -4.2680 0.8864 -0.1335 s = 9 k = 18 v = 1.0000 -13.7078 31.3172 -28.6193 14.0110 -4.2680 0.8864 -0.1335 0.0153 s = 10 k = 20 v = 1.0000 -13.7078 31.3172 -28.6193 14.0110 -4.2680 0.8864 -0.1335 0.0153 -0.0014 s = 11 k = 22 v = 1.0000 -13.7078 31.3172 -28.6193 14.0110 -4.2680 0.8864 -0.1335 0.0153 -0.0014 0.0001 y = 0.5000