# Possible problem with method of resolution of differential equation

I'm trying to simulate an elevator circuit. I divided the problem in two steps. First step: simulate the problem with $d(t)$ and $e(t)$ constant. The final conditions of this system will be the initial conditions for the second step.

L = 5*10^(-3);
do = 0.5;
c = 200*10^(-6);
eo = 6;
R = 12;
\[CapitalDelta]e = 1;
\[CapitalDelta]d1 = -0.2;
\[CapitalDelta]d2 = 0.2;
e = eo;
T = 0.002;
d[t_] = do;
dien[t_] := t/T*(UnitStep[t] - UnitStep[t - T]);
Sierra[t_] := dien[Mod[t, T]];
S[t_] := Piecewise[{{1, d[t] > Sierra[t]}, {0, d[t] < Sierra[t]}, {0,
d[t] == Sierra[t]}}];
eqn1 := x1'[t] == -x1[t]/(R*c) + 1/c*x2[t]*(1 - S[t]);
eqn2 := x2'[t] == -1/L*x1[t]*(1 - S[t]) + 1/L*e;
sol1 = NDSolve[{eqn1, eqn2, x1[0] == 0, x2[0] == 0}, {x1, x2}, {t, 0,
0.1}, Method -> "ExplicitRungeKutta"]
Zin1 = x1[0.1] /. sol1
Zin2 = x2[0.1] /. sol1


(Until here, all is ok)

Second step: this time $d(t)$ and $e(t)$ are functions of time and I'm using the initial conditions listed above.

dd[t_] = do + \[CapitalDelta]d1*UnitStep[t - 0.01] + \[CapitalDelta]d2*
UnitStep[t - 0.04];
dienn[t_] := t/T*(UnitStep[t] - UnitStep[t - T]);
Sierraa[t_] := dienn[Mod[t, T]];
ee[t_] = eo + \[CapitalDelta]e UnitStep[t - 0.07];
Sw[t_] :=
Piecewise[{{1, dd[t] > Sierraa[t]}, {0, dd[t] < Sierraa[t]}, {0,
dd[t] == Sierraa[t]}}];
eqn11 = x11'[t] == -x11[t]/(R*c) + 1/c*x22[t]*(1 - Sw[t])
eqn22 = x22'[t] == -1/L*x11[t]*(1 - Sw[t]) + 1/L*ee[t]
sol2 = NDSolve[{eqn11, eqn22, x11[0] == Zin1, x22[0] == Zin2}, {x11,
x22}, {t, 0, 0.1}]
Show[Plot[Sierraa[t], {t, 0, 0.1}, Exclusions -> None],
Plot[dd[t], {t, 0, 0.1}, Exclusions -> None]]
Show[Plot[ee[t], {t, 0, 0.1}, Exclusions -> None,
PlotRange -> {{0, 0.1}, {0, 10}}],
Plot[5 Sw[t], {t, 0, 0.1}, PlotPoints -> 200, Exclusions -> None]]
Plot[Evaluate[x11[t]] /. sol2, {t, 0, 0.1}, PlotPoints -> 200,
PlotRange -> {{0, 0.1}, {-2, 15}}]


The parameters used here are the same of step 1. The graph for $d(t)$, $Sw(t)$ (switch) and $e(t)$ shows the functions are ok. The problem is with the final solution, when I try to graph Voltage and Current (x11[t] and x22[t]). I get this:

but I'm supposed to get this:

So, it seems the problem is the equations (maybe STIFF? ). I tried using RungeKutta but the problem is still there. Any ideas?

note: I'm sorry for the spanish used in the code.It would have been a hard work translate all the code to english.

• Is there a typo in the code? What is dd[]? – Michael E2 Aug 9 '17 at 0:41
• I'm sorry. Typo just when I did copy-paste the code. Actually dd[t]=d[t]. – Miguel Duran Diaz Aug 9 '17 at 0:43

I think there's a bug that makes the integration bail out early (at t == 0.01) without any warning message. You should report it to WRI.

Here is another workaround:

sol2 = NDSolve[{eqn11, eqn22, x11[0] == First@Zin1,
x22[0] == First@Zin2}, {x11, x22}, {t, 0, 0.1},
Method -> {"DiscontinuityProcessing" -> False}]


(The First in First@Zin1 and First@Zin2 is optional. It depends on whether you really want a 1D vector or a simple scalar as output.)

I got it using MaxStepFraction, Precision goal and AccuracyGoal.

sol2 = NDSolve[{eqn11, eqn22, x11[0] == 13.6525,
x22[0] == 1.25255}, {x11, x22}, {t, 0, 0.1}, AccuracyGoal -> 0.001,
PrecisionGoal -> 0.001, MaxStepFraction -> 1/1000]