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

I am getting a problem with trying to include time dependant libraries in the when using sindy #568

Open
munozdjp opened this issue Oct 10, 2024 · 0 comments

Comments

@munozdjp
Copy link

munozdjp commented Oct 10, 2024

describing the general problem

I can not get an approximation of my system when using time dependant parameters:

I want to predict my system
image

I have tried the following:

import os
import sys
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint, solve_ivp
import pysindy as ps
from pysindy.feature_library import ConcatLibrary

# Define the system of equations
def system(t, x):
    dxdt = t**2 * x[0] - x[1] - x[0]**3 * t**3 + x[0] * x[1]**2
    dydt = x[0] * t + x[1] * t**2 + t**4 * x[1] - x[1] * (x[0]**2 + x[1]**2)
    return [dxdt, dydt]

# Initial conditions and time span
x0 = 1
y0 = 1
xoinitial = [x0, y0]
tspan = (0, 10)
t_eval = np.arange(0, 10, 0.01)  # Time points for evaluation

# Integrate the system
sol = solve_ivp(system, tspan, xoinitial, t_eval=t_eval)

# Prepare data for SINDy
x_train = sol.y.T
 # Stack x and y
t_train = sol.t

def compute_derivatives_analytical(x_solutions, t_eval):
    xdot = np.zeros_like(x_solutions.T)

    # Loop through each time point and evaluate the derivative
    for i, t in enumerate(t_eval):
        xdot[i] = system(
            t,
            x_solutions[:, i]
        )

    return xdot

# Compute the derivatives analytically
xdot = compute_derivatives_analytical(sol.y, t_eval)

# Create feature libraries (polynomial + time)
polynomial_library = ps.PolynomialLibrary(degree=3)
custom_library = ps.CustomLibrary(library_functions=[lambda x, t: t], function_names=["t"])

# Combine polynomial and time-dependent libraries
combined_library = ConcatLibrary([polynomial_library, custom_library])

# Initialize SINDy model with time-dependent features
differentiation_method = ps.FiniteDifference(order=2)
optimizer = ps.STLSQ(threshold=0.1)

sindy_model = ps.SINDy(
    feature_library=combined_library,
    optimizer=optimizer,
    feature_names=["x0", "x1"]
)

# Fit SINDy model
sindy_model.fit(x=x_train, x_dot=xdot, t=t_eval)

# Print the discovered SINDy model
sindy_model.print()

# Predict using SINDy model (optional)
x_pred = sindy_model.simulate(xoinitial, t_eval)

# Plot comparison of original vs predicted
plt.figure(figsize=(10, 6))
plt.plot(t_eval, sol.y[0], label='Original x0', linestyle='--')
plt.plot(t_eval, x_pred[:, 0], label='Predicted x0')
plt.plot(t_eval, sol.y[1], label='Original x1', linestyle='--')
plt.plot(t_eval, x_pred[:, 1], label='Predicted x1')
plt.title('SINDy Model vs Original Hopf Model')
plt.xlabel('Time (t)')
plt.ylabel('x0, x1 values')
plt.grid(True)
plt.legend()
plt.show()


Additional context

however I get a weird error when trying to print the system and it seems i do not have equation when using sindy.

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

1 participant