# Trouble implementing switching hybrid system with NDSolve

I'm trying to use NDSolve to simulate a hybrid nonlinear system that switches between different linear behaviors based on states and inputs (i.e. switching from one linear behavior to another) which is defined as $$\dot{\mathbf{x}}=F(\mathbf{x},\mathbf{u})$$ where $$F$$ is peicewise linear in $$\mathbf{x}$$ and $$\mathbf{u}$$.

Whenever some conditions are true of a certain state and its neighbors, as defined by the truth of the following inequality,

(-X1 + X2 <= -0.25 && -X0 + X1 < 0) || (-X1 + X2 >= 0.25 && -X0 + X1 > 0)


where X1 is the state X0, and X2 are some nearby states or inputs, the state is "locked" to take on the value of another state and its corresponding row in the state matrix is temporarily {0, 0... ...1... ...0, 0}. This behavior applies to a specific list of states (and their nearby states) in the system, so I have a list of inequalities specified. I'm implementing the switching by having two versions of the sytem matrix, A1 and A2, as well as B1 and B2 for the input matrix. I'm using conditional statements with the inequalities (If[]) to select which row of each I'm using in the relevant rows based on the values of the states and inputs. The implementation looks like this; here is A: (B is done similarly)

A[X_, u_] =
{If[inequalities[[1]] /. positionreplace, Evaluate@A2[[1]],
Evaluate@A1[[1]]],
A1[[2]],
A1[[3]],
If[inequalities[[2]] /. positionreplace, Evaluate@A2[[4]],
Evaluate@A1[[4]]],
If[inequalities[[3]] /. positionreplace, Evaluate@A2[[5]],
Evaluate@A1[[5]]],
A1[[6]],
A1[[7]],
If[inequalities[[4]] /. positionreplace, Evaluate@A2[[8]],
Evaluate@A1[[8]]]};


From which we form F:

F[X_, u_] := (A[X, u] /. replacementrules) . X + (B[X, u] /. replacementrules) . u;


And we set up the equation in NDSolve as:

sol = X[t] /. NDSolve[{X'[t] == F[X[t], u[t]], X[0] == X0}, X, {t, 0, maxtime}];


My problem is this: I've tried implementing this conditional row behavior using both Peicewise[] and If[] (where each row of the system (and input) matrix that switches is expressed as a conditional statement, and while it almost completely works as expected (i.e. the states reach their excursion limits, lock to the nearby one, and then come off), for a small portion of the simulation, the first state "sticks" - the governing inquality says that it should return to default behavior but it continues to remain "locked".

I know that the expression itself (with the conditional statements) is working correcty, because, as shown below, when I plot the trajectory in a slice of the state space with the vector equation for $$\dot{\mathbf{x}}$$ at its tip, we see very clearly that during the anomalous behavior $$\dot{\mathbf{x}}$$ is very large and perpendicular, indicating that for some period of time (between roughly t=3.5 to t=9) the solution from NDSolve is invalid despite the expression for xdot functioning correctly in another context (the blue regions are the inequality boundaries).

For whatever reason it seems to consistently be the first first switching inequality out of four doing this - not sure if it significant or not.