-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using DataDrivenDiffEq | ||
using ModelingToolkit | ||
using OrdinaryDiffEq | ||
|
||
using LinearAlgebra | ||
using Plots | ||
gr() | ||
|
||
# Create a test problem | ||
function lorenz(u,p,t) | ||
x, y, z = u | ||
|
||
ẋ = 10.0*(y - x) | ||
ẏ = x*(28.0-z) - y | ||
ż = x*y - (8/3)*z | ||
return [ẋ, ẏ, ż] | ||
end | ||
|
||
u0 = [1.0;0.0;0.0] | ||
tspan = (0.0,100.0) | ||
prob = ODEProblem(lorenz,u0,tspan) | ||
sol = solve(prob, Tsit5(), saveat = 0.005) | ||
|
||
plot(sol,vars=(1,2,3)) | ||
|
||
# Differential data from equations | ||
X = Array(sol) | ||
DX = similar(X) | ||
for (i, xi) in enumerate(eachcol(X)) | ||
DX[:,i] = lorenz(xi, [], 0.0) | ||
end | ||
|
||
# Estimate differential data from state variables via a Savitzky-Golay filter | ||
# Test on a single variable | ||
windowSize, polyOrder = 9, 4 | ||
DX1_sg = savitzky_golay(X[1,:], windowSize, polyOrder, deriv=1, dt=0.005) | ||
|
||
# Exclude borders, where the estimation is less accurate | ||
halfWindow = Int(ceil((windowSize+1)/2)) | ||
DX1_sg = DX1_sg[halfWindow+1:end-halfWindow] | ||
DX = DX[:,halfWindow+1:end-halfWindow] | ||
|
||
# Check if the estimated derivatives are approximate to the "ground truth" | ||
isapprox(DX1_sg, DX[1,:], rtol=1e-2) | ||
|
||
plot(DX1_sg, label = "Estimated with Savitzky-Golay filter") | ||
plot!(DX[1,:],label="Ground truth") | ||
|
||
|
||
# Now let's estimate the derivatives for all variables and use them infer the equations | ||
DX_sg = similar(X) | ||
for i =1:size(X,1) | ||
DX_sg[i,:] = savitzky_golay(X[i,:], windowSize, polyOrder, deriv=1, dt=0.005) | ||
end | ||
|
||
# Exclude borders, where the estimation is less accurate | ||
DX_sg = DX_sg[:,halfWindow+1:end-halfWindow] | ||
X_cropped = X[:,halfWindow+1:end-halfWindow] | ||
|
||
|
||
# Create a basis | ||
@variables u[1:3] | ||
|
||
# Lots of polynomials | ||
polys = [u[1]^0] | ||
for i ∈ 0:3 | ||
for j ∈ 0:3 | ||
for k ∈ 0:3 | ||
push!(polys, u[1]^i * u[2]^j * u[3]^k) | ||
end | ||
end | ||
end | ||
|
||
# And some other stuff | ||
h = [1u[1];1u[2]; cos(u[1]); sin(u[1]); u[1]*u[2]; u[1]*sin(u[2]); u[2]*cos(u[2]); polys...] | ||
basis = Basis(h, u) | ||
|
||
# Get the reduced basis via the sparse regression | ||
opt = STRRidge(0.1) | ||
Ψ = SInDy(X_cropped, DX_sg, basis, maxiter = 100, opt = opt) | ||
print(Ψ) | ||
|
||
|
||
# Now let's try adding some noise | ||
using Random | ||
seed = MersenneTwister(3) | ||
X_noisy = X + 0.01*randn(seed,size(X)) | ||
|
||
DX_sg = similar(X) | ||
X_smoothed = similar(X) | ||
for i =1:size(X,1) | ||
DX_sg[i,:] = savitzky_golay(X_noisy[i,:], windowSize, polyOrder, deriv=1, dt=dt) | ||
X_smoothed[i,:] = savitzky_golay(X_noisy[i,:], windowSize, polyOrder, deriv=0, dt=dt) | ||
end | ||
|
||
DX_sg = DX_sg[:,halfWindow+1:end-halfWindow] | ||
X_smoothed = X_smoothed[:,halfWindow+1:end-halfWindow] | ||
|
||
Ψ = SInDy(X_smoothed, DX_sg, basis, maxiter = 100, opt = opt) | ||
print(Ψ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters