# How to read a system of equations?

**URL:** https://discourse.julialang.org/t/how-to-read-a-system-of-equations/10207
**Category:** Modelling & Simulations
**Tags:** question
**Created:** [April 6, 2018, 9:33pm UTC](https://discourse.julialang.org/t/how-to-read-a-system-of-equations/10207 "2018-04-06T21:33:46Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [April 7, 2018, 3:17am UTC](https://discourse.julialang.org/t/how-to-read-a-system-of-equations/10207/3 "2018-04-07T03:17:41Z")

</div>

One way to define the dynamics is as follows:

```julia
const c1 = 1.0
const c2 = 2.0
const c3 = 3.0
const c4 = 4.0
const c5 = 5.0
const c6 = 6.0

function dynamics(Y)
    x, y = Y
    [c1 + c2 * x + c3 * y,
     c4 + c5 * x + c6 * y]
end

```

So that you can evaluate the dynamics as:

```julia
julia> Y = [2.0, 3.0];

julia> dynamics(Y)
2-element Array{Float64,1}:
 14.0
 32.0

```

Of course you can find the constant offset by simply evaluating the function at Y = 0:

```julia
julia> A = dynamics(zeros(2))
2-element Array{Float64,1}:
 1.0
 4.0

```

Since this system is linear, B = \frac{\partial Y(t)}{\partial Y(t - 1)}. So you could use (automatic) differentiation to extract B. One option is to use [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl) (install using `Pkg.add("ForwardDiff")`) to extract `B`:

```julia
julia> using ForwardDiff

julia> B = ForwardDiff.jacobian(dynamics, Y)
2×2 Array{Float64,2}:
 2.0 3.0
 5.0 6.0

```

Here, you’re essentially implicitly telling ForwardDiff that `Y` contains the variables by having `Y` be the argument of the `dynamics` function while the `c`’s are global constants. Note that [`ForwardDiff.jacobian`](http://www.juliadiff.org/ForwardDiff.jl/stable/user/api.html#ForwardDiff.jacobian) expects as function that maps a vector to a vector. If you don’t want the `c`’s to be global constants, you can use a ‘closure’:

```julia
function dynamics2(Y, C)
    x, y = Y
    [C[1] + C[2] * x + C[3] * y,
     C[4] + C[5] * x + C[6] * y]
end

A = ForwardDiff.jacobian(Y -> dynamics2(Y, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]), Y)

```

By the way, Discourse allows you to write math equations in LaTeX form using e.g. `$x = \frac{y}{z}$`, and see [this post](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) for how to get nice code formatting.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-read-a-system-of-equations/10207)._
