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 / cstr.py
Last active August 29, 2015 14:18
CSTR reactor
import numpy as np
import casadi as ca
from numpy import exp,pi
def ode():
# Plant parameter values
T0 = 350 # K
c0 = 1 # kmol/m^3
@jaeandersson
jaeandersson / solution2.py
Created October 16, 2014 07:24
Solution 2, EDF
from casadi import *
package CSTR
model CSTR "A CSTR"
parameter Modelica.SIunits.VolumeFlowRate F0=100/1000/60 "Inflow";
parameter Modelica.SIunits.Concentration c0=1000 "Concentration of inflow";
Modelica.Blocks.Interfaces.RealInput Tc "Cooling temperature";
parameter Modelica.SIunits.VolumeFlowRate F=100/1000/60 "Outflow";
parameter Modelica.SIunits.Temp_K T0 = 350;
parameter Modelica.SIunits.Length r = 0.219;
@jaeandersson
jaeandersson / nmpc_gn.py
Last active February 15, 2024 21:24
(Numerical Optimal Control, Trondheim): Exercise 10, NMPC
from casadi import *
from casadi.tools import *
from plotter import *
from pylab import *
"""
NOTE: if you use spyder,
make sure you open a Python interpreter
instead of an IPython interpreter
otherwise you wont see any plots
@jaeandersson
jaeandersson / solution_exercise8.py
Created September 24, 2014 09:40
Solution, Exercise 8 (first part)
import numpy as NP
from casadi import *
# Choose collocation points
d = 4 # Degree
tau_root = collocationPoints(d,"legendre")
# Calculate coefficients
C = DMatrix.nan(d+1,d+1) # For the collocation equation
D = DMatrix.nan(d+1) # For the continuity equation
@jaeandersson
jaeandersson / quadcopter.py
Last active August 29, 2015 14:06
Quadcopter model by Joris Gillis
from casadi import *
from casadi.tools import *
import numpy
from numpy import cos,sin, vstack, hstack, multiply
class Quadcopter:
"""
Quadcopter model
@jaeandersson
jaeandersson / solution_exercise4.py
Last active August 29, 2015 14:06
Solution, exercise 4
#import casadi
import numpy
from casadi import *
# Initial and final position
px0 = 0.0; py0 = 1.5
pxF = 20.0; pyF = 0.0
# Time horizon and discretization
T = 3.0
@jaeandersson
jaeandersson / direct_multiple_shooting.py
Created August 7, 2014 12:08
Direct multiple shooting with CasADi
from casadi import *
from numpy import *
import matplotlib.pyplot as plt
N = 20 # Control discretization
T = 10.0 # End time
# Declare variables (use scalar graph)
u = SX.sym("u") # control
x = SX.sym("x",2) # states
@jaeandersson
jaeandersson / direct_single_shooting.py
Created August 6, 2014 22:45
Direct single shooting with CasADi
from casadi import *
from numpy import *
import matplotlib.pyplot as plt
N = 20 # Control discretization
T = 10.0 # End time
# Declare variables (use scalar graph)
u = SX.sym("u") # control
x = SX.sym("x",2) # states
@jaeandersson
jaeandersson / e5_unfinished.py
Created August 6, 2014 02:20
Incomplete implementation of a dynamic programming for a 2-state system
from pylab import *
# End time
T = 10.
# Number of control intervals
N = 20
# Number of Runge-Kutta 4 steps per interval and step size
NK = 20