# Solve y'=y with neuralode

Dear all, I am beginner in this topic and trying to solve simple y’=y (analytical solution: exp(t)) with y0=0 by modifying code on Neural Ordinary Differential Equations · DiffEqFlux.jl (sciml.ai), my code until now:
begin
# NeuralODE

u01 = Float32[2.; 0.]
n_samples = 16
tspan1 = (0.0f0, 1.0f0)

function trueODEfunc(du, u, p, t)
true_A = [-0.1 2.0; -2.0 -0.1]
du .= ((exp(t))'true_A)'
end
t = range(tspan1[1], tspan1[2], length=n_samples)
prob = DiffEqFlux.ODEProblem(trueODEfunc, u01, tspan1)
ode_data = Array(solve(prob, Tsit5(), saveat=t))

model = Chain(
x -> exp(t),
Dense(2, 50, tanh),
Dense(50, 2),
)

n_ode = NeuralODE(model, tspan1, Tsit5(), saveat=t, reltol=1e-7, abstol=1e-9)
ps1 = Flux.params(n_ode)

loss_n_ode() = sum(abs2, ode_data .- n_ode(u01))

data = Iterators.repeated((), 25)  # epochs

pred = n_ode(u01)

traces = [
scatter(x=t, y=ode_data[1, :], name="u_1", mode="lines+markers"),
scatter(x=t, y=ode_data[2, :], name="u_2", mode="lines+markers"),
scatter(x=t, y=pred[1, :], name="u_1 pred", mode="lines+markers"),
scatter(x=t, y=pred[2, :], name="u_2 pred", mode="lines+markers"),
]

plt = plot(traces)


end

of course it throws error but am unable to understand the setup, does anybody has idea how to start to structure the code correctly? thank you in advance

Are you trying to learn it just from data? x -> exp(t) won’t work because it would have to be a function of x, and it’s a somewhat ill-advised to assume the derivative is an exponential function (since it’s the solution that would be).

Your true ODE function isn’t y'=y here, so I an quite get what you’re trying to do.

thanks for your reply, the thing that i am trying to do is to solve simple ode y’=y for t =(0,1) with initial condition y0=1 by using PiNN and with Neural ODE:
begin
time_min = 0.0
time_max = 1.0
y0 = [1.0]
end;
function ode_problem(dy, y, params, t)
dy[1] = y[1]
end;
q_analytical(t) = exp(t)
numerical_solution = let
# Define the ODE problem
prob = ODEProblem(ode_problem, y0, (time_min, time_max))
# Solve the ODE problem
solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
end;
this is my code prior to PiNN and Neural ODE, but I am quite confused how to put this to NN, should I simply generate data from exp() function and learn it? and if yes i am not sure how to implement it using available examples? thank you in advance

the other thing i tried is:

# PiNN

begin
linear(u, p, t) = u
tspan = (0.0, 1.0)
u0 = 0.0
prob1 = ODEProblem(linear, u0, tspan)
end
begin
rng = Random.default_rng()
Random.seed!(rng, 0)
chain = Lux.Chain(Lux.Dense(1, 5, σ), Lux.Dense(5, 1))
ps, st = Lux.setup(rng, chain) |> Lux.f64
alg = NeuralPDE.NNODE(chain, opt, init_params = ps)
end
sol = solve(prob1, alg, verbose = true, maxiters = 2000, saveat = 0.01)

which after couple of executions learns the function but not sure if there is better solution?

What are you trying to do with it? If you know the ODE, you don’t need a neural ODE.

Using a standard ODE solver will be much faster than a NN here.

1 Like

Hi yes agree with you but it is simply the task i need to do even if it is kind of a useless, and to me it is unclear how to push it thru NN so i was asking for help - that’s all.

I’m not 100% sure what you’re even trying to do.