the answer from stackoverflow The observed behavior is exactly the expected behavior, you assign repeatedly to bv, the return value is the last assigned value.
You will need to pass the index i to the boundary condition function, either as a parameter
sol = bvp4c(@Kpath1,@(L,R)bcpath(L,R,i),init);
with
function bv = bcpath(L,R,j)
bv = [L(1)-Y(j) R(1)-Y(j+1)];
end
or by redefining bcpath in every iteration, using the index as global variable,
for i = 1:length(X)-1
function bv = bcpath(L,R)
bv = [L(1)-Y(i) R(1)-Y(i+1)];
end
init = bvpinit(linspace(X(i),X(i+1),10),[0 0]);
sol = bvp4c(@Kpath1,@bcpath,init);
...