1

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?

shopper
  • 309
  • 1
  • 7

2 Answers2

8

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 *)
Michael Seifert
  • 15,208
  • 31
  • 68
4

An alternative to taking a limit is to just replace x with a Series version, and chop off any excess by multiplying by another SeriesData object:

leadingTerms[expr_, {x_, x0_, n_Integer?Positive}]:=
    (expr /. x -> Series[x, {x, x0, n}]) SeriesData[x, x0, {1}, 0, n, 1]

A couple examples:

leadingTerms[Cos[x]/Sin[2x]^3, {x, 0, 3}] //TeXForm

$\frac{1}{8 x^3}+\frac{3}{16 x}+O\left(x^0\right)$

leadingTerms[Gamma[x], {x, Infinity, 3}] //TeXForm

$\left(\sqrt{2 \pi } \sqrt{\frac{1}{x}}+\frac{1}{6} \sqrt{\frac{\pi }{2}} \left(\frac{1}{x}\right)^{3/2}+\frac{1}{144} \sqrt{\frac{\pi }{2}} \left(\frac{1}{x}\right)^{5/2}+O\left(\left(\frac{1}{x}\right)^{7/2}\right)\right) \exp \left(\left(-\log \left(\frac{1}{x}\right)-1\right) x+O\left(\left(\frac{1}{x}\right)^4\right)\right)$

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • Hi Carl, I had some problems with your solution, could you have a look at this please: https://mathematica.stackexchange.com/questions/167501/problem-with-seriesdata – SonerAlbayrak Mar 09 '18 at 08:06