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
Series[f1[x + y], {x, 0, 3}, {y, 0, 3 - x}]
. $\endgroup$