In the code, you have defined the variable d to be a vector d(i) which increases its size in each iteration of loop from 1 to k. So, it is a row vector with index j containing the value of expression at i = j.
If you expect the variable d to be a scalar which contains the value of expression
((x(i+1) - x(i))^2 + (y(i+1) - y(i))^2)^0.5
at i = k, there is no need of loop. You can just use
d = ((x(k+1) - x(k))^2 + (y(k+1) - y(k))^2)^0.5