Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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! –  chris Apr 1 '13 at 7:03

2 Answers 2

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

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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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