Skip to content
FlyingSquirrrel edited this page Mar 6, 2022 · 8 revisions

You may find a single-page cheatsheet or reference card handy in your programming workflow:

Matlab Cheat sheet

a = SX.sym('a');
b = SX.sym('b');

J = jacobian(sin(a),a);
H = hessian([a;b],[a;b]);

spy(sparsity(H))

% Function with one scalar input, one output
x = a^2;
f = Function('f',{a},{x});
x_res = f(2);

% Function with two scalar inputs, one output
x = a^2+b^2;
f = Function('f',{a,b},{x});
x_res = f(2,3);

% Function with one vector input, one output
x = a^2+b^2;
f = Function('f',{[a;b]},{x});
x_res = f([2;3]);

% Function with two inputs, two outputs
x = a^2+b^2;
y = a+b;
f = Function('f',{a,b},{x,y});
[x_res,y_res] = f(2,3);

% Calling a function that has input/output labels
x = a^2+b^2;
f = Function('f',struct('a',a,'b',b,'x',x),char('a','b'),char('x'));
res = f('a',2,'b',3);
res = f('b',3,'a',2);
res = f('a',2); % default b=0

% QP
y = a^2+b^2;
solver = qpsol('solver','qpoases',struct('x',[a;b],'f',y));
res = solver('x0',[0.1;0.2]);
full(res.x)

% NLP
y = a^2+b^2;
solver = nlpsol('solver','ipopt',struct('x',[a;b],'f',y));
res = solver('x0',[0.1;0.2]);
full(res.x)

% integrator
%   dot(a) = 1,
%   dot(b) = a^2+b^2,
%  
y = a^2+b^2;
intg = integrator('intg','cvodes',struct('x',[a;b],'ode',[1;y]));
res = intg('x0',[0.1;0.2]);
full(res.xf)
Clone this wiki locally