# Taylor Expansion in Mathematica (Multivariate)

1. Jul 27, 2012

Hi!!

I'm trying to linearize a function f which is dependent of 4 variables, each one dependent of time.

f[var1_,var2_,var3_,var4_]:= .... expression

I use Series[f, {var1,var10 ,1},{var2,var20 ,1},...] syntax
as I read on the documentation center.

The problem is that the program only read the 2 first variables as function variables (green colored) and the last 2 appeared in blue.

It causes me confusion because I thought Mathematica could do multivariate Taylor expansion...

Anyone has any idea of what is wrong? Thanks in advance.

Last edited: Jul 27, 2012
2. Jul 27, 2012

### jackmell

You need to give us a canonical example. Like:

$$f(a,b,c,d)=2a+3ab^2-c^3+\sin(d)$$

Ok, suppose it's that. Can Mathematica do even that? Now you said each of the variables are functions of t as in:

$$f(t)=2a(t)+3a(t)b(t)^2-c(t)^3+\sin(d(t))$$

Ok, suppose it's that. Not sure what you want at this point though.

3. Jul 27, 2012

Thank you very much Jackmell. That is basically the idea, but my function f is much more complicated than that. That is why it would be a very tedious work to write it in Latex.
I tried to export it with Mathematica function, but I have not got pretty satisfactory results.

So, let´s focus, the question is if Mathematica is able to do Taylor series expansion of f for all of the 4 variables. (In my expression, is like if only were able to recognize the first 2 as variables of f, i.e. in the function you wrote, a,b, but no for c,d)

4. Jul 27, 2012

### jackmell

Ok, that's what "canonical" means. You don't have to post your function, only a simple piece of it or something simple that looks kind of like it minus any unnecessary trappings like hard-to-read variables, unnecessary constant expressions, and the like.

Tell you what, the example above wasn't a good one. I'll use:

$$f(a,b,c,d)=a \sin(b+c)-\cos(ad) e^{ac}$$

Here's the code and result and note I first block the code, choose Cell/Convert To/Raw Input Form, then cut and paste it here so all the formatting is gone but at least it correctly presented:
Code (Text):

In[15]:=
f[a_, b_, c_, d_] := a*Sin[b + c] -
Cos[d*a]*Exp[a*c]
Normal[Series[f[a, b, c, d], {a, 0, 5}, {b, 0, 5},
{c, 0, 5}, {d, 0, 5}]]

Out[16]=
-1 + a*b - (a*b^3)/6 + (a*b^5)/120 +
(-((a*b^2)/2) + (a*b^4)/24)*c +
(-(a^2/2) - (a*b)/2 + (a*b^3)/12 - (a*b^5)/240)*
c^2 + (-(a/6) - a^3/6 + (a*b^2)/12 -
(a*b^4)/144)*c^3 + (-(a^4/24) + (a*b)/24 -
(a*b^3)/144 + (a*b^5)/2880)*c^4 +
(a/120 - a^5/120 - (a*b^2)/240 + (a*b^4)/2880)*
c^5 + (a^2/2 + (a^3*c)/2 + (a^4*c^2)/4 +
(a^5*c^3)/12)*d^2 + (-(a^4/24) - (a^5*c)/24)*d^4

Is this not what you want?

Last edited: Jul 27, 2012
5. Jul 27, 2012

Thank you very much! That is exactly in the same syntax form I wrote. I will copy/paste but I think it will be useless

f[x_, \[Theta]_, \[Alpha]_, vm_] := ((1/(rm*rmp^2))*(7*(js + h^2*(mc + mp)) - 3*h^2*mp*Cos[\[Alpha][t]]^2 - 3*h*mp*Sin[2*\[Alpha][t]]*x[t] + (7*(mc + mp) - 3*mp*Sin[\[Alpha][t]]^2)*x[t]^2)*
(14*kg*kt*rmp*\[Eta]g*\[Eta]m*vm[t] - 2*Derivative[1][x][t]*(7*beq*rm*rmp^2 + 7*kg^2*km*kt*\[Eta]g*\[Eta]m - 3*mp*rm*rmp^2*Sin[2*\[Alpha][t]]*Derivative[1][\[Theta]][t]) +
rm*rmp^2*(g*((14*mc + 11*mp)*Sin[\[Theta][t]] - 3*mp*Sin[2*\[Alpha][t] + \[Theta][t]]) + 7*lp*mp*Sin[\[Alpha][t]]*Derivative[1][\[Alpha]][t]^2 +
14*lp*mp*Sin[\[Alpha][t]]*Derivative[1][\[Alpha]][t]*Derivative[1][\[Theta]][t] + (mp*(7*lp + 6*h*Cos[\[Alpha][t]])*Sin[\[Alpha][t]] + (14*mc + 11*mp - 3*mp*Cos[2*\[Alpha][t]])*x[t])*
Derivative[1][\[Theta]][t]^2)) + (h*(7*(mc + mp) - 3*mp*Cos[\[Alpha][t]]^2) - 3*mp*Cos[\[Alpha][t]]*Sin[\[Alpha][t]]*x[t])*
((-mp)*(7*lp + 6*h*Cos[\[Alpha][t]] + 6*Sin[\[Alpha][t]]*x[t])*((-g)*Sin[\[Alpha][t] + \[Theta][t]] + 2*Sin[\[Alpha][t]]*Derivative[1][x][t]*Derivative[1][\[Theta]][t] +
(h*Sin[\[Alpha][t]] - Cos[\[Alpha][t]]*x[t])*Derivative[1][\[Theta]][t]^2) - 7*(2*g*h*mc*Sin[\[Theta][t]] + 2*g*h*mp*Sin[\[Theta][t]] + 2*c*g*ms*Sin[\[Theta][t]] + g*lp*mp*Sin[\[Alpha][t] + \[Theta][t]] +
h*lp*mp*Sin[\[Alpha][t]]*Derivative[1][\[Alpha]][t]^2 - 2*lp*mp*Sin[\[Alpha][t]]*Derivative[1][x][t]*Derivative[1][\[Theta]][t] + 2*h*lp*mp*Sin[\[Alpha][t]]*Derivative[1][\[Alpha]][t]*
Derivative[1][\[Theta]][t] + x[t]*((-lp)*mp*Cos[\[Alpha][t]]*Derivative[1][\[Alpha]][t]^2 - 2*lp*mp*Cos[\[Alpha][t]]*Derivative[1][\[Alpha]][t]*Derivative[1][\[Theta]][t] +
2*(mc + mp)*(g*Cos[\[Theta][t]] - 2*Derivative[1][x][t]*Derivative[1][\[Theta]][t])))))/(7*(js*(14*mc + 11*mp - 3*mp*Cos[2*\[Alpha][t]]) + 2*(7*mc^2 + 11*mc*mp + 4*mp^2)*x[t]^2));

