Skip to main content
Post Closed as "Duplicate" by Michael E2, m_goldberg, mikado, MarcoB, vapor
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

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 expectedMultivariable Taylor expansion does not work as expected

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

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

Tweeted twitter.com/StackMma/status/821014388391944193
Source Link
Sumit
  • 16k
  • 2
  • 33
  • 74

Series expansion of a multivariable function to an order n for any number of variables

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