I did search everywhere, even on SO. and scholarly articles on google are far beyond my mental capabilities. This is as close as it gets, but doesn't answer my question. There is no answer for this as Far as I have looked for. Believe Me. ( But feel free to prove me wrong )
I have a homework problem for calculating Time complexity of sine function using Taylor's expansion of sine(x) function. I am not asking for Taylor series or Taylor series function program, but its time complexity. I know the next term in Taylor expansion is given as :
Term in x^n = (Term in x^n-2) * x * x / n / (n-1)
The function snippet is this:
double sine(double x) {
int n;
double sum, term;
n = 3;
sum = x;
term = x;
while(isgreater(fabs(term), 0.0000000001)) {
term = (term * x * x) / ( n * (n -1));
if(n % 4 == 3)
sum -= term;
else
sum += term;
n = n + 2;
}
return sum;
}
fabs() is the function for absolute value and 0.0000000001 is the precision required. If my understanding is correct, the code will stop when the value of the last calculated term is less than/equal to the precision float set.
My deduction so far is maybe the Time complexity will depend on x^2/n^2 ? or it is not deduce-able cause we don't know at which specific index/number the tern will be less then precision float ?
Math is not strong with me, But thankfully there are Masters like you out there :)
As an adjunct to LutzL fine analysis, sometimes a sanity check and visual of simple testing the code provides insight as to the time complexity.
Sample output
Graphing this is fairly linear once
x > 3
.Looking closer we see a step in time every power and using the trend looks very close to
t = pow(x,1/3.)
Using
clock()
is fairly crude, yet provides a visual to the analysis.With
x * x
, I would have expectedterm
to affectisgreater()
about everysqrt(x)
as that would oblige another loop iteration.Conclusion: time complexity for small values about
power(x,1.0/d)
(2.0 <= d <= 3.1) and linear for large values.Note there are a number of issues about the quality of OP's
sine()
that render its result weak for manyx
. For many valuesx
> 900,sine(x)
was an infinite loop.