# Is it possible to solve pdes using neuralpde.jl that involve interpolated function

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-solve-pdes-using-neuralpde-jl-that-involve-interpolated-function/103820>\
**Category:** General Usage\
**Tags:** sciml, neural-network, differentialequation\
**Created:** [September 13, 2023, 4:42pm UTC](https://discourse.julialang.org/t/is-it-possible-to-solve-pdes-using-neuralpde-jl-that-involve-interpolated-function/103820 "2023-09-13T16:42:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shashank](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashank/32/12323_2.png) [@Shashank](https://discourse.julialang.org/u/Shashank)\
**Post date:** [September 13, 2023, 4:42pm UTC](https://discourse.julialang.org/t/is-it-possible-to-solve-pdes-using-neuralpde-jl-that-involve-interpolated-function/103820/1 "2023-09-13T16:42:12Z")

</div>

I am trying to solve a differential equation of the form dy(t)/dt = -y(t) f(t), but I do not know the function f(t). I generate f(t) by interpolating values from a data file. In that case, neuralpde.jl fails. Is it possible to convert an interpolating function to a symbolic function? Are there any other alternatives to solving such differential equations using neural networks?

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [September 13, 2023, 5:53pm UTC](https://discourse.julialang.org/t/is-it-possible-to-solve-pdes-using-neuralpde-jl-that-involve-interpolated-function/103820/2 "2023-09-13T17:53:04Z")

</div>

It seems like that form could be solved with an [integrating factor](https://www.mathcentre.ac.uk/resources/uploaded/mathcentre-ode.pdf)? Please forgive if math is shaky, it has been many many years since I took that class.

\begin{aligned} \frac{d y}{d t} + y f(t) &= 0 \\ E &= \exp\biggl(\int\_{-\infty}^t f(w) dw \biggr) \\ E \frac{d y}{d t} + E y f(t) &= 0 \\ \frac{d}{dt}\bigl(y E \bigr) &= 0 \\ \therefore y &= C \exp\biggl(-\int\_{-\infty}^t f(w) dw \biggr) \end{aligned}

---

<div class="post-metadata">

**Author:** ![Shashank](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashank/32/12323_2.png) [@Shashank](https://discourse.julialang.org/u/Shashank)\
**Post date:** [September 13, 2023, 5:56pm UTC](https://discourse.julialang.org/t/is-it-possible-to-solve-pdes-using-neuralpde-jl-that-involve-interpolated-function/103820/3 "2023-09-13T17:56:53Z")

</div>

Yes. I did not want to write the complicated pde I am trying to solve. Right now, I am solving the pde by discretizing it over the domain, and it is taking a long time. I was hoping that neural network-based methods would be helpful.

---

<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 13, 2023, 6:38pm UTC](https://discourse.julialang.org/t/is-it-possible-to-solve-pdes-using-neuralpde-jl-that-involve-interpolated-function/103820/4 "2023-09-13T18:38:10Z")

</div>

> [@Shashank](#):
>
> I generate f(t)f(t)f(t) by interpolating values from a data file.

Using DataInterpolations.jl? That should work.

> [@Shashank](#):
>
> Are there any other alternatives to solving such differential equations using neural networks?

Why using Neural Networks? A traditional method will be a whole lot faster here.

---

<div class="post-metadata">

**Author:** ![Shashank](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashank/32/12323_2.png) [@Shashank](https://discourse.julialang.org/u/Shashank)\
**Post date:** [September 13, 2023, 8:35pm UTC](https://discourse.julialang.org/t/is-it-possible-to-solve-pdes-using-neuralpde-jl-that-involve-interpolated-function/103820/5 "2023-09-13T20:35:52Z")

</div>

> [@ChrisRackauckas](#):
>
> DataInterpolations.jl

I can get the interpolation to work, but neuralpde does not accept it.

The following code works:

```julia
## coll_t1p.jl
using DelimitedFiles
using DataInterpolations

cemit_fp=readdlm("Cemit_t1.dat",Float64)

cemit=LinearInterpolation(vec(cemit_fp[:,1]),vec(cemit_fp[:,2]))

println(size(vec(cemit_fp[:,1])))
println(typeof(vec(cemit_fp[:,1])))

println(size(vec(cemit_fp[:,2])))
println(typeof(vec(cemit_fp[:,2])))

println(cemit(20.0e0))

```

but when I try to solve the following differential equation, I get an error.

```julia
using NeuralPDE, Lux, ModelingToolkit, Optimization, OptimizationOptimisers
import ModelingToolkit: Interval, infimum, supremum
include("coll_t1.jl")

@parameters r cth
@variables u(..)
Dr = Differential(r)
Dcth = Differential(cth)
Icth = Integral(cth in DomainSets.OpenInterval(-1,1))

# 2D PDE
eq = cth * Dr(u(r,cth)) + ((1 - cth * cth) / r) * Dcth(u(r,cth)) ~ cemit(r) - d(r) * u(r,cth) + cd(r) * Icth(u(r,cth))

```

I am looking for a solution to this equation. The functions cemit, d and cd are interpolated from data files. The way I am solving the equation right now is by solving the time-dependent equation:

```julia
eq = Dt(u(t,r,ch)) + cth * Dr(u(t,r,cth)) + ((1 - cth * cth) / r) * Dcth(u(t,r,cth)) ~ cemit(r) - d(r) * u(t,r,cth) + cd(r) * Icth(u(t,r,cth))

```

by discretizing over r and cth and evolving over time using an odesolver. At long enough time, it reaches a steady state. I was thinking I could get the steady state directly using neuralpde.jl. I tried writing my own code using Flux.jl but I do not get the right results. The loss function does not decrease fast enough and the code is very slow because of the integral in the equation.

---

<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 13, 2023, 8:38pm UTC](https://discourse.julialang.org/t/is-it-possible-to-solve-pdes-using-neuralpde-jl-that-involve-interpolated-function/103820/6 "2023-09-13T20:38:52Z")

</div>

Did you try just using DifferenitalEquations.jl and SteadyStateProblem?

---

<div class="post-metadata">

**Author:** ![Shashank](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashank/32/12323_2.png) [@Shashank](https://discourse.julialang.org/u/Shashank)\
**Post date:** [September 13, 2023, 8:45pm UTC](https://discourse.julialang.org/t/is-it-possible-to-solve-pdes-using-neuralpde-jl-that-involve-interpolated-function/103820/7 "2023-09-13T20:45:18Z")

</div>

Thanks a lot! I was not aware of that. Let me try it out and see whether it works in my case.
