Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions html/type_handling/03-unitful.html

Large diffs are not rendered by default.

94 changes: 38 additions & 56 deletions markdown/type_handling/03-unitful.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,81 +12,71 @@ DifferentialEquations.jl allows for one to use Unitful.jl to have unit-checked a

To use Unitful, you need to have the package installed. Then you can add units to your variables. For example:

````julia

```julia
using Unitful
t = 1.0u"s"
````

```

````
```
1.0 s
````
```





Notice that `t` is a variable with units in seconds. If we make another value with seconds, they can add

````julia

```julia
t2 = 1.02u"s"
t+t2
````

```

````
```
2.02 s
````
```





and they can multiply:

````julia

```julia
t*t2
````

```

````
```
1.02 s^2
````
```





You can even do rational roots:

````julia

```julia
sqrt(t)
````

```

````
```
1.0 s^1/2
````
```





Many operations work. These operations will check to make sure units are correct, and will throw an error for incorrect operations:

````julia

```julia
t + sqrt(t)
````

```

````
```
Error: DimensionError: 1.0 s and 1.0 s^1/2 are not dimensionally compatible.
````
```



Expand All @@ -96,19 +86,17 @@ Error: DimensionError: 1.0 s and 1.0 s^1/2 are not dimensionally compatible.

Just like with other number systems, you can choose the units for your numbers by simply specifying the units of the initial condition and the timestep. For example, to solve the linear ODE where the variable has units of Newton's and `t` is in Seconds, we would use:

````julia

```julia
using DifferentialEquations
f = (y,p,t) -> 0.5*y
u0 = 1.5u"N"
prob = ODEProblem(f,u0,(0.0u"s",1.0u"s"))
sol = solve(prob,Tsit5())
````

```

````
```
Error: DimensionError: N s^-1 and 0.75 N are not dimensionally compatible.
````
```



Expand All @@ -120,15 +108,13 @@ $$\frac{dy}{dt} = f(t,y)$$

we must have that `f` is a rate, i.e. `f` is a change in `y` per unit time. So we need to fix the units of `f` in our example to be `N/s`. Notice that we then do not receive an error if we do the following:

````julia

```julia
f = (y,p,t) -> 0.5*y/3.0u"s"
prob = ODEProblem(f,u0,(0.0u"s",1.0u"s"))
sol = solve(prob,Tsit5())
````

```

````
```
retcode: Success
Interpolation: specialized 4th order "free" interpolation
t: 3-element Array{Unitful.Quantity{Float64,𝐓,Unitful.FreeUnits{(s,),𝐓,nothing}},1}:
Expand All @@ -139,37 +125,33 @@ u: 3-element Array{Unitful.Quantity{Float64,𝐋 𝐌 𝐓^-2,Unitful.FreeUnits{
1.5 N
1.5362091208988309 N
1.7720406194871123 N
````
```





This gives a a normal solution object. Notice that the values are all with the correct units:

````julia

```julia
print(sol[:])
````

```

````
```
Unitful.Quantity{Float64,𝐋 𝐌 𝐓^-2,Unitful.FreeUnits{(N,),𝐋 𝐌 𝐓^-2,nothing}}[1.5 N, 1.5362091208988309 N, 1.7720406194871123 N]
````
```





We can plot the solution by removing the units:

````julia

```julia
using Plots
gr()
plot(ustrip(sol.t),ustrip(sol[:]),lw=3)
````

```

![](figures/03-unitful_9_1.png)

Expand Down Expand Up @@ -212,8 +194,8 @@ Status `/builds/JuliaGPU/DiffEqTutorials.jl/tutorials/type_handling/Project.toml
[abce61dc-4473-55a0-ba07-351d65e31d42] Decimals 0.4.1
[0c46a032-eb83-5123-abaf-570d42b7fbaa] DifferentialEquations 6.15.0
[497a8b3b-efae-58df-a0af-a86822472b78] DoubleFloats 1.1.13
[eff96d63-e80a-5855-80a2-b1b0885c5ab7] Measurements 2.2.1
[1dea7af3-3e70-54e6-95c3-0bf5283fa5ed] OrdinaryDiffEq 5.42.5
[91a5bcdd-55d7-5caf-9e0b-520d859cae80] Plots 1.6.0
[1986cc42-f94f-5a68-af5c-568840ba703d] Unitful 1.4.0
[eff96d63-e80a-5855-80a2-b1b0885c5ab7] Measurements 2.3.0
[1dea7af3-3e70-54e6-95c3-0bf5283fa5ed] OrdinaryDiffEq 5.43.0
[91a5bcdd-55d7-5caf-9e0b-520d859cae80] Plots 1.6.12
[1986cc42-f94f-5a68-af5c-568840ba703d] Unitful 1.5.0
```
Binary file modified markdown/type_handling/figures/03-unitful_9_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pdf/type_handling/03-unitful.pdf
Binary file not shown.