0
$\begingroup$
a1 D11 Cos[n x] + a0 a1 D11 Cos[n x] + 1/2 a1 a3 D11 Cos[n x] + 1/2 a2 a4 D11 Cos[n x]

I have a large trigonometric expression. Can I factor out different coefficients to Cos[n x] and Sin[n x] so that it will be easy to copy the expression corresponding to each trigonometric term? I just want the coefficient to each trigonometric function to be printed out separately.

$\endgroup$
  • $\begingroup$ I have tried to reword your question to make it somewhat more readable. However, I'd appreciate it if you could clarify it further. I am afraid that it is still not very clear what you need to accomplish. $\endgroup$ – MarcoB May 6 '15 at 8:15
  • $\begingroup$ Thanks for helping me out. it was not written clear by me, sorry for that. {%[[1, 1]], %[[2, 1]]} this what i was needed as i wanted to print out different coefficients one by one. So now can i use this syntax. $\endgroup$ – Amandeep May 6 '15 at 8:22
  • $\begingroup$ Amandeep, I noticed that you un-accepted my answer to your question. Does my answer not satisfy your question any more? $\endgroup$ – MarcoB May 11 '15 at 18:44
  • $\begingroup$ Collect[%, {Cos[n x], Sin[n x]}] this function is not working properly when we have resultant expression in fortran Form e.g xpr = a1* D11* Cos[n* x] + a0* a1 D11 Cos[n x] + 1/2 a1 *a3 *D11 *Cos[n *x] + 1/2 *a2 *a4 D11 Cos[n x] + a2*a3*a4*Sin[nx] then How we can segregate Cos and Sin terms. And Also we should be able to print out differently one by one so to copy coefficients easily $\endgroup$ – Amandeep May 11 '15 at 19:04
  • $\begingroup$ Amandeep, is it possible that you have a typo in the last term of the expression in your last comment, i.e. Sin[nx] ? Shouldn't that be Sin[n*x] or, equivalently, Sin[n x] with a space between n and x? If in fact a space is needed there, then it seems to me that the Collect function still works as intended. I will also add another possible way to achieve the same thing using Coefficient whose output may be easier to copy out. $\endgroup$ – MarcoB May 11 '15 at 19:17
1
$\begingroup$

Second update (2015-05-11):

Amandeep, you recently left a comment with the following expression:

xpr = a1*D11*Cos[n*x] + a0*a1*D11*Cos[n*x] + 1/2*a1*a3*D11*Cos[n*x] + 1/2*a2*a4*D11*Cos[n*x] + a2*a3*a4*Sin[n*x] 

I believe that you may have left out a multiplication sign on the argument of the last Sin function. Once we add that back in, the approach using Collect still seems to work:

Collect[xpr, {Cos[n x], Sin[n x]}]

(* Out: 
(a1 D11 + a0 a1 D11 + (a1 a3 D11)/2 + (a2 a4 D11)/2) Cos[n x] + a2 a3 a4 Sin[n x] 
*)

An alternative approach is to use the Coefficient function:

Coefficient[xpr, Cos[n x]]
Coefficient[xpr, Sin[n x]]

(*Out:
a1 D11 + a0 a1 D11 + (a1 a3 D11)/2 + (a2 a4 D11)/2
a2 a3 a4
*)

Hopefully those coefficients should be easy enough to copy and paste, or to otherwise work with programmatically.

First update

Upon re-reading your question, I realized that the D11 Cos[n x] factor is common to all terms in your original expression. It would be just as easy to collect that term:

a1 D11 Cos[n x] + a0 a1 D11 Cos[n x] + 1/2 a1 a3 D11 Cos[n x] + 1/2 a2 a4 D11 Cos[n x];
Collect[%, D11 Cos[n x]]

(* Output: (a1 + a0 a1 + (a1 a3)/2 + (a2 a4)/2) D11 Cos[n x] *)

In my understanding, you want to factor the expression to obtain the coefficients of the $\cos(n\ x)$ and $\sin(n\ x)$. However, your example does not contain any Sin[] expressions.

Let me consider a modification of your expression instead, in which I have changed some of the original Cos[n x] into Sin[n x]:

a1 D11 Cos[n x] + a0 a1 D11 Sin[n x] + 1/2 a1 a3 D11 Cos[n x] + 1/2 a2 a4 D11 Sin[n x]

You can then use Collect to obtain your coefficients:

Collect[%, {Cos[n x], Sin[n x]}]

(* Output: (a1 D11 + (a1 a3 D11)/2) Cos[n x] + (a0 a1 D11 + (a2 a4 D11)/2) Sin[n x] *)

Now they should be easier to copy out.

Alternatively, you could also programmatically extract the two coefficient from the result above, as parts of the output expression:

{%[[1, 1]], %[[2, 1]]}

(* Output: {a1 D11 + (a1 a3 D11)/2, a0 a1 D11 + (a2 a4 D11)/2} *)
$\endgroup$
0
$\begingroup$

Have you tried Simplify

expr = a1 D11 Cos[n x] + a0 a1 D11 Cos[n x] + 1/2 a1 a3 D11 Cos[n x] + 
   1/2 a2 a4 D11 Cos[n x];

expr // Simplify

1/2 (a1 (2 + 2 a0 + a3) + a2 a4) D11 Cos[n x]

expr2 = a1*D11*Cos[n*x] + a0*a1 D11*Cos[n*x] + 
   1/2 a1*a3*D11*Cos[n*x] + 1/2*a2*a4 D11*Cos[n*x] + a2*a3*a4*Sin[n*x];

expr2 // Simplify

1/2 (a1 (2 + 2 a0 + a3) + a2 a4) D11 Cos[n x] + a2 a3 a4 Sin[n x]

$\endgroup$
  • $\begingroup$ I can do above thing, but I want different expression factors to be printed out differently $\endgroup$ – Amandeep May 5 '15 at 20:17
  • $\begingroup$ Collect[%, {Cos[n x], Sin[n x]}] this function is not working properly when we have resultant expression in fortran Form e.g xpr = a1* D11* Cos[n* x] + a0* a1 D11 *Cos[n x] + 1/2 a1 *a3 *D11 *Cos[n *x] + 1/2 *a2 *a4 D11* Cos[n x] + a2*a3*a4*Sin[nx] then How we can segregate Cos and Sin terms. And Also we should be able to print out differently one by one so to copy coefficients easily. $\endgroup$ – Amandeep May 11 '15 at 17:34
  • $\begingroup$ @Amandeep - I have added the expression from your comment. If the output from the use of Simplify is not the result that you want, you need to clarify want you want by providing an explicit example of the desired output. $\endgroup$ – Bob Hanlon May 11 '15 at 18:03

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.