5
$\begingroup$

I am trying to replace some variables in the form of n*wt with wt. However, I came across some cases where the /. gives unexpected results. For example, -Cos[\[Pi]/6 - 6*wt] + 7/5 Cos[\[Pi]/6 + 6* wt] /. {6*wt -> wt} would give me the result -Cos[\[Pi]/6 - 6 wt] + 7/5 Cos[\[Pi]/6 + wt]. It seems that the replacement worked for the second half of the expression, but not the first half. Could anyone explain why is this and what would be a reliable way to do this?

$\endgroup$
  • 6
    $\begingroup$ Evaluate -Cos[\[Pi]/6 - 6*wt] // FullForm $\endgroup$ – Edmund Mar 10 at 22:25
  • 2
    $\begingroup$ adding to Edmunds comment: it is vital to understand that the Mathematica pattern matching is working with the internal representation (FullForm) of expressions, it is not based on mathematical identities. $\endgroup$ – Albert Retey Mar 11 at 19:42
8
$\begingroup$

You can use

-Cos[π/6 - 6*wt] + 7/5 Cos[π/6 + 6*wt] /. {a_ wt :> Sign[a] wt}

or, if you want to replace only coefficients 6 or -6,:

-Cos[π/6 - 6*wt] + 7/5 Cos[π/6 + 6*wt] /. {(a : 6 | -6) wt :> Sign[a] wt}

to get

-Cos[[Pi]/6 - wt] + 7/5 Cos[[Pi]/6 + wt]

| improve this answer | |
$\endgroup$
7
$\begingroup$

Mathematica is generally more reliable if your replacement is for a single variable rather than an expression, such as:

-Cos[π/6 - 6*wt] + 7/5 Cos[π/6 + 6*wt] /. {wt -> wt/6}
(*7/5 Cos[wt + π/6] - Cos[π/6 - wt]*)
| improve this answer | |
$\endgroup$
3
$\begingroup$

you may try something like:

    -Cos[\[Pi]/6 - 6*wt] + 7/5 Cos[\[Pi]/6 + 6*wt] /. Cos[x_ + y_] :> Cos[x + y/6]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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