CODE 1:

```
clc;
clear all;
close all;
h=0.1;
T=5;
n=50;
xf=12;
vf=2;
Vg=10;
Ag=2;
c=2;
cvx_begin
variables Pb(50) V(50) A(50) X(50) k(50)
for i=1:n-1
E1=10*(k(i+1)-k(i))+h*Pb(i)+h*c*pow_abs(V(i),3);
if i==1
E=E1;
else
E=E1+E;
end
end
minimize E
subject to
for i=1:n-1
V(i+1)-V(i)==h*A(i);
X(i+1)-X(i)==0.5*h*(V(i+1)+V(i));
A(i)<=Ag;
V(i)>=0;
V(i)<=Vg;
k(i)==pow_abs(V(i),2)/2;
end
xf-X(n)<=0;
V(n)-vf<=0;
cvx_end
disp(E);
```

CODE 2:

```
clc;
clear all;
close all;
h=0.1;
T=5;
n=50;
xf=12;
vf=2;
Vg=10;
Ag=2;
c=2;
cvx_begin
variables Pb(50) V(50) A(50) X(50)
for i=1:n-1
E1=((V(i+1)^2-V(i)^2)/2)+h*Pb(i)+h*c*V(i)^3;
if i==1
E=E1;
else
E=E1+E;
end
end
minimize E
subject to
for i=1:n-1
V(i+1)-V(i)==h*A(i);
X(i+1)-X(i)==0.5*h*(V(i+1)+V(i));
A(i)<=Ag;
V(i)>=0;
V(i)<=Vg;
end
xf-X(n)<=0;
V(n)-vf<=0;
cvx_end
disp(E);
```

I am trying to minimize E (Energy) by using above two codes, but they are violating DCP ruleset. It would be of great help if you find what mistake I am making here.

V:Velocity ; X:position; Pb:Braking Power; vf:Final Velocity ; xf:Final Position; Vg:Max Velocity; Ag:Max acceleration; h:step size; c:drag Coefficient