Try this for i : 1 thru 4 do for j :1 thru 4 do (hx[i,j] := diff( H, state[1,i], state[1,j] ), h2[i,j] := subs( hx[i,j], q1=q10, q2=q20, p1=p10, p2=p20 )) ; note the comma, the extra parens, the i:1 RJF