and that is the next order and the output

Normal[Series[f[x, \[Theta], \[Alpha], vm], {x, 0, 1}, {\[Theta], 0, 1}, {\[Alpha], 0, 1}, {vm, 0, 1}]]

f[0, 0, 0, 0] + x f [1, 0, 0, 0][0, 0, 0, 0] + θ (f [0, 1, 0, 0][0, 0, 0, 0] + x f [1, 1, 0, 0][0, 0, 0, 0]) +
α (f [0, 0, 1, 0] [0, 0, 0, 0] + x f [1, 0, 1, 0] [0, 0, 0, 0] + θ (f [0, 1, 1, 0] [0, 0, 0, 0] + x f[1, 1, 1, 0] [0, 0, 0, 0])) + vm (f (0,0,0,1) [0, 0, 0, 0] + x f (1,0,0,1) [0, 0, 0, 0] + θ (f (0,1,0,1) [0, 0, 0, 0] + x f (1,1,0,1) [0, 0, 0, 0]) + α(f (0,0,1,1) [0, 0, 0, 0] + x f (1,0,1,1) [0, 0, 0, 0] + θ(f (0,1,1,1) [0, 0, 0, 0] + x f (1,1,1,1) [0, 0, 0, 0])))

6. Jul 27, 2012

### jackmell

Looks like you're mixing up function names and variable names. Take a simple example:

f[a_]:=cos(a(t))

Then you attempt to compute the series in terms of a as:

Series[f[a],{a,0,5}]

I'm not sure how Mathematica is handling that. If I wanted a series in terms of t, I would write:

f[t_]:=cos(a(t))

then:

Series[f[t],{t,0,5}]

but not sure that's what you want.

7. Jul 27, 2012

### Hepth

Yeah this is a weird way of defining things.

If you want a series in "t" then you can just do that one series.

If you want a series in x, theta, alpha and vm, then they should NOT be defined as is:

f[a_]:= Cos[a[t]]

Means if I put f[Sin] I get Cos[Sin[t]]

Basically the variable is the FUNCTION "a" and not its value at that time "t".

EXAMPLE :

f[a_] := Cos[a[t]]
D[f[a], a]
D[f[a], a[t]]

The first is NOT what you want, the second IS. So your "variable" should be a[t] not "a".:

Normal[Series[
f[x, \[Theta], \[Alpha], vm], {x[t], 0, 1}, {\[Theta][t], 0,
1}, {\[Alpha][t], 0, 1}, {vm[t], 0, 1}]]

8. Jul 30, 2012

Thank you both very much!!

It was the problem. (My misconception of what Mathematica undertood as variables... )

I put the expression in a explicit dependency of time form and It seems to work, although It was not linear at all .... ???? I don't know why.

Normal[Series[f1[x, θ,α, vm], {x[t], 0, 1}, {θ[t], 0, 1}, {α[t], 0, 1}, {vm[t], 0, 1}]] /. {Derivative[1][x][t] -> 0, Derivative[1][θ][t] -> 0,
Derivative[1][α][t] -> 0, Derivative[1][vm][t] -> 0}

in order to full linearize but the expression I get (after value subsitution) is this:

2459.07 (-0.000560617 α[t] + 0.000219434 vm[t] (2.84172 - 0.1725 x[t] α[t]) +
0.00372572 θ[t] + x[t] (-0.00100995 + 0.000296346 α[t] θ[t]))

where you could see that it is a non linear expression

It is supposed that I have to get a State Space Linear Model of this and another two equations, but I don't know why these non/linear terms appeared there

I also suppose that is correct to assign 0 to the first derivatives of the generalized coordinates, because It is a State of equilibrium and in the Taylor expansion form, you must evaluate the first derivatives in that point, but maybe I am wrong in this belief

9. Sep 6, 2012