4

I have a question. I want to write the result of the expression such as y=2cos(n.Pi/3) for 100 values n into a list. Could you tell me which code I should use?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Vô Danh
  • 163
  • 5

3 Answers3

6

As @LouisB comments, using Table:

f[n_] := 2 Cos[n π/3]
Table[f[n], {n, 1, 100}]

({1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1,
-2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2,
-1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1, -1, -2, -1, 1, 2, 1,-1, -2, -1}
)

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
  • 1
    https://mathematica.stackexchange.com/questions/269646/table-mix-with-if-function Could you explain to me this question? – Vô Danh Jun 19 '22 at 12:21
5

You can take advantage of the Listable attribute of the trigonometric functions (ie the fact that they automatically thread over lists, to produce a list of results) as follows:

2 Cos[Pi/3 Range@100]
MarcoB
  • 67,153
  • 18
  • 91
  • 189
Andreas Lauschke
  • 4,009
  • 22
  • 20
  • https://mathematica.stackexchange.com/questions/269646/table-mix-with-if-function Could you explain to me this question? – Vô Danh Jun 19 '22 at 12:21
1

Using Array :

Array[2 Cos[# \[Pi]/3] &, 100]

also delivers the same output. If you don't want to use pure functions or if a function is available, then you can use the function (as given in the other answer).

f[n_] := 2 Cos[n π/3]
Array[f, 100]
Syed
  • 52,495
  • 4
  • 30
  • 85