Mathworks Interview Question for Applications Developers

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
of 5 vote

Using taylor series approximation ( this is the exact method which is used in math libraries of programming languages )

- Cerberuz December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 votes

To expand this answer, sin(x) is approximated as x - x^3/3! + x^5/5! - x^7/7! + ... to some number of terms that allow sufficient precision. cos(x) is approximated as 1 - x^2/2! + x^4/4! - x^6/6! +... . These formulas come from calculus.

To minimize the magnitude of the error incurred when approximating, angles are first reduced to the range [-pi, pi] and only then approximated using the above formulas. This can significantly reduce error because of the high powers of x in some of the terms. The formula doesn't start to converge very well until you have a number of terms that is somewhat proportional (give or take) to the absolute value of x. If you have -pi <= x <= pi, you're guaranteed that the formula does a pretty good job of converging after about just 5-10 terms.

- eugene.yarovoi December 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
of 1 vote

we use series expansion of sine and cosine,as
sin(x) = x - x^3/3! + x^5/5! - x^7/7! + .....
cos(x) = 1-x^2/2! + x^4/4! - x^6/6! + .....
we calculate both power of x and factorial by power methods,
for example in cos series we first calculate x^2, then each time we just multiply x^2 with current maximum power to get next element.
similarly for sin series also we use the same fact.
Note that here x is in radian.
complexity : approx O(1), or it depends upon accuracy required.

- sonesh January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
of 1 vote

A very good answer to this question from stackoverflow
First, you have to do some sort of range reduction. Trig functions are periodic, so you need to reduce arguments down to a standard interval. For starters, you could reduce angles to be between 0 and 360 degrees. But by using a few identities, you realize you could get by with less. If you calculate sines and cosines for angles between 0 and 45 degrees, you can bootstrap your way to calculating all trig functions for all angles.

Once you've reduced your argument, most chips use a CORDIC algorithm to compute the sines and cosines. You may hear people say that computers use Taylor series. That sounds reasonable, but it's not true. The CORDIC algorithms are much better suited to efficient hardware implementation. (Software libraries may use Taylor series, say on hardware that doesn't support trig functions.) There may be some additional processing, using the CORDIC algorithm to get fairly good answers but then doing something else to improve accuracy.

There are some refinements to the above. For example, for very small angles theta (in radians), sin(theta) = theta to all the precision you have, so it's more efficient to simply return theta than to use some other algorithm. So in practice there is a lot of special case logic to squeeze out all the performance and accuracy possible. Chips with smaller markets may not go to as much optimization effort.

- Siddarth April 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

sine cosine optimization

- Anonymous January 29, 2018 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More