Published
Edited
Sep 10, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function initializeState() {
// position(t+1) = position(t) + velocity(t) * dt
// velocity(t+1) = velocity(t)
let dt = .01;
let accelvel = math.matrix([[1, dt], [0, 1]]);
let state = {'P': 1,
'Q': math.diag(math.matrix([1, 0, 1, 0])),
'R': math.evaluate("penalty * identity(2)", {penalty: penalty}),
'A': math.concat(math.concat(accelvel, math.zeros(2, 2), 1),
math.concat(math.zeros(2, 2), accelvel, 1), 0
),
'B': math.matrix([[0, 0], [1, 0], [0, 0], [0, 1]]),
'x': math.matrix([200, 0, 200, 0]),
'u': math.matrix([0, 0]),
'goal': 100};
state.P = math.identity(state.Q.size()[0]);
for(var i = 0; i < 50; i++) {
// Use a fixed point iteration to find out the optimal P.
// Page 3.6 in the lecture.
state.P = math.evaluate("Q + (A' * P * A) - (A' * P * B * inv(R + B' * P * B) * B' * P * A)", state);
}
state.K = math.evaluate("-inv(R + B' * P * B) * B' * P * A", state);
return state
}
Insert cell
function advanceState(state, goal) {
// Use the optimal filter to advance the state
state.g = goal;
state.u = math.evaluate("K * (x - g)", state);
var a = [];
for(var i = 0; i < state.x.size()[0]; i++) {
a.push(noise * rnorm());
}
state.w = math.matrix(a);
state.x = math.evaluate("A * x + B * u + w", state);
return state
}
Insert cell
md`# Imports`
Insert cell
math = require("mathjs@7")
Insert cell
random = require('d3-random@1.1.0')
Insert cell
rnorm = random.randomNormal()
Insert cell
cat = FileAttachment("cat-astronaut.png")
Insert cell
catImg = html`
${await cat.image()}
`
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more