Taylor Expansion in Mathematica (Multivariate)

In summary, the user is attempting to use Mathematica's Series function to do a Taylor series expansion of a complicated function with 4 variables, but is running into issues with the syntax and the program only recognizing the first 2 variables as function variables. They are seeking help on how to properly use the Series function for all 4 variables.
  • #1
road_king
18
0
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:
Physics news on Phys.org
  • #2
You need to give us a canonical example. Like:

[tex]f(a,b,c,d)=2a+3ab^2-c^3+\sin(d)[/tex]

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

[tex]f(t)=2a(t)+3a(t)b(t)^2-c(t)^3+\sin(d(t))[/tex]

Ok, suppose it's that. Not sure what you want at this point though.
 
  • #3
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
road_king said:
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)

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:

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

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:
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:
  • #5
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
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
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
Hepth said:
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}]]

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.

I add this:

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
Anyone knows the proper way to assign the initial conditions for the first order derivatives to the Series function??
 

Related to Taylor Expansion in Mathematica (Multivariate)

1. What is the purpose of using Taylor expansion in Mathematica for multivariate functions?

The purpose of using Taylor expansion in Mathematica for multivariate functions is to approximate a complicated multivariate function using a simpler polynomial function. This can be useful for performing calculations and making predictions, as well as gaining a better understanding of the behavior of the original function.

2. How do I perform a Taylor expansion in Mathematica for a multivariate function?

To perform a Taylor expansion in Mathematica for a multivariate function, you can use the Series function. This function takes in the function you want to expand, the variables you want to expand around, and the order of the expansion. For example, Series[f[x,y], {x,0}, {y,0}, 2] would expand the function f around the point (x,y) = (0,0) up to the second order in both x and y.

3. How do I specify the variables and order of the expansion in Mathematica?

You can specify the variables and order of the expansion in Mathematica by using the Series function. The first argument of the function is the function you want to expand, followed by the variables you want to expand around, and the order of the expansion. For example, Series[f[x,y], {x,0}, {y,0}, 3] would expand the function f around the point (x,y) = (0,0) up to the third order in both x and y.

4. Can I perform a Taylor expansion for a specific point in Mathematica?

Yes, you can perform a Taylor expansion for a specific point in Mathematica by specifying the point as the center of the expansion. For example, Series[f[x,y], {x,1}, {y,2}, 2] would expand the function f around the point (x,y) = (1,2) up to the second order in both x and y.

5. How can I use the Taylor expansion in Mathematica to approximate a multivariate function?

You can use the Taylor expansion in Mathematica to approximate a multivariate function by choosing a higher order for the expansion. The higher the order, the closer the approximation will be to the original function. However, it is important to note that the approximation will only be accurate within a certain range around the point of expansion. Therefore, it is important to choose the point of expansion carefully to ensure a good approximation in the desired range.

Similar threads

Replies
3
Views
970
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
406
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
965
  • Linear and Abstract Algebra
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • Introductory Physics Homework Help
Replies
7
Views
759
  • Calculus and Beyond Homework Help
Replies
1
Views
967
  • General Math
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
Back
Top