function [u, v_lin] = speed_profile_u(params, du, v_max, at_max, ar_max, v_ini, v_end)

u = 0:du:1;
v_lin = v_max*ones(1,length(u));
cr = zeros(size(u));
dsu = zeros(size(u));

dparams = [params(:,2), 2*params(:,3), 3*params(:,4), [0;0]];
for i = 1:length(u)
    dxu = dparams(1,:) * [1;u(i);u(i)^2;u(i)^3];
    dyu = dparams(2,:) * [1;u(i);u(i)^2;u(i)^3];
    dsu(i) = sqrt(dxu^2+dyu^2);
end

mins = [];
vmins = [];
cr(1) = cr_bezier(params, u(1));
cr(2) = cr_bezier(params, u(2));
for i=3:length(u)
    cr(i) = cr_bezier(params, u(i));
    if ((abs(cr(i)) > abs(cr(i-1))) && (abs(cr(i-1)) < abs(cr(i-2))))
        mins = [mins, i];
        vmins = [vmins, sqrt(ar_max*abs(cr(i)));];
    end
end
plot(u,cr, 'b-', u(mins), cr(mins), 'bx');

mins = [1, mins, length(u)];
vmins = [v_ini, vmins, v_end];

for j=1:length(mins);
    im = mins(j);
    vm = vmins(j);
    if vm < v_max
        v_lin(im) = vm;

        % Profile for preceding velocity
        for i = (im-1):-1:1
            dt = (-v_lin(i+1)+sqrt(v_lin(i+1)^2+2*at_max*du*dsu(i+1)))/at_max;
            nv = v_lin(i+1)+at_max*dt;
            if nv < v_lin(1,i)
                v_lin(1,i) = nv;
            else
                break;
            end
        end
        % Profile for following sector
        for i = (im+1):length(u)
            dt = (-v_lin(i-1)+sqrt(v_lin(i-1)^2+2*at_max*du*dsu(i-1)))/at_max;
            nv = v_lin(i-1)+at_max*dt;
            if nv < v_lin(i)
                v_lin(i) = nv;
            else
                break;
            end
        end
    end
end
