# Getting Started

The workflow for DataDrivenDiffEq.jl is similar to other SciML packages. You start by defining a DataDrivenProblem and then dispatch on the solve command to return a DataDrivenSolution.

Here is an outline of the required elements and choices:

using DataDrivenDiffEq
using ModelingToolkit
using LinearAlgebra

Generate a test problem

f(u) = u.^2 .+ 2.0u .- 1.0
X = randn(1, 100);
Y = reduce(hcat, map(f, eachcol(X)));

Create a problem from the data

problem = DirectDataDrivenProblem(X, Y, name = :Test)
Direct DataDrivenProblem{Float64} Test in 1 dimensions and 100 samples

Choose a basis

@variables u
basis = Basis(monomial_basis([u], 2), [u])
println(basis)
Model ##Basis#325 with 3 equations
States : u
Independent variable: t
Equations
φ₁ = 1
φ₂ = u
φ₃ = u^2

Solve the problem, using the solver of your choosing

res = solve(problem, basis, STLSQ())
println(res)
println(result(res))
Linear Solution with 1 equations and 3 parameters.
Returncode: solved
L₂ Norm error : [1.4344326225796133e-30]
AIC : [-6865.678340375698]
R² : [1.0]

Model ##Basis#328 with 1 equations
States : u
Parameters : p₁ p₂ p₃
Independent variable: t
Equations
φ₁ = p₁ + p₂*u + p₃*(u^2)

## Copy-Pasteable Code

using DataDrivenDiffEq
using ModelingToolkit
using LinearAlgebra

f(u) = u.^2 .+ 2.0u .- 1.0
X = randn(1, 100);
Y = reduce(hcat, map(f, eachcol(X)));

problem = DirectDataDrivenProblem(X, Y, name = :Test)

@variables u
basis = Basis(monomial_basis([u], 2), [u])
println(basis)

res = solve(problem, basis, STLSQ())
println(res)
println(result(res))

# This file was generated using Literate.jl, https://github.com/fredrikekre/Literate.jl