Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
oCakeBn opened this issue Jan 28, 2022 · 2 comments

Comments

@oCakeBn
Copy link

oCakeBn commented Jan 28, 2022

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
Copy link
Collaborator

AlCap23 commented Jan 28, 2022

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
Copy link
Author

oCakeBn commented Jan 29, 2022

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants