Your line
for i=2:size(t)
is wrong. size(t) returns a vector, and the colon operator does strange things when it is given a vector.
Change the size(t) to length(t)
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!