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?
3 Answers
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})
- 23,117
- 3
- 21
- 44
-
1https://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
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]
- 67,153
- 18
- 91
- 189
- 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
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]
- 52,495
- 4
- 30
- 85
Tablemay give you some ideas. – LouisB Jun 18 '22 at 22:51