# Unitful + DiffEq + different physical quantities + derivatives?

**URL:** <https://discourse.julialang.org/t/unitful-diffeq-different-physical-quantities-derivatives/62635>\
**Category:** General Usage\
**Tags:** question, package, diffeq, performance\
**Created:** [June 9, 2021, 5:17pm UTC](https://discourse.julialang.org/t/unitful-diffeq-different-physical-quantities-derivatives/62635 "2021-06-09T17:17:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [June 9, 2021, 5:17pm UTC](https://discourse.julialang.org/t/unitful-diffeq-different-physical-quantities-derivatives/62635/1 "2021-06-09T17:17:23Z")

</div>

I’m working on model using Differential Equation that has two state variables, current and temperature. So when using `DifferentialEquations.jl` I define something like

`f(du, u, p, t) = ...`

`u` is a vector with two elements, each of which I’d like to have different units and therefore different types. And du has yet different units from `u` since we add `1/s` when we take the time derivative.

Any advice or packages on how to deal with this in a type stable way? For now I strip all the units off and pass floats to `f`, but I’d rather propagate the units all the way through if possible.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [June 9, 2021, 5:48pm UTC](https://discourse.julialang.org/t/unitful-diffeq-different-physical-quantities-derivatives/62635/2 "2021-06-09T17:48:30Z")

</div>

[RecursiveArrayTools.jl](https://github.com/SciML/RecursiveArrayTools.jl) has an `ArrayPartition` which usually works for this. For Example:

```julia
using Unitful, DifferentialEquations, RecursiveArrayTools, Plots, UnitfulRecipes

function f!(du, u, p, t)
    du[1] = p[1] * sin(2π * t * p[2])
    du[2] = u[1]
end

function setupProblem(amplitude, frequency)
    u0 = ArrayPartition([0.0u"m/s"], [0.0u"m"])
    p = [amplitude, frequency]
    problem = ODEProblem(f!, u0, (0.0u"s", 10.0u"s"), p)
end

prob = setupProblem(0.1u"m/s^2", 1.0u"Hz")
soln = solve(prob)
plot(soln, vars=(0, 2))

```
