Skip to content

Instantly share code, notes, and snippets.

@jkoendev
jkoendev / dpc_simulate_simplified.matlab
Last active April 25, 2019 12:37
Double pendulum on a cart (dpc) simulation code, Matlab. You are free to use, modify, copy, distribute the code. See full repo: https://github.com/jkoendev/double-pendulum-on-cart
function r= dpc_simple_simulate
% get a random starting state between min state and max state
x_min = [-1; -pi; -pi; -.05; -1; -1];
x_max = -x_min;
x0 = rand(6,1) .* (x_max-x_min)+x_min;
% simulate
tspan = 0:0.01:8;
[tspan, X] = ode45(@dpc_ode, tspan, x0);
r = struct;
@jkoendev
jkoendev / dpc_simulate_simplified.py
Last active April 25, 2019 12:36
Double pendulum on a cart (dpc) simulation code, Python. You are free to use, modify, copy, distribute the code. See full repo: https://github.com/jkoendev/double-pendulum-on-cart
import numpy as np
import math
from scipy.integrate import solve_ivp
def main():
# get a random starting state between min state and max state
x_min = np.array([-1, -np.pi, -np.pi, -.05, -1, -1]);
x_max = -x_min;
x0 = np.random.uniform(low=0.0, high=1.0, size=6) * \
(x_max-x_min)+x_min;
@jkoendev
jkoendev / testNnpGeneration.m
Created February 19, 2019 08:53
try code generation for casadi nlp
x = casadi.MX.sym('x',2);
f = x(1)*x(1) + x(2)*x(2);
g = x(1)+x(2)-10;
% solver = casadi.nlpsol('solver', 'ipopt', struct('x', x, 'f', f, 'g', g),struct('jit',true));
% solver.generate_dependencies('nlp.c');
solver = casadi.nlpsol('solver', 'ipopt', struct('x', x, 'f', f, 'g', g));
solver.generate_dependencies('nlp.c',struct('mex',true));
@jkoendev
jkoendev / testJIT.m
Created February 19, 2019 08:52
Try just-in-time compilation with casadi
import casadi.*
% Degree of interpolating polynomial
d = 3;
% Get collocation points
tau_root = [0 collocation_points(d, 'radau')];
% Coefficients of the collocation equation
C = zeros(d+1,d+1);
@jkoendev
jkoendev / testCasadiDLL.m
Created February 19, 2019 08:51
Try generating casadi code dll
clear classes % necessary to update/reload .so
MEX_COMPILE = false;
x = casadi.SX.sym('x',2);
f = x;
for i=1:10
f = sin(f)*cos(f)';
end
df = jacobian(f,x);
@jkoendev
jkoendev / testCodeGeneration.m
Created February 19, 2019 08:51
Test casadi code generation
% syms x
%
% f = x;
%
% for i=1:100
% f = sin(f)*cos(f);
% end
% diff(f,x)
@jkoendev
jkoendev / testExternal.m
Created February 19, 2019 08:50
Test code generation external functionality of casadi
x = casadi.SX.sym('x',2);
f = x;
for i=1:10
f = sin(f)*cos(f)';
end
df = jacobian(f,x);
fun = casadi.Function('fun',{x},{f});
jacfun = casadi.Function('jac_fun',{x},{df});
@jkoendev
jkoendev / testIpoptInterface.m
Last active November 4, 2024 03:32
Generate sparsity pattern and interface for ipopt from casadi symbolics
function testIpoptInterface
x = casadi.MX.sym('x',2);
f = x(1)*x(1) + x(2)*x(2);
g = x(1)+x(2)-10;
% https://groups.google.com/forum/#!searchin/casadi-users/generate|sort:relevance/casadi-users/pxhU1tgkaEI/svmrRIzbFwAJ
fGrad = gradient(f,x);
gJac = jacobian(g,x);
lam = casadi.MX.sym('lam', g.sparsity());