Code makes no sense
x=xhist(1,:);
x = sdpvar(1);
First line is redundant, as you overwrite that variable immediately
[p,a,v] = polynomial(x,6);
Why are you definig a scalar polynomial, when the path is in 2D? OK, I see you want y(x), but when moving in 2D, it is much much more natural to have {x(t),y(t)}. Otherwise it is impossible to go straight down (as your path appears to want to do), or move to the right and down and then turn around and go left and down
You define p but never use it
norm(replace(y,x,t_i) -
What is y, it is not defined. There is no variable called t_i either