## Mathworks Interview Question

Development Support Engineers```
//Taylor Series Approximation
double theta = 45; //45 degrees
double PIby180 = 3.14159265358979323846264338327950/180;
double x = theta*PIby180;
double sinx = x*(1 - (x*x)*(1 - (x*x)*(1 - (x*x)/(6*7) ) / (4*5) ) /(2*3) );
x = (theta+90)*PIby180;
double cosx = x*(1 - (x*x)*(1 - (x*x)*(1 - (x*x)/(6*7) ) / (4*5) ) /(2*3) );
```

const double PI = 3.14159265358979323846264338327950;

const double epsilon = 0.000000000000000000000000001;

//sinx = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! - ...

//this is not for old-fashioned handhold calculator

double sine(double x){

x = x - 2 * PI * (int)((x + PI)/(2*PI)); //put x in the interval (-PI, PI]

double result = x;

flip = 1;

xsquare = x * x;

xexp = x;

fac = 1;

curOrder = 1;

double oldvalue = 0;

while (abs(result - oldvalue) > epsilon){

oldvalue = result;

flip = flip * -1;

fac = fac * (curOrder + 1) * (curOrder + 2);

curOrder += 2;

xexp *= xexp * xsqure;

result = result + flip * xexp / fac;

}

return result;

}

You can use Taylor series:

- thomas November 21, 2010sin(x) = x / 1! - x^3 / 3! + x^5 / 5! - ...