# Plotting a differential equation using Sympy.jl and Plots.jl

**URL:** <https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876>\
**Category:** General Usage\
**Tags:** plotting, diffeq\
**Created:** [September 18, 2019, 4:41am UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876 "2019-09-18T04:41:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [September 18, 2019, 4:41am UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876/1 "2019-09-18T04:41:07Z")

</div>

I was just checking out some parts of the Julia ecosystem and wanted to plot the solutions to some simple linear first order ordinary differential equations that were solved by Sympy.

So if I have a growth equation like

```julia
using SymPy
@vars t k
F = SymFunction("F")
diffeq = F'(t) - k*F(t)
res = dsolve(diffeq, F(t), ics=(F, 0, 2))

```

So I get an expected result like

F(t) = 2e^{kt}

However, when I go to plot this, the code will break

```julia
using Plots
plot(res, -1, 5) 

```

The message looks as below. I can include the full stacktrace if anyone is interested.

```julia
ERROR: MethodError: no method matching ##465(::Float64)
Closest candidates are:
  ##465(::Any, ::Any) at none:0

```

So I am just trying to figure out what I am missing to make the plot work? Any suggestions?

---

<div class="post-metadata">

**Author:** ![jkbest2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkbest2/32/7350_2.png) [@jkbest2](https://discourse.julialang.org/u/jkbest2)\
**Post date:** [September 18, 2019, 5:17am UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876/2 "2019-09-18T05:17:20Z")

</div>

You probably want to get a vector of times and the solution to your ODE at those times using your parameter, and then pass those vectors to `plot`. The `plot` function doesn’t know how to plot an arbitrary SymPy object.

---

<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:** [September 18, 2019, 10:44am UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876/3 "2019-09-18T10:44:37Z")

</div>

> [@jkbest2](#):
>
> The `plot` function doesn’t know how to plot an arbitrary SymPy object.

That’s incorrect. It does know how to plot an arbitrary SymPy object. This just looks like a bug. I’d open an issue.

---

<div class="post-metadata">

**Author:** ![jkbest2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkbest2/32/7350_2.png) [@jkbest2](https://discourse.julialang.org/u/jkbest2)\
**Post date:** [September 18, 2019, 12:58pm UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876/4 "2019-09-18T12:58:26Z")

</div>

Oops! Sorry, should have checked first!

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [September 18, 2019, 1:26pm UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876/5 "2019-09-18T13:26:03Z")

</div>

You need to plot an expression that can be “lambdified,” in this case, just the right hand side. You also need to have `k` set to a value, not a symbol. Something like this:

```julia
plot(rhs(res)(k=>2), 0, 2 )

```

The `lambdify` function could (should) be extended to handle `Eq` objects like `res`, so that might be worth an issue.

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [September 18, 2019, 3:21pm UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876/6 "2019-09-18T15:21:39Z")

</div>

Thanks Chris. Wow, I did not imagine I would find a bug. I figured that so many people would have used SymPy to plot simple ODEs that it must be a well-traveled area of code. I will open a bug request and see what the response is. Thanks.

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [September 18, 2019, 3:25pm UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876/7 "2019-09-18T15:25:21Z")

</div>

@j_verzani thanks for the explanation and the example. I saw that SymPy could plot other simple symbolic equations in a tutorial, but could not get it to work with the solutions to ODEs. I saw that a regular symbolic equation only had the right hand side and that ODE solutions had both a right and left hand side–but referenced the same “Symbol” type. So now I understand that I can extract the equation from the ODE solution using `rhs()`. I can use the code you provided :). I will also open up an issue with the SymPy.jl folks and see what they say.

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [September 18, 2019, 3:29pm UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876/8 "2019-09-18T15:29:13Z")

</div>

@jkbest2 thanks for the suggestion. You are right that I could have tried manually computing the output of the equation. I remember that in python Sympy a user can directly plot an ODE solution, so I figured it might be the same in Julia. Turns out Julia users can directly plot the solution too. I guess you never know till you try.

---

<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:** [September 18, 2019, 3:29pm UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876/9 "2019-09-18T15:29:35Z")

</div>

> [@j\_verzani](#):
>
> The `lambdify` function could (should) be extended to handle `Eq` objects like `res` , so that might be worth an issue.

Ahh, I didn’t know the Eq was different. Thanks!

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [September 18, 2019, 3:41pm UTC](https://discourse.julialang.org/t/plotting-a-differential-equation-using-sympy-jl-and-plots-jl/28876/10 "2019-09-18T15:41:53Z")

</div>

New issue opened with SymPy.jl.

[https://github.com/JuliaPy/SymPy.jl/issues/298](https://github.com/JuliaPy/SymPy.jl/issues/298)

So we can track the progress there.
