1
$\begingroup$

Anyone could explain to me which problem is? after running the function "Table" combining with "If" as below, but the result is not a value instead.

 k:=8; n:=k+1; Alpha:=Pi/6; r:=3;
 In[5]:= Subscript[y, n][i_] := r cos[i \[Alpha]]

 In[6]:= Subscript[y, n] = 
 Table[if [i == 1, Subscript[y, n][0], Subscript[y, n][i]], {i, 1, n}]

 Out[6]= {if[True, 3 cos[0], 3 cos[\[Pi]/6]], 
 if[False, 3 cos[0], 3 cos[\[Pi]/3]], 
 if[False, 3 cos[0], 3 cos[\[Pi]/2]], 
 if[False, 3 cos[0], 3 cos[(2 \[Pi])/3]], 
 if[False, 3 cos[0], 3 cos[(5 \[Pi])/6]], 
 if[False, 3 cos[0], 3 cos[\[Pi]]], 
 if[False, 3 cos[0], 3 cos[(7 \[Pi])/6]], 
 if[False, 3 cos[0], 3 cos[(4 \[Pi])/3]], 
 if[False, 3 cos[0], 3 cos[(3 \[Pi])/2]]}
$\endgroup$
4
  • 1
    $\begingroup$ Try If[] instead of if [] all MM functions start with Capitals $\endgroup$
    – Lou
    Jun 19, 2022 at 12:26
  • $\begingroup$ #Lou, thank you so much. My basic fault :( $\endgroup$
    – Vô Danh
    Jun 19, 2022 at 12:35
  • 1
    $\begingroup$ Note also that the cosine function is Cos and the imaginary unit is I. When you define constants, you don't need to use SetDelayed (:=); use Set (=) instead (see this FAQ). $\endgroup$
    – MarcoB
    Jun 19, 2022 at 12:39
  • 1
    $\begingroup$ Welcome to the Mathematica Stack Exchange. The introductory book written by the inventor is a good learning resource. There is a fast intro for math students as well as a fast intro for programmers to choose from. $\endgroup$
    – Syed
    Jun 19, 2022 at 13:47

1 Answer 1

1
$\begingroup$

I assume you want something like the following:

Subscript[y, n][i_] := r Cos[i α] /. {r -> 3, α -> π/6}
Table[If[i == 1, Subscript[y, n][0], Subscript[y, n][i]], {i, 1, 9}]
(*{3, 3/2, 0, -(3/2), -((3 Sqrt[3])/2), -3, -((3 Sqrt[3])/2), -(3/2), 0}*)

Or in the same way:

Module[{s}, With[{k = 8}, s = k + 1; Table[If[i == 1, Subscript[y, n][0], Subscript[y, n][i]], {i, 1, s}]]]
(*{3, 3/2, 0, -(3/2), -((3 Sqrt[3])/2), -3, -((3 Sqrt[3])/2), -(3/2), 0}*)
$\endgroup$

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