# Plotting the solution of a differential equation

**URL:** <https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627>\
**Category:** New to Julia\
**Tags:** diffeq\
**Created:** [January 19, 2021, 6:22pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627 "2021-01-19T18:22:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)\
**Post date:** [January 19, 2021, 6:22pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/1 "2021-01-19T18:22:49Z")

</div>

## I have written the Julia example for solving the Lorenz equation as in the page: [https://diffeq.sciml.ai/stable/tutorials/ode\_example/#Example-2:-Solving-Systems-of-Equations](https://diffeq.sciml.ai/stable/tutorials/ode_example/#Example-2:-Solving-Systems-of-Equations) :

function lorenz!(du,u,p,t)  
du[1] = 10.0\*(u[2]-u[1])  
du[2] = u[1]\*(28.0-u[3]) - u[2]  
du[3] = u[1]\*u[2] - (8/3)\*u[3]  
end

## u0 = [1.0;0.0;0.0] tspan = (0.0,100.0) prob = ODEProblem(lorenz!,u0,tspan) sol = solve(prob)

The problem appears when plotting because I obtain two different results when I was expecting the same result. Below I have attached the piece of code together with the plots.  
In the code, x[1,:],x[2,:] and x[3,:] are respectively the x,y,z components of the solution points.

 ![julia_plot](https://global.discourse-cdn.com/julialang/original/3X/a/1/a1314b0624a5f4b0486ba3073693237c5df7a283.png)

Thank you very much in advance.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 19, 2021, 8:29pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/2 "2021-01-19T20:29:00Z")

</div>

When plotting [DifferentialEquations.jl](https://github.com/SciML/DifferentialEquations.jl) solutions, data interpolation takes place within **Plots**. It seems to be a built in recipe…

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 19, 2021, 8:36pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/3 "2021-01-19T20:36:22Z")

</div>

Welcome with an interesting problem.

It is a little difficult to read your problem. It is recommended to insert in-line code between two single back-ticks (`). If you want to typeset more lengthy, multiline code, start the code section with three back-ticks followed by the word julia on the same line. Then new line(s) with your code. Close the code section with three back-ticks (without the word julia).

Presenting code as screen shot makes it virtually impossible to read on my screen – simply because my browser doesn’t allow for zooming the image.

---

<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:** [January 19, 2021, 9:11pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/4 "2021-01-19T21:11:57Z")

</div>

The plot recipe is using the continuous solution `sol(t)` to sample uniformly spaced points and generate a nicer plot than if you used the sample points `sol.t` and `sol[i]`.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 19, 2021, 9:55pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/5 "2021-01-19T21:55:06Z")

</div>

@ChrisRackauckas, in your [article](https://openresearchsoftware.metajnl.com/articles/10.5334/jors.151/) it is said that the continuous sol(t) is the interpolated solution at time t. Could you shed some light about this. Is it sampled or interpolated? Thank you.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 19, 2021, 10:45pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/6 "2021-01-19T22:45:00Z")

</div>

Chris is of course the authority here. My understanding is that the solution, assume it is named `sol`, is a data structure where `sol.t` contains the vector of the time points where the solution is computed, while `sol[i]` contains the solution at time `sol.t[i]`.

The data structure also contains an interpolation function, which is accessed using the idea of a “functor”: the “functor” facility enables the creation of a function with the same name as the solution data structure (`sol` in this example). So `sol(t)` calls this interpolation function for the solution at time `t`.

This means that if you use the _interpolation function_ in exactly the time points where the solution has been computed, i.e., `sol(sol.t[i])`, this should be identical to `sol[i]` (with a possible reorganization of the structure of the solution). But whereas you can only access the solution data structure `sol[i]` at integer values `i`, the interpolation function can be assessed at any floating point value (note: I have not tested it for extrapolation).

Specifically, you can create a range of time points, e.g., `T = range(0,20,length=500)` (just as a stupid example), and then compute the solution via the interpolation function in these time points: `sol.(T)`, and then (`using Plots`), plot the result by `plot(T,sol.(T))`.

I’m sure my explanation is not 100% accurate, but it is relatively close I think.

So, in summary: accessing the solution by square bracket, `sol[i]`, accesses the computed values stored in an array, while accessing the solution by parenthesis, `sol(t)`, uses function notation and accesses the interpolation function.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 19, 2021, 10:53pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/7 "2021-01-19T22:53:00Z")

</div>

… a similar idea of “functor” is used with package Polynomials.jl:

```julia
julia> using Polynomials

julia> pol = Polynomial([1,-2,0.5])
Polynomial(1.0 - 2.0*x + 0.5*x^2)

julia> length(pol)
3

julia> pol[0] # here, I access pol as a vector with the zero order coefficient, etc.
1.0

julia> pol[1]
-2.0

julia> pol[2]
0.5

julia> pol(3.14) # here, I access pol as a *function* 
-0.35019999999999984

```

---

<div class="post-metadata">

**Author:** ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)\
**Post date:** [January 20, 2021, 1:23am UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/8 "2021-01-20T01:23:55Z")

</div>

Thank you very much for all the answers. I am interested in reading some documentation about this issue. I have searched in both the DifferentialEquations.jl and Plots.jl documentation without success. I would like to see how is this functor built and how is the interpolation in the plots side performed. I guess there are some default arguments, and some arguments to be modified in order to manage the interpolation.  
All the best.

---

<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:** [January 20, 2021, 5:09am UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/9 "2021-01-20T05:09:26Z")

</div>

All of the code is open and fairly straightforward. Here’s the plot recipe:

[https://github.com/SciML/DiffEqBase.jl/blob/master/src/solutions/solution\_interface.jl#L59](https://github.com/SciML/DiffEqBase.jl/blob/master/src/solutions/solution_interface.jl#L59)

By default

[https://github.com/SciML/DiffEqBase.jl/blob/master/src/solutions/solution\_interface.jl#L66](https://github.com/SciML/DiffEqBase.jl/blob/master/src/solutions/solution_interface.jl#L66)

it plots 10,000 evenly spaced points.

[https://github.com/SciML/DiffEqBase.jl/blob/master/src/solutions/solution\_interface.jl#L201-L208](https://github.com/SciML/DiffEqBase.jl/blob/master/src/solutions/solution_interface.jl#L201-L208)

It just uses `sol(t)`. When I say “evenly”, if the plot is in log-space it takes those points in log space.

Is that what you were looking for?

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 20, 2021, 12:22pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/10 "2021-01-20T12:22:01Z")

</div>

> [@martinmestre](#):
>
> would like to see how is this functor built

In the Julia on-line documentation, search for “Function-like objects”.

---

<div class="post-metadata">

**Author:** ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)\
**Post date:** [January 20, 2021, 4:52pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/11 "2021-01-20T16:52:50Z")

</div>

@ChrisRackauckas @BLI :  
Thank you very much for your answers. Please let me know if I have understood correctly according to the following statements:  
The solution sol(t) is an interpolating function that can be evaluated at any “t”. Besides, inside the object sol, there is additional information (plot\_density argument, a 10,000 points grid) that can be later used by Plots. The interpolation to this dense grid is not done by Plots, but it is already built in sol(t). Is this right?  
Thanks again.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 20, 2021, 6:25pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/12 "2021-01-20T18:25:01Z")

</div>

First – if I write something that is at odds with what Chris writes, you should believe in Chris and not me – I’m just a user; Chris and his group developed the package.

I don’t know what plot density is used when plotting with the command `plot(sol)` and its variants. But you can always create your own sampling of the function by, say, `time = range(t1,t2,length=N)` and then `plot(time,sol.(time))` – here, you can choose `t1`, `t2`, and `N` relatively freely, although I’d assume you have to restrict `t1` and `t2` to be within the time span used when solving the model 🙂 .

---

<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:** [January 20, 2021, 6:53pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/13 "2021-01-20T18:53:01Z")

</div>

> [@martinmestre](#):
>
> The solution sol(t) is an interpolating function that can be evaluated at any “t”. Besides, inside the object sol, there is additional information (plot\_density argument, a 10,000 points grid) that can be later used by Plots. The interpolation to this dense grid is not done by Plots, but it is already built in sol(t). Is this right?

It’s done in the plot recipe.

---

<div class="post-metadata">

**Author:** ![martinmestre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinmestre/32/215857_2.png) [@martinmestre](https://discourse.julialang.org/u/martinmestre)\
**Post date:** [January 20, 2021, 7:19pm UTC](https://discourse.julialang.org/t/plotting-the-solution-of-a-differential-equation/53627/14 "2021-01-20T19:19:48Z")

</div>

Problem understood. Thanks.
