Language: C++

% initial condition
x0 = 0; % [m]
vx0 = 0; % [m/s]

% time define
t_m     = 0.25; % [s] movement time
t_pm    = 0.5;  % [s] post-movement time
t_step  = 0.01; % [s]
t_m_num = t_m/t_step; % [] corresponds to T
t_pm_num = t_pm/t_step; % [] corresponds to R

% system define
A_c = [0,1;
       0,0];
B_c = [0;1];
sys_c = ss(A_c,B_c,[],[]);
sys_d = c2d(sys_c, t_step);
A_d = sys_d.A;
B_d = sys_d.B;

% model param
k = 0.0001; % const []

% matrix define
Aeq = zeros(2,t_m_num+t_pm_num+1);
%for i=1:(t_m_num+t_pm_num)
for i=1:(t_m_num)
    temp = A_d^(t_m_num-i)*B_d;
    Aeq(:,i) = temp;
end
beq = [0.1;0]; % [m] desired position and velocity

H = zeros(t_m_num+t_pm_num+1);
for i=1:(t_m_num+t_pm_num)
    if(i <= t_m_num+1)
        for j=1:t_pm_num
            temp = (A_d^(t_m_num+j-1-(i-1))*B_d)*(A_d^(t_m_num+j-1-(i-1))*B_d).';
            H(i,i) = H(i,i) + temp(1,1);
        end
    else 
        for j=(i-t_m_num):t_pm_num
            temp = (A_d^(t_m_num+j-1-(i-1))*B_d)*(A_d^(t_m_num+j-1-(i-1))*B_d).';
            H(i,i) = H(i,i) + temp(1,1);
        end
    end
end

%% optimization
options = optimoptions('quadprog', 'Display', 'iter');
[u, fval, exitflag, output, lambda] = quadprog(H,[],[],[],Aeq,beq,[],[],[],options);
%% plot input and trajectory
t_array = linspace(0,t_m+t_pm,t_m_num+t_pm_num+1);
x = zeros(2,t_m_num+t_pm_num+1);
x(:,1) = [x0;vx0];
for i=1:t_m_num+t_pm_num
    x(:,i+1) = A_d*x(:,i) + B_d*(u(i)+normrnd(0,(k*u(i)^2)^0.5));
end

subplot(3,1,1);
plot(t_array,u);
xlabel('time[s]');
subplot(3,1,2);
plot(t_array,x(1,:)); % position
xlabel('time[s]');
ylabel('pos[m]');
subplot(3,1,3);
plot(t_array,x(2,:)); % velocity
xlabel('time[s]');
ylabel('vel[m/s]');

Compiler output:
a.cc:37:80: warning: missing terminating ' character
   37 |             temp = (A_d^(t_m_num+j-1-(i-1))*B_d)*(A_d^(t_m_num+j-1-(i-1))*B_d).';
      |                                                                                ^
a.cc:37:80: error: missing terminating ' character
   37 |             temp = (A_d^(t_m_num+j-1-(i-1))*B_d)*(A_d^(t_m_num+j-1-(i-1))*B_d).';
      |                                                                                ^~
a.cc:42:80: warning: missing terminating ' character
   42 |             temp = (A_d^(t_m_num+j-1-(i-1))*B_d)*(A_d^(t_m_num+j-1-(i-1))*B_d).';
      |                                                                                ^
a.cc:42:80: error: missing terminating ' character
   42 |             temp = (A_d^(t_m_num+j-1-(i-1))*B_d)*(A_d^(t_m_num+j-1-(i-1))*B_d).';
      |                                                                                ^~
a.cc:49:24: warning: multi-character literal with 8 characters exceeds 'int' size of 4 bytes
   49 | options = optimoptions('quadprog', 'Display', 'iter');
      |                        ^~~~~~~~~~
a.cc:49:36: warning: multi-character literal with 7 characters exceeds 'int' size of 4 bytes
   49 | options = optimoptions('quadprog', 'Display', 'iter');
      |                                    ^~~~~~~~~
a.cc:49:47: warning: multi-character character constant [-Wmultichar]
   49 | options = optimoptions('quadprog', 'Display', 'iter');
      |                                               ^~~~~~
a.cc:61:8: warning: multi-character literal with 7 characters exceeds 'int' size of 4 bytes
   61 | xlabel('time[s]');
      |        ^~~~~~~~~
a.cc:64:8: warning: multi-character literal with 7 characters exceeds 'int' size of 4 bytes
   64 | xlabel('time[s]');
      |        ^~~~~~~~~
a.cc:65:8: warning: multi-character literal with 6 characters exceeds 'int' size of 4 bytes
   65 | ylabel('pos[m]');
      |        ^~~~~~~~
a.cc:68:8: warning: multi-character literal with 7 characters exceeds 'int' size of 4 bytes
   68 | xlabel('time[s]');
      |        ^~~~~~~~~
a.cc:69:8: warning: multi-character literal with 8 characters exceeds 'int' size of 4 bytes
   69 | ylabel('vel[m/s]');
      |        ^~~~~~~~~~
a.cc:1:1: error: expected unqualified-id before '%' token
    1 | % initial condition
      | ^
a.cc:2:9: error: expected unqualified-id before '%' token
    2 | x0 = 0; % [m]
      |         ^
