I am trying to simulate a pulse width modulated signal in a NDSolve, but i have a hard time passing the signal function in. This is my code:
(* Arbitrary test circuit *)
components = {r iR[t] == vR[t], l iL'[t] == vL[t], iC[t] == c vC'[t]};
connections = {vR[t] + vL[t] + vC[t] - v1[t] == 0,
iL[t] == iR[t], iR[t] == iC[t]};
ic = {iR[0] == 0, iL[0] == 0, iC[0] == 0, vR[0] == 0,
vL[0] == 0, vC[0] == 0, iR'[0] == 0, iL'[0] == 0,
iC'[0] == 0, vR'[0] == 0, vL'[0] == 0, vC'[0] == 0};
params = {r -> 5, l -> 10^-2, c -> 10^-4};
signal = {1, 0.1, 0.1, 0.2, 0, 1, 0, 1, 0, 0.4, 0.4, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
(* Problem in this definition*)
v1[t_] := If[
Mod[t, 1/100]*100 > signal[[ IntegerPart[100.0*t + 1] ]]
, 1, 0
];
(* solve does thus not work*)
sol = NDSolve[
{components, connections, ic} /. params,
vC, {t, 0.0, 0.2}
];
I feel like i'm close but still so far away. The error seems to indicate that
...0,0,0,0,0,0,0,0,0}[[1/2]]] the error is in the Part lookup. How can this be im asking for the IntegerPart? Should i be using some other function for this. Tried wrapping a N in the expression.
Also any ideas how i could make the signal function smooth step would be great.



