# \[show and tell\] Blog post on interval arithmetic in Pluto.jl

**URL:** <https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070>\
**Category:** Community\
**Tags:** blog-post, pluto\
**Created:** [July 21, 2021, 8:53pm UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070 "2021-07-21T20:53:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuel\_Ainsworth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_ainsworth/32/15644_2.png) [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Post date:** [July 21, 2021, 8:53pm UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070/1 "2021-07-21T20:53:18Z")

</div>

I just wrote a [blog post](https://samlikes.pizza/pluto_interval_blog.jl.html) entirely in Julia and Pluto.jl! It’s the story of how I flew too close to the sun implementing my own interval arithmetic library, and lessons learned along the way. It’s got some math, some twists, love interests, action. A blog post for the whole family.

This is the first blog post I’ve ever published since my prehistoric wordpress days, and I have to say Pluto.jl was a dream to work with! It’s so good that it’s making me start to consider Julia for projects that I never would’ve otherwise done in Julia or in notebooks for that matter. It doesn’t have any of the same is-this-cell-stale anxiety that I had with Jupyter. Shout out to @fonsp and team!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 21, 2021, 9:13pm UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070/2 "2021-07-21T21:13:26Z")

</div>

Nice. Very clean. How did you host it? Is this more than just slapping the generated HTML onto a static site?

---

<div class="post-metadata">

**Author:** ![Samuel\_Ainsworth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_ainsworth/32/15644_2.png) [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Post date:** [July 21, 2021, 9:37pm UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070/3 "2021-07-21T21:37:56Z")

</div>

Thanks! I just exported to HTML and threw it onto a static site! I don’t know if this is the best solution, but it works for now…

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 21, 2021, 9:45pm UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070/4 "2021-07-21T21:45:47Z")

</div>

It looks great. I guess I’m just amazed that it’s that easy to make that good looking of a site with Pluto. It makes it look like you did a ton of work, putting a working Binder integration button on the top and everything 😅

---

<div class="post-metadata">

**Author:** ![mforets](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mforets/32/298_2.png) [@mforets](https://discourse.julialang.org/u/mforets)\
**Post date:** [July 22, 2021, 12:05am UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070/5 "2021-07-22T00:05:23Z")

</div>

Nice post!

Let me share another example to further illustrate your statement that:

> Practically speaking it means that although interval arithmetic generally works very well without supervision, you may want to write hand-crafted interval arithmetic functions for certain operations or consider different possible rewrites of your computation

This small example mixes intervals, the sine function… and differential equations, for the whole Julia :juliabouncer: familly. (Credits to @AnderGray for this nice example).

The function x(t) = x\_0 \exp\left(\cos(t) - 1\right), x\_0 \in \mathbb{R}, t \geq 0 solves the differential equation x'(t) = -x(t) ~ \sin(t).

Standard integration schemes fail to produce helpful solutions if the initial state is an interval, for reasons explained in your post, eg. dependency problems. This is easy to check:

```julia
using DifferentialEquations, IntervalArithmetic

# initial condition
x₀ = [-1 .. 1]

# define the problem
function f(dx, x, p, t)
    dx[1] = -x[1] * sin(t)
end

# pass to solvers
prob = ODEProblem(f, x₀, (0.0, 2.0))
sol = solve(prob, Tsit5(), adaptive=false, dt=0.05, reltol=1e-6);

# there is no plot recipe readily available so we create a plot with the help of LazySets.jl
using LazySets, Plots
using LazySets: Interval

out = [Interval(sol.t[i]) × Interval(sol.u[i][1]) for i in 1:20]

plot(out, vars=(0, 1), xlab="t", ylab="x(t)", lw=3.0, alpha=1., c=:black, marker=:none, title="Standard integrator with interval initial condition")

```

 ![Screenshot from 2021-07-21 21-02-55](https://global.discourse-cdn.com/julialang/original/3X/a/a/aafe3bd5ceb66cf0f412b184322039e3370f8f25.png)

See that the solution explodes already for 1 second. On the other hand, specialized algorithms can handle this case without noticeable wrapping effect, producing a sequence of sets whose union covers the true solution for all initial points.

```julia
using ReachabilityAnalysis, Plots

# define the model
@taylorize function f(dx, x, p, t)
    dx[1] = -x[1] * sin(t)
end

# define the set of initial states; in this case, an interval
X0 = -1 .. 1

# define the initial-value problem
prob = @ivp(x' = f(x), x(0) ∈ X0, dim=1);

# solve it
sol = solve(prob, alg=TMJets21a(abstol=1e-10), T=15);

# it is illustrative to plot the computed flowpipe and the known analytic solution
# for a range of initial conditions that cover the range of `U0`.
analytic_sol(x0) = t -> x0 * exp(cos(t) - 1.0)

dt = range(0, 15, length=100)
x0vals = range(-1, 1, length=25)

fig = plot()
plot!(fig, sol, vars=(0, 1), lw=0.0, title="Specialized (Taylor-model based) integrator")
[plot!(fig, dt, analytic_sol(x0).(dt), lab="", c=:magenta, lw=2.0, xlab="t", ylab="x(t)") for x0 in x0vals]; #!jl
fig

```

 ![Screenshot from 2021-07-21 21-02-39](https://global.discourse-cdn.com/julialang/original/3X/d/d/dd2b9070313896fb3c2d472e6362776105d3cde2.png)

We will discuss this and other examples at our JuliaCon workshop “It’s all set: a hands-on introduction to JuliaReach” next monday :julia:

---

<div class="post-metadata">

**Author:** ![Samuel\_Ainsworth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_ainsworth/32/15644_2.png) [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Post date:** [July 22, 2021, 12:29am UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070/6 "2021-07-22T00:29:06Z")

</div>

Nice! What’s the trick here? What does `@taylorize` do?

---

<div class="post-metadata">

**Author:** ![mforets](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mforets/32/298_2.png) [@mforets](https://discourse.julialang.org/u/mforets)\
**Post date:** [July 22, 2021, 12:53am UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070/7 "2021-07-22T00:53:13Z")

</div>

> [@Samuel\_Ainsworth](#):
>
> What’s the trick here?

Do you mean the math? You could check works by Berz and Makino, M. Joldes, X. Chen, F. Bünger to name just a few.

About the implementation, you could check a JuliaCon 2018 presentation by @lbenet and @dpsanders about Taylor models. The methods used above to propagate solutions of nonlinear ODEs is more recent though.

> [@Samuel\_Ainsworth](#):
>
> What does `@taylorize` do?

It is an optimization strategy used in TaylorIntegration.jl; I checked removing it in this example and there is no difference with and without it.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 22, 2021, 8:05am UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070/8 "2021-07-22T08:05:21Z")

</div>

Great post!

For interactivity, you could use [GitHub - JuliaPluto/PlutoSliderServer.jl: Web server to run just the `@bind` parts of a Pluto.jl notebook](https://github.com/JuliaPluto/PlutoSliderServer.jl) in addition. With it, users can manipulate PlutoUI elements on the web page itself (without running Pluto locally) and the shown notebook content updates in real-time.  
The disadvantage is that it needs som (virtual) machine to run the server.

PlutoSliderServer is used e.g. here:

[https://computationalthinking.mit.edu/Spring21/images/](https://computationalthinking.mit.edu/Spring21/images/)

---

<div class="post-metadata">

**Author:** ![Samuel\_Ainsworth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_ainsworth/32/15644_2.png) [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Post date:** [July 22, 2021, 8:20pm UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070/9 "2021-07-22T20:20:15Z")

</div>

Thanks! Yeah, I would’ve liked to add some interactivity but unfortunately the site is fully static, so I don’t have a VM to spare. It would really be nice if Julia could run in the browser via WebAssembly or something instead.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 23, 2021, 7:07am UTC](https://discourse.julialang.org/t/show-and-tell-blog-post-on-interval-arithmetic-in-pluto-jl/65070/10 "2021-07-23T07:07:30Z")

</div>

Wasm support for Julia would be great!  
There are some efforts in this direction: [https://github.com/Keno/julia-wasm](https://github.com/Keno/julia-wasm), [Web Platform Projects – Summer of Code](https://julialang.org/jsoc/gsoc/wasm/)
