Cody

Problem 44492. Approximate the cosine function

Solution 1563369

Submitted on 18 Jun 2018 by tamir ben shitrit
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)

a = 1 k = 1 y = 1 a = 0 k = 2 y = 1

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

a = 1 k = 1 y = 1 a = -4.9348 k = 2 y = -3.9348 a = 4.0587 k = 3 y = 0.1239 a = -1.3353 k = 4 y = -1.2114 a = 0.2353 k = 5 y = -0.9760 a = -0.0258 k = 6 y = -1.0018 a = 0.0019 k = 7 y = -0.9999 a = -1.0464e-04 k = 8 y = -1.0000 a = 4.3031e-06 k = 9 y = -1.0000

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

a = 1 k = 1 y = 1 a = -1.2337 k = 2 y = -0.2337 a = 0.2537 k = 3 y = 0.0200 a = -0.0209 k = 4 y = -8.9452e-04 a = 9.1926e-04 k = 5 y = 2.4737e-05 a = -2.5202e-05 k = 6 y = -4.6477e-07

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

a = 1 k = 1 y = 1 a = -13.7078 k = 2 y = -12.7078 a = 31.3172 k = 3 y = 18.6094 a = -28.6193 k = 4 y = -10.0099 a = 14.0110 k = 5 y = 4.0011 a = -4.2680 k = 6 y = -0.2669 a = 0.8864 k = 7 y = 0.6195 a = -0.1335 k = 8 y = 0.4860 a = 0.0153 k = 9 y = 0.5013 a = -0.0014 k = 10 y = 0.4999 a = 9.8593e-05 k = 11 y = 0.5000

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!