This is a minimisation problem, to minimise the integral over possible $0\leq t \leq T$, $T$ is free,
$J = \text{min} \int_0^T (\alpha + \beta_1\cdot v \cdot R_T \cdot q+ \beta_2 \cdot \frac{M}{1000} \cdot v \cdot a^2 \cdot p \cdot q)\,dt$
where,
$R_T = b_1 + b_2 \cdot v^2 + \frac{M}{1000}\cdot a$
$ p = \text{sgn}(a) = \begin{cases} 1, & \text{if $a > 0$} \\ 0, & \text{otherwise} \end{cases} $
$ q = \text{sgn}(R_T) = \begin{cases} 1, & \text{if $R_T > 0$} \\ 0, & \text{otherwise} \end{cases} $
$s$ is travel distance, $v$ is velocity, $a$ is acceleration and subject to dynamics, $ \begin{cases} \dot s(t)= v(t)\text{;}\ v(0) = 0\text{,}\ s(T) = D \\ \dot v(t)= a(t)\text{;}\ a(0) = 0 \end{cases} $
In the above expressions, $\alpha$, $\beta_1$, $\beta_2$, $b_1$, $b_2$, $M$, $D$ are constants.
Any suggestions about computing the minimum $J$ are greatly appreciated. (Is there any routine in matlab that solves this problem?)