You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: markdown/type_handling/03-unitful.md
+38-56Lines changed: 38 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,81 +12,71 @@ DifferentialEquations.jl allows for one to use Unitful.jl to have unit-checked a
12
12
13
13
To use Unitful, you need to have the package installed. Then you can add units to your variables. For example:
14
14
15
-
````julia
16
-
15
+
```julia
17
16
using Unitful
18
17
t =1.0u"s"
19
-
````
20
-
18
+
```
21
19
22
-
````
20
+
```
23
21
1.0 s
24
-
````
22
+
```
25
23
26
24
27
25
28
26
29
27
30
28
Notice that `t` is a variable with units in seconds. If we make another value with seconds, they can add
31
29
32
-
````julia
33
-
30
+
```julia
34
31
t2 =1.02u"s"
35
32
t+t2
36
-
````
37
-
33
+
```
38
34
39
-
````
35
+
```
40
36
2.02 s
41
-
````
37
+
```
42
38
43
39
44
40
45
41
46
42
47
43
and they can multiply:
48
44
49
-
````julia
50
-
45
+
```julia
51
46
t*t2
52
-
````
53
-
47
+
```
54
48
55
-
````
49
+
```
56
50
1.02 s^2
57
-
````
51
+
```
58
52
59
53
60
54
61
55
62
56
63
57
You can even do rational roots:
64
58
65
-
````julia
66
-
59
+
```julia
67
60
sqrt(t)
68
-
````
69
-
61
+
```
70
62
71
-
````
63
+
```
72
64
1.0 s^1/2
73
-
````
65
+
```
74
66
75
67
76
68
77
69
78
70
79
71
Many operations work. These operations will check to make sure units are correct, and will throw an error for incorrect operations:
80
72
81
-
````julia
82
-
73
+
```julia
83
74
t +sqrt(t)
84
-
````
85
-
75
+
```
86
76
87
-
````
77
+
```
88
78
Error: DimensionError: 1.0 s and 1.0 s^1/2 are not dimensionally compatible.
89
-
````
79
+
```
90
80
91
81
92
82
@@ -96,19 +86,17 @@ Error: DimensionError: 1.0 s and 1.0 s^1/2 are not dimensionally compatible.
96
86
97
87
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:
98
88
99
-
````julia
100
-
89
+
```julia
101
90
using DifferentialEquations
102
91
f = (y,p,t) ->0.5*y
103
92
u0 =1.5u"N"
104
93
prob =ODEProblem(f,u0,(0.0u"s",1.0u"s"))
105
94
sol =solve(prob,Tsit5())
106
-
````
107
-
95
+
```
108
96
109
-
````
97
+
```
110
98
Error: DimensionError: N s^-1 and 0.75 N are not dimensionally compatible.
111
-
````
99
+
```
112
100
113
101
114
102
@@ -120,15 +108,13 @@ $$\frac{dy}{dt} = f(t,y)$$
120
108
121
109
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:
122
110
123
-
````julia
124
-
111
+
```julia
125
112
f = (y,p,t) ->0.5*y/3.0u"s"
126
113
prob =ODEProblem(f,u0,(0.0u"s",1.0u"s"))
127
114
sol =solve(prob,Tsit5())
128
-
````
129
-
115
+
```
130
116
131
-
````
117
+
```
132
118
retcode: Success
133
119
Interpolation: specialized 4th order "free" interpolation
0 commit comments