Archive for category Uncategorized
Having fun with Ordinary Differential Equations in Clojure using com.konato.ode part 2
Posted by stephaner in Uncategorized on July 17th, 2009
Introduction
In the first tutorial, we introduced the com.konato.ode library to simulate differential equations with a classical damped spring mechanical system. We used as a source the system of differential equations. In this second tutorial, we will show how to use the functions transfer2ode and state2ode to convert the same system presented on the form of transfert function and state space into systems of differential equations and the simulate them using the odeslolve function.
Converting Transfert functions
Let say, we want to convert a simple integrator with a fixed input of 1. The transfer function of an integrator is 1/s. The function transfer2ode require three paramaters, the numerator, the denominator and the input function. The numerator should be a least one degree less than the denominator.
user> (def fps (transfer2ode [1] [1 0] [1]))
#'user/fps
user> fps
{:dotfps {:x1 #<ode$tf2odedotlast__2126$fn__2131 com.konato.ode$tf2odedotlast__2126$fn__2131@4d40df>}, :outfps {:y1 #<ode$tf2odeout__2157$fn__2159 com.konato.ode$tf2odeout__2157$fn__2159@1de041e>}}
fps is an hashmap who contains differential equation and the output equation. We can now use them to simulate our system for a period t of 2 seconds using odesolve function.
user> (keepdecim (last (:y1 (odesolve rk4 (:dotfps fps) {:x1 0.0} :t 0 2 0.01 (:outfps fps)))) 5)
2.0
This is the result of the integration of 1 over a period t 2 second as expected.
Now, we will use the transfert function the classical damped spring mechanical system used in part 1:
We will create a function who can simulate the system to allow validation with the results obtained in part 1:
(defn transferdampedho [m fv k xini xdotini T0 TF DT]
"Test the Runge-Kutto order 4 method and the transfer2ode for a classic damped harmonic oscilator."
(let [
DEN [1 (/ fv m) (/ k m)]
NUM [0 1]
inp [0]
fps (transfer2ode NUM DEN inp)
]
(odesolve rk4 (:dotfps fps) {:x2 0.0, :x1 0.9} :t T0 TF DT (:outfps fps))
))
Then we can study the error between the classical versus the transfer function conversion:
user> (= 0 (reduce + (map #(- %1 %2) (:x (dampedho 1 1 1 1.0 0.0 0.0 2.0 0.01)) (:y1 (transferdampedho 1 1 1 1.0 0.0 0.0 2.0 0.01))))) true
The result are the same as expected.
Converting State Space
If we have a State Space representation of a system, we can use the state2ode function to convert it into differentials and outputs equation who then can be used by odesolve. Let say, we have our classical damped spring oscillator in the form of a State Space:
We will create a function who can simulate the system to allow validation with the results obtained in part 1:
(defn statedampedho [m fv k xini xdotini T0 TF DT]
"Test the Runge-Kutto order 4 method and the state2ode for a classic damped harmonic oscilator."
(let [
A [[0 1][(* -1 (/ k m)) (* -1 (/ fv m))]]
B [[0][1]]
C [[1 0]]
D [[0]]
U [0]
fps (state2ode A B C D U)
]
(odesolve rk4 (:dotfps fps) {:x2 0.0, :x1 0.9} :t T0 TF DT (:outfps fps))
))
Then we can study the error between the classical versus the transfer function conversion:
user> (= 0 (reduce +(map #(- %1 %2) (:x (dampedho 1 1 1 1.0 0.0 0.0 2.0 0.01)) (:y1 (statedampedho 1 1 1 1.0 0.0 0.0 2.0 0.01))))) true
The result are the same as expected.
Conclusion
In this second part, we studied usage of the conversion function included in the com.konato.ode library. Conversion of Transfer functions and State Space were demonstrated. Subsequent tutorials will show more possibilities of the library.

