5
$\begingroup$

This question already has an answer here:

Suppose I want to expand a multivariable function to an order n.

f1[x_, y_] = Sin[x + y];
Series[f1[x + y], {x, 0, 3}, {y, 0, 3}] // Normal // Expand

$x - x^3/6 + y - (x^2 y)/2 - (x y^2)/2 + (x^3 y^2)/12 - y^3/6 + ( x^2 y^3)/12$

So I have to eliminate terms like $x^3 y^2$. One way could be using Exponent.

series[f_Symbol, x_, y_, n_] := Module[{expn},
       expn = Series[f[x, y], {x, 0, n}, {y, 0, n}] // Normal // Expand;
       Total@Select[Level[expn, 1], (Exponent[#, x] + Exponent[#, y]) <= n &] 
      ]

series[f1, x, y, 3]

$x - x^3/6 + y - (x^2 y)/2 - (x y^2)/2 - y^3/6$

  • Is there any better way to do it?
  • Can it be generalised for any number of variables?

By generalised I mean given a function, can the code detect the variables and make the expansion? I tried to use Variables but it does not work with trigonometric functions.

A related question might be Multivariable Taylor expansion does not work as expected

$\endgroup$

marked as duplicate by Michael E2, m_goldberg, mikado, MarcoB, happy fish May 2 '17 at 6:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ +1 It would be great if there were a built in function for this, or otherwise if it were possible to do Series[f1[x + y], {x, 0, 3}, {y, 0, 3 - x}]. $\endgroup$ – Feyre Jan 16 '17 at 12:21
  • $\begingroup$ Thanks @DanielLichtblau for the comment (and eyeopener :|). I can assure you there was no hidden intention behind the question. Sometimes I stuck with simple questions just because I search the answer in wrong place :p $\endgroup$ – Sumit Jan 25 '17 at 7:03
3
$\begingroup$

Another possibility is to rescale x,y,... by sx, sy,... and expand around s:

series[f_, var : {_Symbol ..}, n_Integer?Positive] := Module[{s},
  Expand@Normal[Series[ f /. Thread[ var -> (s var)], {s, 0, n}]] /. 
   s -> 1]
$\endgroup$
1
$\begingroup$
series[f_, var : {_Symbol ..}, n_Integer?Positive] :=
 Module[{expr =
    Series[f, Sequence @@ ({#, 0, n} & /@ var)] //
      Normal // Expand},
  Select[expr, Total[Exponent[#, var]] <= n &]]

f1[x_, y_] = Sin[x + y];

series[f1[x, y], {x, y}, 3]

(*  x - x^3/6 + y - (x^2 y)/2 - (x y^2)/2 - y^3/6  *)

f2[x_, y_, z_] = z*Sin[x + y];

series[f2[x, y, z], {x, y, z}, 4]

(*  x z - (x^3 z)/6 + y z - 1/2 x^2 y z - 1/2 x y^2 z - (y^3 z)/6  *)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.