1
$\begingroup$

Simple example:

k = 5;
list = {};
For[i = 1, i <= k - 2,
 AppendTo[list, 1/(1 + Cos[i Pi/k])];
 i = i + 2];
Print[list];

{1/(1+1/4 (1+Sqrt[5])),1/(1+1/4 (1-Sqrt[5]))}

I want the expression to be kept and printed as:

{1/(1 + Cos[Pi/5]), 1/(1 + Cos[3 Pi/5])}

I have tried Hold and its several versions, but nothing seems to do the trick.

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – chris Apr 1 '13 at 7:03
3
$\begingroup$
k = 5;
list = {};
For[i = 1, i <= k - 2, 
  With[{i = i, k = k}, 
   AppendTo[list, HoldForm[1/(1 + Cos[(i \[Pi])/k])]]];
  i = i + 2];
Print[list];

Mathematica graphics

$\endgroup$
  • 1
    $\begingroup$ Perhaps Table[With[{i = i, k = k}, HoldForm[1/(1 + Cos[i Pi /k])]], {i, 1, k - 2, 2}] is more Mathemaica-ish $\endgroup$ – Dr. belisarius Mar 31 '13 at 23:22
  • $\begingroup$ @belisarius I know, but that's not what the OP wanted. $\endgroup$ – Sjoerd C. de Vries Apr 1 '13 at 8:31
  • $\begingroup$ Sjoerd: I know you know :). I was "talking" to the OP. Sorry for the confusion $\endgroup$ – Dr. belisarius Apr 1 '13 at 8:46
3
$\begingroup$

As stated HoldForm is your friend here. Also see Defer if you intend to reuse the output.

Equivalent methods:

Table[With[{i = i, k = k}, HoldForm[1/(1 + Cos[i Pi/k])]], {i, 1, k - 2, 2}]

Table[{i, k} /. {i_, k_} :> HoldForm[1/(1 + Cos[i Pi/k])], {i, 1, k - 2, 2}]

Table[HoldForm[1/(1 + Cos[# Pi/#2])] &[i, k], {i, 1, k - 2, 2}]

Mathematica graphics

$\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.