Skip to content

Instantly share code, notes, and snippets.

@jgillis
Last active September 30, 2020 13:04
Show Gist options
  • Save jgillis/6630a1e4221a686c20b502aa8783f044 to your computer and use it in GitHub Desktop.
Save jgillis/6630a1e4221a686c20b502aa8783f044 to your computer and use it in GitHub Desktop.
import casadi.*
x = MX.sym('x');
y = MX.sym('y');
xy = [x;y];
p = MX.sym('p');
nlp = struct;
nlp.x = xy;
nlp.p = p;
nlp.f = (1-x)^2 + 0.2*(y-x^2)^2;
g = (x+0.5)^2+y^2;
nlp.g = [g-p^2;...
g-p^2/2];
options = struct;
options.ipopt.print_level = 0;
options.print_time = false;
options.bound_consistency = true;
options.clip_inactive_lam = true;
options.calc_lam_x = false;
options.calc_lam_p = false;
solver = nlpsol('solver','ipopt',nlp,options);
lbx = [0 -inf];
ubx = [inf inf];
lbg = [-inf 0];
ubg = [0 inf];
z = @(xy) xy(2,:)-xy(1,:);
res = solver('x0',0,'p',p,'lbx',lbx,'ubx',ubx,'lbg',lbg,'ubg',ubg);
F = Function('F',{p},{z(res.x)});
J = Function('J',{p},{jacobian(z(res.x),p)});
p0 = linspace(1,2);
figure()
plot(p0,full(F(p0)),'b-o');
hold on
plot(p0,full(J(p0)),'rx');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment