Cody

Problem 44492. Approximate the cosine function

Solution 1563657

Submitted on 18 Jun 2018 by OZ FEDERMAN
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 = 0 t = 1 j = 0 t = 1 y = 1 j = 1 t = 0 y = 1 j = 2

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

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

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

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

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

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

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!