dynamicslab / pysindy

A package for the sparse identification of nonlinear dynamical systems from data
https://pysindy.readthedocs.io/en/latest/
Other
1.42k stars 310 forks source link

Linear Parameter Varying System Identification (continued #352) #358

Closed Aditya16828 closed 1 year ago

Aditya16828 commented 1 year ago

Continuation of #352

import matplotlib.pyplot as plt
import pysindy as ps
import numpy as np

N = 1000
N_drop = 500
r0 = [3.5, 3.0]
vs=2+np.random.random(size=(2,N+N_drop))
xss = []
uss = []
for i in range(len(vs)):
    r = r0[i] + 1/vs[i]
    xs = []
    x = 0.5
    for n in range(N + N_drop):
        if n >= N_drop:
            xs.append(x)
        x = r[n] * x * (1 - x)
    xss.append(np.array(xs))
    uss.append(vs[i][N_drop:])

li = []
for i in range(1000):
  li.append(np.random.uniform(-6987.1714830972005, 6514.658733236362))

uss.append(np.array(li))

li = []
for i in range(1000):
  li.append(np.random.uniform(-6987.1714830972005, 6514.658733236362))

uss.append(np.array(li))

feature_lib = ps.PolynomialLibrary(degree=4, include_bias=True)
parameter_lib = ps.CustomLibrary(library_functions=[lambda x:1/x, lambda x:1/x, lambda x: x, lambda x: x], function_names=[lambda x:x+'^-1', lambda x:x+'^-1', lambda x: x, lambda x: x],include_bias=True)
lib = ps.ParameterizedLibrary(
    feature_library=feature_lib,
    parameter_library=parameter_lib,
    num_features=4,
    num_parameters=2,
)

opt = ps.STLSQ(threshold=1e-1, normalize_columns=False)
model = ps.SINDy(feature_library=lib, optimizer=opt, feature_names=["x1", "x2", "v1", "v2", "u1", "u2"], discrete_time=True)

model.fit(np.stack(xss, -1), u=np.stack(uss, -1))
model.print()

I have a situation where I have 2 control inputs other than the variable v (responsible for varying parameters). In this code I tried to create a similar situation by generating random numbers between a range where uss[2] and uss[3] are the control inputs (other than v). The code is running but it is not giving the correct output. Can anyone plz look into it and suggest me the changes or the ways I can tackle such a situation.

Jacob-Stevens-Haas commented 1 year ago

Hey, since you've asked a few questions here, I'm going to ask that you cleave more closesly to the expected way of asking questions.

I ran your model, and as expected, SINDy found that the random control inputs you added to u didn't affect x. So I'm not sure what you're asking.

Aditya16828 commented 1 year ago

So sorry sir, I was making a mistake. I overlooked that the values of uss[2] and uss[3] were being randomly generated, so there may not be any correlation between xss and uss. It worked correctly if I am using this code.