Problem 13.7 of Additional Exercises by Stephen Boyd: Energy consumption optimization problem Consider a unit mass electric vehicle with position $x(t)∈ℝ$, moving with velocity $v(t)$ and acceleration $a(t)$. The vehicle needs to be maneuvred from initial position of rest to final position $x(T)$ at time $t=T$ satisfying $x(T)≥x_f$. Requirements are $\dot v≤A$, $0≤v(t)≤V$, $v(T)≤v_f$.
Let $p(t),p_b(t),p_d(t)$ be power delivered by battery, power lost in braking, power lost due to drag respectively and $k(t)$ be kinetic energy of vehicle which is $v(t)^2/2$. Then, $$p(t)=\dot k(t)+p_b(t)+p_d(t)$$ where where, $p_d(t)=cv(t)^3$ and the power used for braking $p_b(t)$ can be controlled.
Take $T=nh$ and divide interval [$0,T]$ into $n$ subintervals of length $h$ to discretize the vehicle equations of motion, $\dot x=v,\dot v=a$. In discrete form we get following equations
$$x_{r+1}=x_r+h(v_r+v_{r+1})/2$$
$$v_{r+1}=v_r+ha_r$$
$$p_r=(k_{r+1}−kr)/h+p_{b,r}+p_{d,r}$$
$$k_r=v^2_r/2$$
Total energy consumed is $E=h \sum_{r=0}^{n}p_r$.

*

*Formulate an optimization problem for minimum energy consumption. Write all variables and constraints on them. Indicate decision variables. Comment on the nature of the problem.


*Solve the problem by taking $x_f=12 m, v_f=2 m/s, V=10 m/s, A=2 m/s^2, c=2, h=0.1, T=5 s$.
I am fairly new to convex optimization and solving these kinds of problems. I am not sure how to go around with this one. I think formulating the optimization problem is the tough part. I think I can approach solving the problem, once I know what exactly needs to be done. Thoughts and hints are appreciated.
Thanks
Edit: I recently found out that this problem is the same as problem 13.7 of the additional exercises in Stephen Boyd's book- 2020 edition.
 A: 
$E = h \sum_{r=0}^{n}p_r$

This seems to be an error because it sums $n+1$ intervals of width $h$.
The optimization problem is almost written out in full already:
\begin{align}
\min \quad & h \sum_{r=0}^{n-1}p_r \\
\text{s.t.} \quad & x_{r+1}=x_r+h(v_r+v_{r+1})/2 \quad \forall r \in \{0,1,\ldots,n-1\} \\
&v_{r+1}=v_r+ha_r \quad \forall r \in \{0,1,\ldots,n-1\} \\
&p_r=(k_{r+1}−k_r)/h+p_{b,r}+p_{d,r} \quad \forall r \in \{0,1,\ldots,n-1\} \\
&p_{d,r}=cv_r^3 \quad \forall r \in \{0,1,\ldots,n-1\} \\
&k_r=v^2_r/2 \quad \forall r \in \{0,1,\ldots,n\} \\
& x_0 = 0 \\
& x_n \geq x_f \\
& a_r \leq A \quad \forall r \in \{0,1,\ldots,n-1\} \\
& 0 \leq v_r \leq V \quad \forall r \in \{0,1,\ldots,n\} \\
& v_n \leq v_f \\
& a,p,p_b,p_d \in \mathbb{R}^{n-1}, \; x,v,k \in \mathbb{R}^n
\end{align}
The optimization variables and their dimensions are listed on the last line. The constraints $p_{d,r}=cv_r^3$ and $k_r=v^2_r/2$ are not convex, but they can be rephrased as inequalities: $p_{d,r} \geq cv_r^3$ and $k_r \geq v^2_r/2$, which are convex inequalities on the domain $v_r \geq 0$. At optimality, the inequalities will be tight.
