Linear Time Continuous System
Similar to the linear time discrete example
, we will now estimate a linear time continuous system $\partial_t u = A u$. We simulate the corresponding system using OrdinaryDiffEq.jl
and generate a ContinuousDataDrivenProblem
from the simulated data.
using DataDrivenDiffEq
using LinearAlgebra
using OrdinaryDiffEq
using DataDrivenDMD
using Plots
A = [-0.9 0.2; 0.0 -0.2]
u0 = [10.0; -10.0]
tspan = (0.0, 10.0)
f(u, p, t) = A * u
sys = ODEProblem(f, u0, tspan)
sol = solve(sys, Tsit5(), saveat = 0.05);
We could use the DESolution
to define our problem, but here we want to use the data for didactic purposes. For a ContinuousDataDrivenProblem
, we need either the state trajectory and the timepoints or the state trajectory and its derivate.
X = Array(sol)
t = sol.t
prob = ContinuousDataDrivenProblem(X, t)
Continuous DataDrivenProblem{Float64} ##DDProblem#275 in 2 dimensions and 201 samples
And plot the problem's data.
plot(prob)
We can see that the derivative has been automatically added via a collocation
method, which defaults to a LinearInterpolation
. We can do a visual check and compare our derivatives with the interpolation of the ODESolution
.
DX = Array(sol(t, Val{1}))
scatter(t, DX', label = ["Solution" nothing], color = :red, legend = :bottomright)
plot!(t, prob.DX', label = ["Linear Interpolation" nothing], color = :black)
Since we have a linear system, we can use gDMD
, which approximates the generator of the dynamics
res = solve(prob, DMDSVD())
println(res)
"DataDrivenSolution{Float64}" with 2 equations and 4 parameters.
Returncode: Success
Residual sum of squares: 0.2378268402390993
And also plot the prediction of the recovered dynamics
plot(res)
Copy-Pasteable Code
using DataDrivenDiffEq
using LinearAlgebra
using OrdinaryDiffEq
using DataDrivenDMD
A = [-0.9 0.2; 0.0 -0.2]
u0 = [10.0; -10.0]
tspan = (0.0, 10.0)
f(u, p, t) = A * u
sys = ODEProblem(f, u0, tspan)
sol = solve(sys, Tsit5(), saveat = 0.05);
X = Array(sol)
t = sol.t
prob = ContinuousDataDrivenProblem(X, t)
# This file was generated using Literate.jl, https://github.com/fredrikekre/Literate.jl
This page was generated using Literate.jl.