Skip to content

Instantly share code, notes, and snippets.

@crabmusket
Last active December 17, 2015 18:59
Show Gist options
  • Save crabmusket/5657087 to your computer and use it in GitHub Desktop.
Save crabmusket/5657087 to your computer and use it in GitHub Desktop.
Controllability of a system described with a state equation.
module Main where
import Control.Monad (liftM)
import Numeric.LinearAlgebra (readMatrix, multiply, rank, cols, rows, fromBlocks)
main = do
putStrLn "Enter matrix A (in format \"1 2 3; 4 5 6; 7 8 9\")"
a <- getMatrix
putStrLn "Enter matrix B (in format \"1; 2; 3\")"
b <- getMatrix
let cm = controllability a b
c = isControllable cm
putStr "Controllability matrix: "
print cm
putStr "Is system controllable: "
print (if c then "yes" else "no")
-- For controllability, every row must be linearly independent.
isControllable cm = rank cm == rows cm
-- Controllability matrix. See:
-- http://en.wikipedia.org/wiki/Controllability
controllability a b = let
cm' = iterate (multiply a) b -- Infinite list [b ab aab ...]
cm = take (cols a) cm' -- Finite list of length (cols a)
in fromBlocks [cm] -- Glue columns into square matrix
-- Convenience: read matrix using ';' instead of '\n' to separate rows.
getMatrix = liftM lineToMat getLine
where lineToMat = readMatrix . map translate
translate c = if c == ';' then '\n' else c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment