I want to numerically solve a system of differential equations in Mathematica, say for example
$\frac{dx}{dt} = f(x,y,t) \\ \frac{dy}{dt} = g(x,y,t)$
for $x(t), y(t)$. However, the functions depend on previous values - lets say there occurs a time $t_0$ at which we have $f_0 = f(x(t_0),y(t_0),t_0)$ and $g_0 = g(x(t_0),y(t_0),t_0)$. The functions $f$ and/or $g$ are piecewise defined about and depend on this value.
Apologies if this is a poor and needlessly complicated example, but take:
$f(x,y,t) = x y \sin(t) \cos(t)$
$g(x,y,t) = \begin{cases} x \exp(-t) \text{ if } t\leq t_0 \\ g_0 + g_0 (t-t_0) \exp(t-t_0) \text{ if } t_0 < t \leq t_1 \\ g_1 + g_1 (t-t_1) \exp(-t) \text{ if } t > t_1 \end{cases}$
And consider for example that $t_0$ occurs where $x'(t) = 0$ and $t_1 = t_0 + 0.1$. Later in the solution you can see that $x$ has several maxima, so $t_0$ and $t_1$ need to be periodically updated. Here $g_0 = x(t_0) \exp(-t_0)$ and $g_1 = g_0 + g_0 (t_1 - t_0) \exp(t_1-t_0)$. Also make some initial conditions e.g. $x(0) = y(0) = 1$.
How do I best implement this in Mathematica? Here's my attempt, but it seems crude and I don't think it is functional:
f[x_, y_, t_] := x*y*Sin[t]*Cos[t];
g[t_, x_, t0_, g0_, t1_, g1_] :=
If[t <= t0, x*Exp[-t],
If[t <= t1, g0 + g0*(t - t0)*Exp[t], g1 + g1*(t - t1)*Exp[-t]]];
t0 = 100; t1 = 100; (* Just initially *)
eqns = {
x'[t] == f[x[t], y[t], t],
y'[t] == g[t, x[t], t0, g0, t1, g1],
x[0] == 1,
y[0] == 1,
WhenEvent[x'[t] == 0, g0 = g[t, x[t], t0, g0, t1, g1]; t0 = t; Print[t]],
WhenEvent[t == t0 + 0.1, t1 = t; g1 = g[t, x[t], t0, f0, t1, g1]; Print[t]]
};
sol = NDSolve[eqns, {x, y}, {t, 0, 10}];
xn[t_] := x[t] /. sol[[1]][[1]];
yn[t_] := y[t] /. sol[[1]][[2]];
Plot[{xn[t], yn[t]}, {t, 0, 10}]
Thanks for your help (and sorry for any confusion!)

