Skip to content

How to plot time history and phase diagram of data-driven equation #567

@Aminofa70

Description

@Aminofa70

Dear Developers,
Thanks for you great package. I used your package for modeling a non-autonomous Duffing equation.
I want to plot the time-history and phase digram of the ode obtained from data here (res and pm) and compare it with data (here data is generated using ode duffing). I would appreciate it if you could guid me?

using DataDrivenDiffEq
using ModelingToolkit
using DifferentialEquations
using LinearAlgebra
using DataDrivenSparse
using Plots
gr()
# --- Duffing system with forcing ---
function duffing(u, p, t)
    du = similar(u)
    δ = 0.3 ; α = 1.0 ; β = 1.0 ;γ = 0.1 ; ω = 1.2
    du[1] = u[2]
    du[2] = -δ*u[2] - α*u[1] - β*u[1]^3 + γ*cos(ω*t)
    return du
end
u0 = [1.0; 0.0]
tspan = (0.0, 200.0)
dt = 0.01
prob = ODEProblem(duffing, u0, tspan)
sol = solve(prob, Tsit5(), saveat=dt)
X = sol[:, :]
DX = similar(X)
for (i, xi) in enumerate(eachcol(X))
    DX[:, i] = duffing(xi, [], sol.t[i])
end
t = sol.t
ddprob = ContinuousDataDrivenProblem(X, t, DX=DX)
@parameters t
@variables u[1:2]
u = collect(u)
Ψ = Basis([u; u[1]^3; 0.1*cos.(1.2*t)], u, independent_variable=t)  
λ = 1e-3
opt = STLSQ(λ)
res = solve(ddprob, Ψ, opt)
fieldnames(typeof(res))
basis_result = get_basis(res)
fieldnames(typeof(basis_result))
pm = get_parameter_map(basis_result)
println("Discovered parameters:")
for (k,v) in pm
    println(k, " => ", v)
end


Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions