1
$\begingroup$

For example a vector:

vec={l (Cos[φ1[t]] + Cos[φ2[t]] + 
    Cos[φ3[t]] + Cos[φ4[t]] + 
    Cos[φ5[t]] + Cos[φ6[t]] + 
    Cos[φ7[t]] + Cos[φ8[t]] + 
    1/2 Cos[φ9[t]]), 
 l (-Sin[φ1[t]] - Sin[φ2[t]] - 
    Sin[φ3[t]] - Sin[φ4[t]] - 
    Sin[φ5[t]] + Sin[φ6[t]] + 
    Sin[φ7[t]] + Sin[φ8[t]] + 
    1/2 Sin[φ9[t]])}

And I want to use Taylor Expansion on ALL Cos and Sin terms (up to first order). So I want to use something like Series[Cos[x],{x,1}] but I don't want to write that for every variable, because there can be $n$ variables where $n$ is defined by the user.

So is there a more compact and faster way or do I really have to Taylor expand one term at once for all of them?

$\endgroup$
3
  • 2
    $\begingroup$ Does this do it? vec /. {(a : Cos | Sin)[x_] :> Normal@Series[a[x], {x, 0, 1}]}. (I assumed you wanted to expand about 0 in each case, but if not, change the 0 to something else.) $\endgroup$
    – march
    Mar 25, 2016 at 19:43
  • $\begingroup$ @march: It sure does! Thanks! $\endgroup$
    – skrat
    Mar 25, 2016 at 19:57
  • $\begingroup$ Are you expanding at t=0? If so, could just do Series[vec, {t, 0, 2}]. $\endgroup$ Mar 25, 2016 at 21:51

2 Answers 2

2
$\begingroup$

Here is one straight-forward way. Taking

vec = l*{
  Cos[f1[t]] + Cos[f2[t]] + Cos[f3[t]] + Cos[f4[t]] + Cos[f5[t]] + Cos[f6[t]] + Cos[f7[t]] + Cos[f8[t]] + 1/2 Cos[f9[t]],
  -Sin[f1[t]] - Sin[f2[t]] - Sin[f3[t]] - Sin[f4[t]] - Sin[f5[t]] + Sin[f6[t]] + Sin[f7[t]] + Sin[f8[t]] + 1/2 Sin[f9[t]]
};

we have

vec /. {Cos[x_] :> Normal@Series[Cos[x], {x, 0, 1}], Sin[x_] :> Normal@Series[Sin[x], {x, 0, 1}]}
(* {(17 l)/2, l (-f1[t] - f2[t] - f3[t] - f4[t] - f5[t] + f6[t] + f7[t] + f8[t] + f9[t]/2)}

We can package the two rules together using Alternatives (|) on Sin or Cos and naming the pattern:

vec /. {(a : Cos | Sin)[x_] :> Normal@Series[a[x], {x, 0, 1}]}

Per a comment by the OP, here's a slightly more general version:

vec = {a Cos[x[t] + d] x'[t], b Sin[x[t] + y[t] + d] x'[t] y'[t]};
vec /. {(a : Cos | Sin)[x_] :> (Normal@Series[a[y], {y, 0, 1}] /. y -> x)}
(* {a x'[t], b (d + x[t] + y[t]) x'[t] y'[t]} *)

Alternatively, adapting Jens' solution,

vec /. {(a : Cos | Sin)[x_] :> (Normal@Series[a[ϵ x], {ϵ, 0, 1}] /. ϵ -> 1)}
$\endgroup$
7
  • 1
    $\begingroup$ Really compact: vec /. f_[t_] :> Normal[f[t] + O[t]^2] $\endgroup$ Mar 26, 2016 at 0:42
  • $\begingroup$ This works.. nicely until you come across something like Cos[x[t]+y[t]]. Is it possible to extend your idea for this case to? $\endgroup$
    – skrat
    Mar 31, 2016 at 17:17
  • $\begingroup$ @skrat. I'm pretty sure that Jens' solution below actually works for the general case, so perhaps you should accept his answer instead! $\endgroup$
    – march
    Mar 31, 2016 at 18:27
  • $\begingroup$ The problem with Jens' solution is that is too general! For example the result of this Normal[Series[ Cos[x1[t] - x2[t]] x1'[t] x2'[t] /. h_[t] :> \[Epsilon] h[t], {\[Epsilon], 0, 1}]] /. \[Epsilon] -> 1 is zero while i want it to be (x1[t] - x2[t])x1'[t] x2'[t]. So Jens' method, as he wrote works also on products etc.. which is not something I would like at the moment. $\endgroup$
    – skrat
    Mar 31, 2016 at 18:32
  • 1
    $\begingroup$ @skrat. See updated post. $\endgroup$
    – march
    Mar 31, 2016 at 18:42
2
$\begingroup$

To be more general and also allow it to work to the desired order when products of cosines or sines appear, I would do the following:

Normal[
  Series[vec /. h_[t] :> ϵ h[t], {ϵ, 0, 1}]] /. ϵ -> 1

(*
==> {(17 l)/2, 
  -l φ1[t] - l φ2[t] - l φ3[t] -
   l φ4[t] - l φ5[t] + l φ6[t] + 
  l φ7[t] + l φ8[t] + 1/2 l φ9[t]}
*)

This will also apply without modification to expressions involving functions other than Sin or Cos. The replacement multiplies every function of t by a small parameter ϵ and expands with respect to that. Then I set the parameter to 1 in the end.

Edit after question was modified in comment

The above approach will of course continue to yield consistent expansions if there are products of small quantities that you would like to keep. All you need to do is increase the order of expansion in ϵ to the desired power. Specifically, for the example in the comment to @march's answer, you would do this:

Normal[
  Series[Cos[x1[t] - x2[t]] x1'[t] x2'[t] /. 
    h_[t] :> ϵ h[t], {ϵ, 0, 2}]] /. ϵ -> 1

(* ==> Derivative[1][x1][t] Derivative[1][x2][t] *)

One should emphasize that my goal here is to obtain consistent expansions. If you want inconsistent approximations (which I wouldn't recommend doing), then of course a more selective use of replacement rules would be required.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.