# External polynomial supply to DifferentialEquation.jl

**URL:** <https://discourse.julialang.org/t/external-polynomial-supply-to-differentialequation-jl/84906>\
**Category:** General Usage\
**Created:** [July 28, 2022, 6:33am UTC](https://discourse.julialang.org/t/external-polynomial-supply-to-differentialequation-jl/84906 "2022-07-28T06:33:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aasgher385](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aasgher385/32/38329_2.png) [@aasgher385](https://discourse.julialang.org/u/aasgher385)\
**Post date:** [July 28, 2022, 6:33am UTC](https://discourse.julialang.org/t/external-polynomial-supply-to-differentialequation-jl/84906/1 "2022-07-28T06:33:59Z")

</div>

Hi everyone

I have a problem solving the Riccati type differential equation.

\frac{dU}{dt} = \delta \frac{dD}{dt} - \lambda U - \gamma U^2

I have an array of dimension M for D from which, in my opinion we can get a polynomial and then supply the polynomial into DifferentialEquations.jl routine inside which we can then differentiate and convert back to an array of dimension M.

I also tried using the finite difference approach, but the right-hand side with \frac{dD}{dt} has dimension M-1, which is a problem.

Any help is appreciated.

---

<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 28, 2022, 7:36am UTC](https://discourse.julialang.org/t/external-polynomial-supply-to-differentialequation-jl/84906/2 "2022-07-28T07:36:43Z")

</div>

> [@aasgher385](#):
>
> I have an array of dimension M MM for D DD from which, in my opinion we can get a polynomial and then supply the polynomial into DifferentialEquations.jl routine inside which we can then differentiate and convert back to an array of dimension M MM .

Use DataInterpolations.jl and grab its derivative?

---

<div class="post-metadata">

**Author:** ![aasgher385](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aasgher385/32/38329_2.png) [@aasgher385](https://discourse.julialang.org/u/aasgher385)\
**Post date:** [July 28, 2022, 8:41am UTC](https://discourse.julialang.org/t/external-polynomial-supply-to-differentialequation-jl/84906/3 "2022-07-28T08:41:49Z")

</div>

Thank you so much for your quick response.

I have used the DataInterpolations.jl and generated a p=CubicSpline(D,t) but taking the derivative with the command derivative(p) is not working. To make it work, I have to use convert(Polynomial,p), and then the differentiation is possible, but the nodes are of magnitude 1e16.

Experiment:

1. Considering M=11 nodes, with t=collect(0.0:1.0:10) and u=ones(M)
2. p=CubicSpline(u,t)
3. c=convert(Polynomial,p)
4. p\_2 = derivative(c)
5. p\_2.(t) = [1.0, 1045.0, 3.81707e8,1.0225e12 ,2.93529e14, 2.41548e16 ,8.95379e17,1.90888e19,2.71108e20,2.82139e21,2.29657e22]

Apart from this being of large magnitude, it is working.

Is there any possibility that we can have a polynomial of a certain order (like maybe 5 or something) but with a number of nodes that is fixed like M in this case?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [July 28, 2022, 11:17am UTC](https://discourse.julialang.org/t/external-polynomial-supply-to-differentialequation-jl/84906/4 "2022-07-28T11:17:12Z")

</div>

The problem is the conversation to a `Polynomial`. You could use this instead:

```julia
using DataInterpolations

M = 11
ts = 0.0:10.0
u = ones(M)

p = CubicSpline(u, ts)
D = [DataInterpolations.derivative(p, t) for t in ts]

```

If you want the derivative as a function, you could use `ForwardDiff` or simply define

```julia
dp_dt = t -> DataInterpolations.derivative(p, t)

```

and use that in your ODE problem. (But make sure that `p` in this setting is not a global variable, but a parameter of the ODE, otherwise it will be unnecessarily slow.)

PS: By the way, you can embed source code with one or three backticks, i.e. ````` source code `````.

---

<div class="post-metadata">

**Author:** ![aasgher385](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aasgher385/32/38329_2.png) [@aasgher385](https://discourse.julialang.org/u/aasgher385)\
**Post date:** [August 1, 2022, 6:50am UTC](https://discourse.julialang.org/t/external-polynomial-supply-to-differentialequation-jl/84906/5 "2022-08-01T06:50:08Z")

</div>

Thanks, @SteffenPL, and @ChrisRackauckas. I was able to make it work through your instructions. I was wondering about what if we use Chebyshev polynomial instead of CubicSpline. Would it work provided this number of nodes would go up and polynomial would of order M?

```julia
using Polynomials 
M = 11
u = ones(M)
p = ChebyshevT(u)
p1 = convert(Polynomial, c)

```

I would like to have a Chebyshev polynomial of order, say 5, instead of going through all M nodes.
