Skip to content

Instantly share code, notes, and snippets.

View jaeandersson's full-sized avatar

Joel Andersson jaeandersson

  • Madison, Wisconsin
View GitHub Profile
@jaeandersson
jaeandersson / dms_gn.m
Created October 5, 2017 22:17
Direct multiple shooting and Gauss-Newton
classdef dms_gn < handle
properties
% Symbolic representation of the OCP
ocp
% OCP data
data
% Solver options
opts
% Verbose output
verbose
@jaeandersson
jaeandersson / codegen.py
Created June 28, 2017 16:25
codegen with derivatives
from casadi import *
# Simple function:
x = SX.sym('x',2)
y = SX.sym('f',2)
F = Function('F', [x,y],[sin(x)*cos(y), dot(x,y)], ['x','y'], ['f','g'])
# Generate code, including derivatives
CG = CodeGenerator('F')
CG.add(F)
@jaeandersson
jaeandersson / rocket_intermediates.py
Created June 1, 2017 22:21
Modified rocket example, with local variable
# Modified version of the rocket example in Section 7.2 of the CasADi v3.2 User Guide
# Cf. http://guide.casadi.org/
from casadi import *
dae = DaeBuilder()
# Add input expressions
a = dae.add_p('a')
b = dae.add_p('b')
u = dae.add_u('u')
h = dae.add_x('h')
v = dae.add_x('v')
@jaeandersson
jaeandersson / import.m
Created June 21, 2016 00:15
Import package functionality for Octave
function varargout = import(varargin)
% Import elements from a package
%
% Cache of aliase
persistent imported;
% Make sure cell array
if ~iscell(imported)
imported = cell(0, 1);
@jaeandersson
jaeandersson / import.m
Last active March 6, 2024 16:34
Import a package into Octave using the syntax "import packagename.*"
function import(varargin)
% Import a package. Only entire packages can be imported currently.
error(nargchk(1, inf, nargin, 'struct'));
% Import the packages one-by-one
for i=1:nargin
import1(varargin{i});
end
end
function import1(pkgname)
# Pendulum collocation
# w position of cart
# w1,y1 position of pendulum
import numpy as NP
from casadi import *
from casadi.tools import *
import matplotlib.pyplot as plt
@jaeandersson
jaeandersson / Function.m
Created June 16, 2016 13:37
Autogenerated MATLAB code for "casadi::Function"
classdef Function < casadi.SharedObject
%General function.
%
%A general function $f$ in casadi can be multi-input, multi-output. Number of
%inputs: nin n_in() Number of outputs: nout n_out() We can view this
%function as a being composed of a ( nin, nout) grid of single-input, single-
%output primitive functions. Each such primitive function $f_ {i, j}
%\\forall i \\in [0, nin-1], j \\in [0, nout-1]$ can map as $\\mathbf
%{R}^{n, m}\\to\\mathbf{R}^{p, q}$, in which n, m, p, q can take
%different values for every (i, j) pair. When passing input, you specify
from casadi import *
# 1D
grid = [[0, 1, 2]]
values = [0, 1, 2]
print "grid = ", grid
print "values = ", values
F = interpolant('F', 'linear', grid, values)
print 'F(2.4) = ', F(2.4)
@jaeandersson
jaeandersson / switch.py
Last active January 21, 2016 17:06
switch / if-else
from casadi import *
c = MX.sym('c')
x = MX.sym('x')
z1 = if_else(c, sin(x), cos(x))
z2 = conditional(c, [sin(x), cos(x)], exp(x))
@jaeandersson
jaeandersson / cov.py
Last active August 29, 2015 14:20 — forked from adbuerger/cov.py
import casadi as ca
import pylab as pl
# [...]
# A is of type SX, column vector, shape (788, 1), dense
# B is of type sx, column vector, shape (984, 1), dense
# V is of type struct_symSX, shape (1594, 1), dense
# W is a numpy array, matrix with shape (788, 788), almost diagonal