I'm doing calculations with Series where I don't know the power of the leading order term. I would like to keep a specified number of terms, but since I don't know the leading order this is proving difficult. For instance, suppose I want only 3 terms in a series of some complicated function func[x]. I can call
Series[func[x],{x,0,3}]
This may return something like
(* 1/x^3 + 1/x^2 + 1/(2 x) + 1/6 + x/24 + x^2/120 + x^3/720 + O(x^4) *)
But, since I didn't know it was going to start at x^-3, I have computed more terms than I needed. (Or worse, it might have started at x^2 and I would not have computed enough terms.)
So, the question is, can I write a function that will expand an expression and only keep N terms (without over-expanding and truncating, which is computationally wasteful)? It seems like this should be a simple task, but I'm finding it remarkably difficult. Any ideas?
Answer
The leading-order power in your series should be the limit as $x \to 0$ of $x f'(x)/f(x)$. So you could do something like
leadcoeff = Limit[x func'[x]/func[x], x->0]
Series[func[x], {x, 0, leadcoeff+2}]
I'm not sure if the extra step of taking the limit kills off any efficiencies you might gain from only calculating the extra terms, though.
Examples:
f[x_] = Cos[x]/Sin[2 x]^3
leadcoeff = Limit[x f'[x]/f[x], x -> 0]
Series[f[x], {x, 0, leadcoeff + 2}]
(* 1/(8 x^3) + 3/(16 x) + O[x]^1 *)
f[x_] = Sin[2 x]^3/Cos[x]
leadcoeff = Limit[x f'[x]/f[x], x -> 0]
Series[f[x], {x, 0, leadcoeff + 2}]
(* 8 x^3 - 12 x^5 + O[x]^7 *)
Comments
Post a Comment