SciML / DataDrivenDiffEq.jl

Data driven modeling and automated discovery of dynamical systems for the SciML Scientific Machine Learning organization
https://docs.sciml.ai/DataDrivenDiffEq/stable/
MIT License
404 stars 57 forks source link

Solving Lorentz attractor example using sol.u and sol.t gives me wrong result #331

Open oCakeBn opened 2 years ago

oCakeBn commented 2 years ago

I've tried Lorentz attractor example from the readme.md.

The example worked like a charm, but when I wanted to run the same example but providing direct trajectories and corresponding time (using sol.u and sol.t like here https://pastebin.com/1GCjTqzb) the experiment failed, producing only one equation (instead of three) with 16 coefficients. I didn't find any example so far (I guess I'm not google-ing right, as all examples on the internet seem to be a copy-past of the example given in the readme file) and that's why I wanted to ask you for a little help with this as I assume that I'm making some beginner mistake and I can't see in which wrong way I'm providing the data.

AlCap23 commented 2 years ago

I've slightly modified your code to


using DataDrivenDiffEq, ModelingToolkit, OrdinaryDiffEq, LinearAlgebra

function lorenz(u, p, t)
    x, y, z = u

    xdot = 10.0*(y-x)
    ydot = x*(28.0 - z) - y
    zdot = x*y - (8/3)*z
    return [xdot, ydot, zdot]
end

u0 = [1.0; 0.0; 0.0]
tspan = (0.0, 100.0)
dt = 0.1
prob = ODEProblem(lorenz, u0, tspan)
sol = solve(prob, Tsit5(), saveat = dt, progress = true)

cddprob = ContinuousDataDrivenProblem(sol) # I can use an ODE solution for definition of a Problem
@variables t x y z
u = [x; y; z]
basis = Basis(polynomial_basis(u, 5), u)
opt = STLSQ(exp10.(-5:0.1:-1))
cdpsol = solve(cddprob, basis, opt, normalize=true, progress = true) # Progress steps does nothing
print(cdpsol, Val{true})

print(cdpsol.basis)

Which gives me

Linear Solution with 3 equations and 7 parameters.
Returncode: solved
L₂ Norm error : [4.4113262057330353e-26, 3.580143344355224e-25, 7.115925489347665]
AIC : [130728.16723562666, 126536.35448699378, 9916.731799817293]
R² : [1.0, 1.0, 0.9999862396296991]
Parameters:
   p₁ : -10.0
   p₂ : 10.0
   p₃ : 28.0
   p₄ : -1.0
   p₅ : -1.0
   p₆ : 1.0
   p₇ : -2.67

Model ##Basis#393 with 3 equations
States : x y z
Parameters : 7
Independent variable: t
Equations
Differential(t)(x) = p₁*x + p₂*y
Differential(t)(y) = p₃*x + p₄*y + p₅*x*z
Differential(t)(z) = p₇*z + p₆*x*y

The error can come from your setup for the problem definition ( since the derivatives are computed using a LinearInterpolation ). I am currently on a dev branch, where collocation is broken, but using the latest release, this should help:


using Plots

X = Array(sol) # Simpler way for the data to be parsed in
t = sol.t
DX = collocate_data(X, t, GaussianKernel())[1] # I compute the derivative and smoothed data, but just use the derivative
cddprob = ContinuousDataDrivenProblem(X, t, InterpolationMethod())
plot(cddprob)

lorenz

But I'll investigate this on the current master within the next few hours.

oCakeBn commented 2 years ago

Hi AlCap23, the slightly modified code is something that runs fine on my machine as that one is I believe the same code given as an example on how to use this package.

I've tried to set up the problem as you've suggested in the second part, but that gives me 3 equations with 55 parameters. Also, I don't see where variable DX is used?

Again, thanks for helping out with this :)