# Algebraic variables

Hello,
I wanted to ask if acados has an option to add algebraic variables to an OCP to be optimized while solving the OCP?
Thanks

Hi,

yes, you can use algebraic variables in acados.
The complete problem description can be found here.
Examples with algebraic variables in MATLAB are here and here.

Hope it helps!

Kind regards,
Josip

Dear Mr. Jospikh,
Thank you for your reply. I wanted to ask about something if you do not mind. In the example attached below, z1 and z2 are algebraic variables and hence the solver should solve for z1=x1=0 and z2=x2=0. Is my understanding correct?

``````model_name_prefix = 'simple_dae';

%% Set up States & Controls
x1    = SX.sym('x1');     % Differential States
x2    = SX.sym('x2');
x = vertcat(x1, x2);

z1      = SX.sym('z1');     % Algebraic states
z2      = SX.sym('z2');
z = vertcat(z1, z2);

u1      = SX.sym('u1');     % Controls
u2      = SX.sym('u2');
u       = vertcat(u1, u2);

%% xdot
x1_dot    = SX.sym('x1_dot');     % Differential States
x2_dot    = SX.sym('x2_dot');
xdot = [x1_dot; x2_dot];

%% cost
expr_y = vertcat(u1, u2, z1, z2);

%% Dynamics: implicit DAE formulation (index-1)
expr_f_impl = vertcat(x1_dot-0.1*x1+0.1*z2-u1, ...
x2_dot+x2+0.01*z1-u2,  ...
z1-x1, ...
z2-x2);

%% constraints
expr_h = vertcat(z1, z2);
expr_h_e = vertcat(x1, x2);

%% initial value
%     x0 = [0.1; -0.1];
%     z0 = [0.0, 0.0];
%     u0 = 0;

model.sym_x = x;
model.sym_xdot = xdot;
model.sym_u = u;
model.sym_z = z;
model.expr_y = expr_y;
model.expr_f_impl = expr_f_impl;
model.expr_h = expr_h;
model.expr_h_e = expr_h_e;
model.name = model_name_prefix;
``````

end