# Seven Lines of Julia (examples sought)

**URL:** <https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416>\
**Category:** General Usage\
**Created:** [November 19, 2020, 7:37am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416 "2020-11-19T07:37:29Z")\
**Posts on this page:** 5\
**Page:** 10

<div class="post-metadata">

**Author:** ![bradeneliason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bradeneliason/32/29031_2.png) [@bradeneliason](https://discourse.julialang.org/u/bradeneliason)\
**Post date:** [August 1, 2025, 2:37pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/197 "2025-08-01T14:37:36Z")

</div>

As a follow up to my previous post and a response to @RoyiAvital’s message to me, here’s a way to do [Proportional Navigation](https://en.wikipedia.org/wiki/Proportional_navigation).

```julia
using Plots, DifferentialEquations, LinearAlgebra

##
# Parameters
N = 5.0

# Target:
r⃗ₜ(t) = [cos(t); sin(t); 0] 
v⃗ₜ(t) = [-sin(t); cos(t); 0] # TODO: derive automatically?

#Define the problem
function propnav(a⃗ₘ, v⃗ₘ, r⃗ₘ, N, t)
    R⃗ = r⃗ₜ(t) - r⃗ₘ # displacement vector from missle to target
    v⃗ₚ = v⃗ₜ(t) - v⃗ₘ # relative velocity from missle to target
    Ω⃗ = (R⃗ × v⃗ₚ) ./ (R⃗⋅R⃗) # rotation vector

    a⃗ₘ .= -N * norm(v⃗ₚ) * cross((R⃗ / norm(R⃗)), Ω⃗)
end

#Initial Conditions
x₀ = [0.0, 0.0, 0.0]
dx₀ = [1.0, 0.0, 0.0]
tspan = (0.0, 1.0)

#Pass to solvers
prob = SecondOrderODEProblem(propnav, dx₀, x₀, tspan, N)
sol = solve(prob, Tsit5(), saveat=0.01)

#Plot
missledata = hcat(sol.u...)'
v⃗ₘx = missledata[:,1]
v⃗ₘy = missledata[:,2]
v⃗ₘz = missledata[:,3]
r⃗ₘx = missledata[:,4]
r⃗ₘy = missledata[:,5]
r⃗ₘz = missledata[:,6]

targetpath = hcat(r⃗ₜ.(sol.t)...)'
r⃗ₜx = targetpath[:,1]
r⃗ₜy = targetpath[:,2]

plot( r⃗ₜx, r⃗ₜy, lw = 5.0 .* sol.t, label="Target")
plot!(r⃗ₘx, r⃗ₘy, lw = 5.0 .* sol.t, label="Missile")

##
@userplot PursuitPlot
@recipe function f(cp::PursuitPlot)
    t, x, y, i = cp.args
    n = length(x)
    linewidth --> range(0.75, 10, length = i)
    aspect_ratio --> 1
    x[1:i], y[1:i]
end

n = length(sol.t)

anim = @animate for i ∈ 2:n
    pursuitplot(sol.t, r⃗ₜx, r⃗ₜy, i, label="Target")
    pursuitplot!(sol.t, r⃗ₘx, r⃗ₘy, i, label="Missile")
    plot!(leg=:topleft)
end
gif(anim, "test.gif", fps = 15)

```

I’m sure there’s a more elegant way to do this but it’s at least a start. I noticed some solvers struggle with instability at the time where the missile and target would collide which makes sense.  
 ![proportional_navigation_animation](https://global.discourse-cdn.com/julialang/original/3X/1/c/1c434ee77736e2f657535df032ed9e23a5ed9de1.gif)

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [October 20, 2025, 2:55pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/198 "2025-10-20T14:55:53Z")

</div>

Thanks for this wonderful example!

I wonder: does the constant k refer to the spring constant here? See e.g. [Harmonic\_oscillator\_in\_a\_fluid](https://en.wikipedia.org/wiki/Langevin_equation#Harmonic_oscillator_in_a_fluid) ?

Should in the update of the velocity v the term `f * Delta t` be replaced by `k * f * Delta t`?

Many thanks again?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 20, 2025, 3:17pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/199 "2025-10-20T15:17:38Z")

</div>

No, that’s the Boltzmann constant (in the example just any number). It is used there in the random perturbation introduced in the velocity for the [Langevin thermostat](https://en.wikipedia.org/wiki/Langevin_dynamics).

---

<div class="post-metadata">

**Author:** ![ziolai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziolai/32/23422_2.png) [@ziolai](https://discourse.julialang.org/u/ziolai)\
**Post date:** [October 20, 2025, 3:48pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/200 "2025-10-20T15:48:31Z")

</div>

Aha!

Is the code thus assuming the spring constant to be equal to one?

Thx again!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 20, 2025, 3:57pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/201 "2025-10-20T15:57:51Z")

</div>

Yes, it is.

[Previous page](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416.md?page=9)
