import casadi.* x = SX.sym('x'); jacobian(sin(x),x) import casadi.* % Symbolic representation x=SX.sym('x'); y=SX.sym('y'); z=y-(1-x)^2; f=x^2+100*z^2; P=struct('x',[x;y],'f',f); % Create solver instance F=nlpsol('F','ipopt',P); % Solve the problem r=F('x0',[2.5 3.0]); disp(r.x) import casadi.* % Formulate the NLP x=SX.sym('x'); y=SX.sym('y'); z=SX.sym('z'); f=x^2+100*z^2; g=z+(1-x)^2-y; P=struct('x',[x;y;z],'f',f,'g',g); % Create solver instance F=nlpsol('F','ipopt',P); % Solve the problem r=F('x0',[2.5 3.0 0.75],'ubg',0,'lbg',0); disp(r.x) import casadi.* opti = Opti(); x = opti.variable(); y = opti.variable(); z = opti.variable(); opti.minimize(x**2+100*z**2); opti.subject_to(z+(1-x)**2-y==0); opti.set_initial(x,2.5); opti.set_initial(y,3.0); opti.set_initial(z,0.75); opti.solver('ipopt'); sol = opti.solve(); [sol.value(x),sol.value(y)] import casadi.* % Formulate the ODE x=SX.sym('x',2); p=SX.sym('p'); z=1-x(2)^2; f=[z*x(1)-x(2)+p;x(1)]; dae=struct('x',x,'p',p,'ode',f); % Create solver instance op=struct('t0',0,'tf',1); F=integrator('F','cvodes',dae,op); % Solve the problem r=F('x0',[0,1],'p',0.1); disp(r.xf) % Create Jacobian function D=F.factory('D',{'x0','p'},{'jac:xf:x0'}); % Solve the problem r=D('x0',[0,1],'p',0.1); disp(r.jac_xf_x0) import casadi.* % Formulate the DAE x=SX.sym('x',2); z=SX.sym('z'); u=SX.sym('u'); f=[z*x(1)-x(2)+u;x(1)]; g=x(2)^2+z-1; h=x(1)^2+x(2)^2+u^2; dae=struct('x',x,'p',u,'ode',f,'z',z,'alg',g,'quad',h); % Create solver instance T = 10; % end time N = 20; % discretization op=struct('t0',0,'tf',T/N); F=integrator('F','idas',dae,op); % Empty NLP w={}; lbw=[]; ubw=[]; G={}; J=0; % Initial conditions Xk=MX.sym('X0',2); w{end+1}=Xk; lbw=[lbw;0;1]; ubw=[ubw;0;1]; for k=1:N % Local control name=['U' num2str(k-1)]; Uk=MX.sym(name); w{end+1}=Uk; lbw=[lbw;-1]; ubw=[ubw; 1]; % Call integrator Fk=F('x0',Xk,'p',Uk); J=J+Fk.qf; % Local state name=['X' num2str(k)]; Xk=MX.sym(name,2); w{end+1}=Xk; lbw=[lbw;-.25;-inf]; ubw=[ubw; inf; inf]; G{end+1}=Fk.xf-Xk; end % Create NLP solver nlp=struct('f',J,'g',vertcat(G{:}),'x',vertcat(w{:})); S=nlpsol('S','ipopt',nlp,struct('ipopt',struct('tol',1e-6))); % Solve NLP r=S('lbx',lbw,'ubx',ubw,'x0',0,'lbg',0,'ubg',0); disp(r.x); import casadi.* % Formulate the DAE x=SX.sym('x',2); z=SX.sym('z'); u=SX.sym('u'); f=[z*x(1)-x(2)+u;x(1)]; g=x(2)^2+z-1; h=x(1)^2+x(2)^2+u^2; dae=struct('x',x,'p',u,'ode',f,'z',z,'alg',g,'quad',h); % Create solver instance T = 10; % end time N = 20; % discretization op=struct('t0',0,'tf',T/N); F=integrator('F','idas',dae,op); opti = Opti(); % Build up objective J = 0; % Initial conditions Xk=opti.variable(2); opti.subject_to(Xk==[0;1]); for k=1:N % Local control Uk=opti.variable(); opti.subject_to(-1<=Uk<=1); % Call integrator Fk=F('x0',Xk,'p',Uk); J=J+Fk.qf; % Local state Xk=opti.variable(2); opti.subject_to(Xk(1)>=-0.25); opti.subject_to(Fk.xf==Xk); end opti.minimize(J); % Create NLP solver opti.solver('ipopt',struct,struct('tol',1e-6)); % Solve NLP sol = opti.solve(); sol.value(opti.x')