# Solving ODE by PDESystem

**URL:** <https://discourse.julialang.org/t/solving-ode-by-pdesystem/100293>\
**Category:** General Usage\
**Tags:** question, pde, ode, neural-network\
**Created:** [June 13, 2023, 6:28pm UTC](https://discourse.julialang.org/t/solving-ode-by-pdesystem/100293 "2023-06-13T18:28:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ethan\_Tran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethan_tran/32/49198_2.png) [@Ethan\_Tran](https://discourse.julialang.org/u/Ethan_Tran)\
**Post date:** [June 13, 2023, 6:28pm UTC](https://discourse.julialang.org/t/solving-ode-by-pdesystem/100293/1 "2023-06-13T18:28:03Z")

</div>

I try to use NeuralPDE to solve the ODE system.  
the code I use below:

```julia
using NeuralPDE, Lux, ModelingToolkit, Optimization, OptimizationOptimJL
import ModelingToolkit: Interval, infimum, supremum

@parameters t
@variables u1(..), u2(..)
D = Differential(t)

omega_R = 120*pi
Pm = 1
E = 1.0566
V = 1
X = 0.8805
Ps = E*V/X
H = 15;

eq = [D(u1(t)) ~ u2(t) - omega_R,
    D(u2(t)) ~ (Pm - Ps*sin(u1(t))-1.2*(u2(t)-omega_R)/omega_R)*(omega_R/(2*H))]

bcs = [u1(0) ~ 0.9851+0.01, u2(0) ~ omega_R]
domains = [t ∈ Interval(0.0,10.0)]
dt = 0.01

input_ = length(domains) # number of dimensions
n = 16
chain =[Lux.Chain(Dense(input_,n,Lux.σ),Dense(n,n,Lux.σ),Dense(n,1)) for _ in 1:2]

@named pde_system = PDESystem(eq,bcs,domains,[t],[u1(t),u2(t)])

strategy = NeuralPDE.GridTraining(dt)
discretization = PhysicsInformedNN(chain, strategy)
sym_prob = NeuralPDE.symbolic_discretize(pde_system, discretization)

pde_loss_functions = sym_prob.loss_functions.pde_loss_functions
bc_loss_functions = sym_prob.loss_functions.bc_loss_functions

callback = function (p, l)
    println("loss: ", l)
    println("pde_losses: ", map(l_ -> l_(p), pde_loss_functions))
    println("bcs_losses: ", map(l_ -> l_(p), bc_loss_functions))
    return false
end

loss_functions = [pde_loss_functions;bc_loss_functions]

function loss_function(θ,p)
    sum(map(l->l(θ) ,loss_functions))
end

f_ = OptimizationFunction(loss_function, Optimization.AutoZygote())
prob = Optimization.OptimizationProblem(f_, sym_prob.flat_init_params)

res = Optimization.solve(prob,OptimizationOptimJL.BFGS(); callback = callback, maxiters=5000)
phi = discretization.phi

ts = 0.0:0.01:10.0
minimizers_ = [res.u.depvar[sym_prob.depvars[i]] for i in 1:2]
u_predict = [[phi[i]([t],minimizers_[i])[1] for t in ts] for i in 1:2]

```

After changing the iterations and getting the loss approx zero (3.571235060904114e-5), I plot the results from NeuralPDE and Tsit5().  
The results from Tsit5() have the graph as a sin function, but the results from NeuralPDE just a line.  
Please help me to fix my code.

![frequency_small](https://global.discourse-cdn.com/julialang/original/3X/e/7/e79df49e4c203a750b330ad25958165cce4ef096.jpeg)

---

<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 25, 2023, 12:12pm UTC](https://discourse.julialang.org/t/solving-ode-by-pdesystem/100293/2 "2023-09-25T12:12:54Z")

</div>

> [@Ethan\_Tran](#):
>
> `strategy = NeuralPDE.GridTraining(dt)`

GridTraining just isn’t stable enough. You need to use something else like `QuadratureStrategy` that automatically adapts. Also `BFGS` is prone to getting into local optima. It shouldn’t be used as the only optimizer. Please follow the directions as shown in the tutorials to get more stable solvers.

Also, note that NNODE will be a faster and more stable method (by design) for ODEs.