a.cc:3:10: error: expected unqualified-id before '%' token
    3 | vx0 = 0; % [m/s]
      |          ^
a.cc:6:17: error: expected unqualified-id before '%' token
    6 | t_m     = 0.25; % [s] movement time
      |                 ^
a.cc:7:17: error: expected unqualified-id before '%' token
    7 | t_pm    = 0.5;  % [s] post-movement time
      |                 ^
a.cc:8:17: error: expected unqualified-id before '%' token
    8 | t_step  = 0.01; % [s]
      |                 ^
a.cc:9:23: error: expected unqualified-id before '%' token
    9 | t_m_num = t_m/t_step; % [] corresponds to T
      |                       ^
a.cc:10:25: error: expected unqualified-id before '%' token
   10 | t_pm_num = t_pm/t_step; % [] corresponds to R
      |                         ^
a.cc:14:8: error: expected unqualified-id before numeric constant
   14 |        0,0];
      |        ^
a.cc:15:1: error: 'B_c' does not name a type
   15 | B_c = [0;1];
      | ^~~
a.cc:15:10: error: expected unqualified-id before numeric constant
   15 | B_c = [0;1];
      |          ^
a.cc:16:1: error: 'sys_c' does not name a type
   16 | sys_c = ss(A_c,B_c,[],[]);
      | ^~~~~
a.cc:17:1: error: 'sys_d' does not name a type
   17 | sys_d = c2d(sys_c, t_step);
      | ^~~~~
a.cc:18:1: error: 'A_d' does not name a type
   18 | A_d = sys_d.A;
      | ^~~
a.cc:19:1: error: 'B_d' does not name a type
   19 | B_d = sys_d.B;
      | ^~~
a.cc:21:1: error: expected unqualified-id before '%' token
   21 | % model param
      | ^
a.cc:22:13: error: expected unqualified-id before '%' token
   22 | k = 0.0001; % const []
      |             ^
a.cc:26:1: error: expected unqualified-id before '%' token
   26 | %for i=1:(t_m_num+t_pm_num)
      | ^
a.cc:29:8: error: expected constructor, destructor, or type conversion before '(' token
   29 |     Aeq(:,i) = temp;
      |        ^
a.cc:30:1: error: 'end' does not name a type
   30 | end
      | ^~~
a.cc:31:12: error: expected unqualified-id before numeric constant
   31 | beq = [0.1;0]; % [m] desired position and velocity
      |            ^
a.cc:31:16: error: expected unqualified-id before '%' token
   31 | beq = [0.1;0]; % [m] desired position and velocity
      |                ^
a.cc:34:1: error: expected unqualified-id before 'for'
   34 | for i=1:(t_m_num+t_pm_num)
      | ^~~
a.cc:39:9: error: 'end' does not name a type
   39 |         end
      |         ^~~
a.cc:44:9: error: 'end' does not name a type
   44 |         end
      |         ^~~
a.cc:50:1: error: expected unqualified-id before '[' token
   50 | [u, fval, exitflag, output, lambda] = quadprog(H,[],[],[],Aeq,beq,[],[],[],options);
      | ^
a.cc:51:1: error: expected unqualified-id before '%' token
   51 | %% plot input and trajectory
      | ^
a.cc:53:1: error: 'x' does not name a type
   53 | x = zeros(2,t_m_num+t_pm_num+1);
      | ^
a.cc:54:2: error: expected constructor, destructor, or type conversion before '(' token
   54 | x(:,1) = [x0;vx0];
      |  ^
a.cc:54:14: error: 'vx0' does not name a type
   54 | x(:,1) = [x0;vx0];
      |              ^~~
a.cc:55:1: error: expected unqualified-id before 'for'
   55 | for i=1:t_m_num+t_pm_num
      | ^~~
a.cc:57:1: error: 'end' does not name a type
   57 | end
      | ^~~
a.cc:60:5: error: expected constructor, destructor, or type conversion before '(' token
   60 | plot(t_array,u);
      |     ^
a.cc:61:7: error: expected constructor, destructor, or type conversion before '(' token
   61 | xlabel('time[s]');
      |       ^
a.cc:62:8: error: expected constructor, destructor, or type conversion before '(' token
   62 | subplot(3,1,2);
      |        ^
a.cc:63:5: error: expected constructor, destructor, or type conversion before '(' token
   63 | plot(t_array,x(1,:)); % position
      |     ^
a.cc:63:23: error: expected unqualified-id before '%' token
   63 | plot(t_array,x(1,:)); % position
      |                       ^
a.cc:65:7: error: expected constructor, destructor, or type conversion before '(' token
   65 | ylabel('pos[m]');
      |       ^
a.cc:66:8: error: expected constructor, destructor, or type conversion before '(' token
   66 | subplot(3,1,3);
      |        ^
a.cc:67:5: error: expected constructor, destructor, or type conversion before '(' token
   67 | plot(t_array,x(2,:)); % velocity
      |     ^
a.cc:67:23: error: expected unqualified-id before '%' token
   67 | plot(t_array,x(2,:)); % velocity
      |                       ^
a.cc:69:7: error: expected constructor, destructor, or type conversion before '(' token
   69 | ylabel('vel[m/s]');
      |       ^