My code is based on the similarity transformation X=VZ
.I simulate the model for transformed equations involving Z
by replacing the state space modelAX+BU
and RX+SU
with transformed equations where X is replaced with VZ
and WZ
resp then I apply transformation again to obtain X back. I want to execute the following Euler code using solve_ivp. Although I'm able to frame the basic equations but not able to access the solution array at each time step as in Euler to obtain X
back from Z
.
X
and Z
are simply the state variable matrices of order 4*1
.
My euler code is :
for i in range(0,500000):
if (w== 0 and X[1]> vdon) or (w==1 and X[0]> 0):
zdot=inv(V)*A*V*Z+inv(V)*B*U
Z(i+1)=Z(i)+ h*zdot
w=1
X(i+1)=V*Z(i+1)
else:
zdot=inv(W)*R*W*Z+inv(W)*S*U
Z(i+1)=Z(i)+ h*zdot
w=0
X(i+1)=W*Z(i+1)
where V
and W
are the eigen vector matrices of A
and R
respectively obtained using
e1,V=LA.eig(A)
e2,W=LA.eig(R)
In scipy's solve_ivp I define the function as
def conv(t,Z):
if (w==0 and X[1]>vdon) or (w==1 and X[0]>0):
zdot=inv(V)*A*V*Z+inv(V)*B*U
w=1
else:
zdot=inv(W)*R*W*Z+inv(W)*S*U
w=0
return zdot
The if
condition shown here doesn't work as expected and has been shown just for code understanding.
and I define the solver equations as:
w=0 #intially
sol= solve_ivp(conv, tspan1,X0)
aa1=sol.t
bb1=sol.y
But I'm unable to define X=W*Z
or X=V*Z
at each solver time step during run time.
Z
andX
. $\endgroup$sw
in the branches of theif
condition. Could you game through and report on what should happen ifX[1]>vdon
is true and the second is false, that is,X[0]<0
? Then you are in a state of artificial oscillation between the phases, where the oscillation frequency depends on the stepsize $h$ and not on anything internal to the model. $\endgroup$sw
changes as well.The logical operation used isor
.IfX[1]>vdon and sw==1
i.e. this condition is true and the other one if false, there would be no switching between the phases and thezdot=inv(V)*A*V*Z+inv(V)*B*U
condition would be executed. $\endgroup$sw==1
, then the first clause of the first condition is false andX[1]>vdon
does not get tested. Now ifX[0]<0
then the second condition is false too and theelse
branch is entered wheresw=0
is set. Now in the next step the second condition is always wrong, onlyX[1]>vdon
gets tested, and if true then thethen
branch is entered andsw=1
is set. As the changes inZ
and thus the computed values ofX
in each phase are small, this phase alternating behavior can persist for a while. This is a generalization of "sliding mode" behavior. $\endgroup$