function y = carback(u)
% function to simulate simple car kinematics
% based on bicycle model
%
% u is [beta;psi;psi(.);v];
% y is xa(.), ya(.), xb(.), yb(.)
%
L = 3.0293; % wheelbase in m Saturn Outlook 2007
w = 1.572;     % width in m Chevy Equinox

beta = u(1);
psi = u(3);
% psid = u(2);
v = u(4);

cbeta = cos(beta);
sbeta = sin(beta);
tbeta = sbeta/cbeta;

psid = -v/L*tbeta;
cpsi = cos(psi);
spsi = sin(psi);

tpsi = spsi/cpsi;

vbb = [v; 0]; % cg v in b coords.
rba = [L; 0];

va = v/cbeta;

b2i = [cpsi -spsi; spsi cpsi];
vbi = b2i*vbb;
if abs(psi) < pi/4
  vai(2,1) = va*sin(psi+beta);
  vai(1,1) = tpsi*(vbi(2)-vai(2)) + vbi(1);
else
    vai(1,1) = va*cos(psi+beta);
    vai(2,1) = 1/tpsi*(vbi(1)-vai(1)) + vbi(2);
end
y = [vai; vbi];