# Series expansion of a multivariable function to an order n for any number of variables [duplicate]

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

## marked as duplicate by Michael E2, m_goldberg, mikado, MarcoB, happy fishMay 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.

• +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}]. – Feyre Jan 16 '17 at 12:21
• 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 – Sumit Jan 25 '17 at 7:03

## 2 Answers

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]

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  *)