h(r+1)= (y(x+1)^2)*cos((2*k*pi*t)/l); Error here
Above, t was defined as
and a = K*M ==> 4. 64 ==> 256
which will have 384+1 = 385 elements. The index r+1 on the LHS is just one element in length.
Dunno what you want or intended, but that's the mismatch.