I am trying to simulate the following hybrid ode
$$ \begin{eqnarray} \dot{x}=-\left(x-a+u\right) \end{eqnarray} $$
where $u$ is given by
$$ \begin{eqnarray} u(t)=\left\{\begin{matrix} 1 & \text{if} & x-x_d>\delta\\ 0 & \text{if} &x-x_d \leq -\delta\\ u(t-dt) & \text{otherwise} \end{matrix}\right. \end{eqnarray} $$
where $a=10$, $x_d=9.5$ and moreover $u(t-dt)$ represent the value of $u$ in the last time step. $a$ and $x_d$ are known constants. I didnt know how to impliment this in mathematica.
PS: I was able to solve this with $\delta=0$. The controller simply becomes
$$ \begin{eqnarray} u(t)=\left\{\begin{matrix} 1 & if & x-x_d>0\\ 0 & if &x-x_d \leq 0\\ \end{matrix}\right. \end{eqnarray} $$
a = 10;
xd = a - 0.25
simtime = 5;
var = {x[t]} // Flatten;
varInit = var /. {t -> 0};
f = -(x[t] - a + 0.5 (1 + Sign[(x[t] - xd)]));
DAE = {Thread[D[var, t] == f],
Thread[varInit == Flatten[RandomReal[{1, 2}, {1, 1}]]]} // Flatten;
sol = NDSolve[DAE, var, {t, 0, simtime}, MaxSteps -> 10000000];
Plot[Evaluate[{x[t], If[x[t] > xd, 1, 0]} /. First[sol]], {t, 0,
simtime}, PlotStyle -> {Red, Green, Blue},
PlotRange -> {{0, simtime}, {0, 12}}, AxesLabel -> {t, \[Theta]},
PlotLabels -> Placed[{\[Theta][t] u[t]}, Above]]
Evaluate[{x[t]} /. First[sol] /. t -> simtime]
But I would like to implement this with a non zero $\delta$. For instance one can use a delta of $\delta=0.25$.
NDSolve::underdet
message "There are more dependent variables, {u[t],x[t]}, than equations, so the system is underdetermined"; in case my failed attempt was due to my erroneous equation setup, perhaps WhenEvent might seem useful $\endgroup$WhenEvent
; If you're certain your system is correctly specified then I suggest you play around withWhenEvent
until you discover a working specification. I could be wrong, but I don't thinkIf
can be helpful in this case. $\endgroup$