Currently the code above does not work since it cannot find variable `x`. Can I "extract" variable x from constr (in another function)? I guess the struct/variable constr already contains the variable information (I saw sth related in the robustify() function, but that was too complicated to understand for me..)
Clearly, if I put the content of add_y() function into the main function, everything works out. But this example is a simplification of a much more complicated case, in which it is better to define functions.
The code not in a figure:
clear
sdpvar x
constr = [x >= 1];
objective = x;
% function add_y() adds an additional constraint
% x + y >= 1, y <= -1
constr_new = add_y(constr);
optimize(constr, obj)
function f_new = add_y(f)
sdpvar y
f_new = [f, x+y >= 1, y <=-1];
end
function f_new = add_y(f)
sdpvar y
x = recover(depends(f));
clear
sdpvar x
constr = [x >= 1];
objective = x;
% function add_y() adds an additional constraint
% x + y >= 1, y <= -1
constr_new = add_y(constr);
optimize(constr, obj)
function f_new = add_y(f)
sdpvar y
z = recover(depends(f));
f_new = [f, z+y >= 1, y <=-1];
end
Thanks!