Skip to content

Commit fb4f352

Browse files
Merge pull request #345 from SciML/rebuild/5a227e44
Rebuild type_handling/03-unitful.jmd
2 parents 943ac4e + 69ddb64 commit fb4f352

File tree

4 files changed

+44
-62
lines changed

4 files changed

+44
-62
lines changed

html/type_handling/03-unitful.html

Lines changed: 6 additions & 6 deletions
Large diffs are not rendered by default.

markdown/type_handling/03-unitful.md

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,81 +12,71 @@ DifferentialEquations.jl allows for one to use Unitful.jl to have unit-checked a
1212

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

15-
````julia
16-
15+
```julia
1716
using Unitful
1817
t = 1.0u"s"
19-
````
20-
18+
```
2119

22-
````
20+
```
2321
1.0 s
24-
````
22+
```
2523

2624

2725

2826

2927

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

32-
````julia
33-
30+
```julia
3431
t2 = 1.02u"s"
3532
t+t2
36-
````
37-
33+
```
3834

39-
````
35+
```
4036
2.02 s
41-
````
37+
```
4238

4339

4440

4541

4642

4743
and they can multiply:
4844

49-
````julia
50-
45+
```julia
5146
t*t2
52-
````
53-
47+
```
5448

55-
````
49+
```
5650
1.02 s^2
57-
````
51+
```
5852

5953

6054

6155

6256

6357
You can even do rational roots:
6458

65-
````julia
66-
59+
```julia
6760
sqrt(t)
68-
````
69-
61+
```
7062

71-
````
63+
```
7264
1.0 s^1/2
73-
````
65+
```
7466

7567

7668

7769

7870

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

81-
````julia
82-
73+
```julia
8374
t + sqrt(t)
84-
````
85-
75+
```
8676

87-
````
77+
```
8878
Error: DimensionError: 1.0 s and 1.0 s^1/2 are not dimensionally compatible.
89-
````
79+
```
9080

9181

9282

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

9787
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:
9888

99-
````julia
100-
89+
```julia
10190
using DifferentialEquations
10291
f = (y,p,t) -> 0.5*y
10392
u0 = 1.5u"N"
10493
prob = ODEProblem(f,u0,(0.0u"s",1.0u"s"))
10594
sol = solve(prob,Tsit5())
106-
````
107-
95+
```
10896

109-
````
97+
```
11098
Error: DimensionError: N s^-1 and 0.75 N are not dimensionally compatible.
111-
````
99+
```
112100

113101

114102

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

121109
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:
122110

123-
````julia
124-
111+
```julia
125112
f = (y,p,t) -> 0.5*y/3.0u"s"
126113
prob = ODEProblem(f,u0,(0.0u"s",1.0u"s"))
127114
sol = solve(prob,Tsit5())
128-
````
129-
115+
```
130116

131-
````
117+
```
132118
retcode: Success
133119
Interpolation: specialized 4th order "free" interpolation
134120
t: 3-element Array{Unitful.Quantity{Float64,𝐓,Unitful.FreeUnits{(s,),𝐓,nothing}},1}:
@@ -139,37 +125,33 @@ u: 3-element Array{Unitful.Quantity{Float64,𝐋 𝐌 𝐓^-2,Unitful.FreeUnits{
139125
1.5 N
140126
1.5362091208988309 N
141127
1.7720406194871123 N
142-
````
128+
```
143129

144130

145131

146132

147133

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

150-
````julia
151-
136+
```julia
152137
print(sol[:])
153-
````
154-
138+
```
155139

156-
````
140+
```
157141
Unitful.Quantity{Float64,𝐋 𝐌 𝐓^-2,Unitful.FreeUnits{(N,),𝐋 𝐌 𝐓^-2,nothing}}[1.5 N, 1.5362091208988309 N, 1.7720406194871123 N]
158-
````
142+
```
159143

160144

161145

162146

163147

164148
We can plot the solution by removing the units:
165149

166-
````julia
167-
150+
```julia
168151
using Plots
169152
gr()
170153
plot(ustrip(sol.t),ustrip(sol[:]),lw=3)
171-
````
172-
154+
```
173155

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

@@ -212,8 +194,8 @@ Status `/builds/JuliaGPU/DiffEqTutorials.jl/tutorials/type_handling/Project.toml
212194
[abce61dc-4473-55a0-ba07-351d65e31d42] Decimals 0.4.1
213195
[0c46a032-eb83-5123-abaf-570d42b7fbaa] DifferentialEquations 6.15.0
214196
[497a8b3b-efae-58df-a0af-a86822472b78] DoubleFloats 1.1.13
215-
[eff96d63-e80a-5855-80a2-b1b0885c5ab7] Measurements 2.2.1
216-
[1dea7af3-3e70-54e6-95c3-0bf5283fa5ed] OrdinaryDiffEq 5.42.5
217-
[91a5bcdd-55d7-5caf-9e0b-520d859cae80] Plots 1.6.0
218-
[1986cc42-f94f-5a68-af5c-568840ba703d] Unitful 1.4.0
197+
[eff96d63-e80a-5855-80a2-b1b0885c5ab7] Measurements 2.3.0
198+
[1dea7af3-3e70-54e6-95c3-0bf5283fa5ed] OrdinaryDiffEq 5.43.0
199+
[91a5bcdd-55d7-5caf-9e0b-520d859cae80] Plots 1.6.12
200+
[1986cc42-f94f-5a68-af5c-568840ba703d] Unitful 1.5.0
219201
```
-89 Bytes
Loading

pdf/type_handling/03-unitful.pdf

74 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)