Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
[deps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"

[compat]
# TODO: update when https://github.com/MakieOrg/Makie.jl/pull/5137 merged
CairoMakie = "0.15.3"
37 changes: 22 additions & 15 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,72 +40,78 @@ Also, using `DynamicQuantities.Constants`, we were able to obtain the (dimension
Let's solve a simple projectile motion problem.
First load the `DynamicQuantities` module:

```julia
```@example projectile
using DynamicQuantities
```

Set up initial conditions as quantities:

```julia
```@example projectile
# Can explicitly import units:
using DynamicQuantities: km, m, s, min

y0 = 10km
v0 = 250m/s
θ = deg2rad(60)
g = 9.81m/s^2
nothing # hide
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use ; instead of a new line with this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, it got buried in the previous PR, but I think I was running into this when trying to do that. It's been a while though, so I'll go back and try it again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yea, still looks like no dice with @example blocks + semicolon suppression:

```@example projectile
# Can explicitly import units:
using DynamicQuantities: km, m, s, min

y0 = 10km
v0 = 250m/s
θ = deg2rad(60)
g = 9.81m/s^2;
```
image

It looks like this may be a design decision

On the flip side, we could use a @repl block if you like and output all the things equally:

```@repl projectile
# Can explicitly import units:
using DynamicQuantities: km, m, s, min

y0 = 10km
v0 = 250m/s
θ = deg2rad(60)
g = 9.81m/s^2
```
image

For completeness, the semicolon also works here, but it's probably not what we want in this case:

```@repl projectile
# Can explicitly import units:
using DynamicQuantities: km, m, s, min

y0 = 10km
v0 = 250m/s
θ = deg2rad(60)
g = 9.81m/s^2;
```
image

```

Next, we use trig functions to calculate x and y components of initial velocity.
`vx0` is the x component and
`vy0` is the y component:

```julia
```@example projectile
vx0 = v0 * cos(θ)
vy0 = v0 * sin(θ)
nothing # hide
```

Next, let's create a time vector from 0 seconds to 1.3 minutes.
Note that these are the same dimension (time), so it's fine to treat
them as dimensionally equivalent!

```julia
```@example projectile
t = range(0s, 1.3min, length=100)
nothing # hide
```

Next, use kinematic equations to calculate x and y as a function of time.
`x(t)` is the x position at time t, and
`y(t)` is the y position:

```julia
```@example projectile
x(t) = vx0*t
y(t) = vy0*t - 0.5*g*t^2 + y0
nothing # hide
```

These are functions, so let's evaluate them:

```julia
```@example projectile
x_si = x.(t)
y_si = y.(t)
nothing # hide
```

These are regular vectors of quantities
with `Dimensions` for physical dimensions.

Next, let's plot the trajectory.
First convert to km and strip units:
Next, let's plot the trajectory. We will use [Makie.jl](https://docs.makie.org/) for its nice unit support:

```julia
x_km = ustrip.(x_si .|> us"km")
y_km = ustrip.(y_si .|> us"km")
```
```@example projectile
using CairoMakie

Now, we plot:
# Convert to kilometers first
x_km = x_si .|> us"km"
y_km = y_si .|> us"km"

```julia
plot(x_km, y_km, label="Trajectory", xlabel="x [km]", ylabel="y [km]")
# Display
lines(x_km, y_km; label="Trajectory", axis=(xlabel="x [km]", ylabel="y [km]"))
```

See [Makie.jl > Dimension conversions](https://docs.makie.org/stable/explanations/dim-converts#Dimension-conversions) for more.

## 3. Using dimensional angles

Say that we wish to track angles as a unit, rather than assume
Expand Down Expand Up @@ -504,3 +510,4 @@ function my_func(x::UnionAbstractQuantity{T,D}) where {T,D}
return x / ustrip(x)
end
```

Loading