Skip to content

Instantly share code, notes, and snippets.

@jaeandersson
Created May 20, 2016 23:28
Show Gist options
  • Save jaeandersson/8a5cda2a7fef8464d85fe2b45dee4cdb to your computer and use it in GitHub Desktop.
Save jaeandersson/8a5cda2a7fef8464d85fe2b45dee4cdb to your computer and use it in GitHub Desktop.
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)
print 'F(1.4) = ', F(1.4)
print 'F(0.4) = ', F(0.4)
print 'F(-.6) = ', F(-.6)
# 2D
grid = [[0, 1, 2], [0, 1, 2]]
values = [0, 1, 2, 10, 11, 12, 20, 21, 22]
print "grid = ", grid
print "values = ", values
F = interpolant('F', 'linear', grid, values)
print 'F([2.4, 0.5]) = ', F([2.4, 0.5])
print 'F([1.4, 0.5]) = ', F([1.4, 0.5])
print 'F([0.4, 0.5]) = ', F([0.4, 0.5])
print 'F([-.6, 0.5]) = ', F([-.6, 0.5])
print 'F([-.6, 1.5]) = ', F([-.6, 1.5])
print 'F([-.6, 2.5]) = ', F([-.6, 2.5])
print 'F([-.6, 3.5]) = ', F([-.6, 3.5])
# Differentiate
gF = F.jacobian();
print 'gF([2.4, 0.5]) = ', gF([2.4, 0.5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment