I want to evaluate the pendulum's motion with changing equilibrium state given by eqilValue[t]:
tauArr = {50, 100, 150, 200, 250}; (* points of changing, there may be a lot of them *)
(* that's why i need a function *)
eqilValue[t_] := (If[0 <= t < tauArr[[1]], eV = 0];
If[t >= tauArr[[Length[tauArr]]], eV = -5];
For[j = 1, j <= (Length[tauArr] - 1), j++,
{If[tauArr[[j]] <= t < tauArr[[j + 1]], eV = j]}];
Return[eV]
);(* equilibrium states, for example *)
gamma = 0.1;
kappa = 0.1;
sol = First[
f /. NDSolve[{f''[t] + gamma*f'[t] + kappa*(f[t] - eqilValue[t]) == 0,
f[0] == 0, f'[0] == 0}, f, {t, 0, 300}]];
Plot[eqilValue[t], {t, 0, 400}]
Plot[sol[t], {t, 0, 300}, PlotRange -> All]
This code throws an error when launched first time, otherwise shows a wrong behavior, just like there is the only final state:
(*NDSolve::ndnum: Encountered non-numerical value for a derivative at t == 0.`. >>
ReplaceAll::reps: {NDSolve[{0.01 (Times[<<2>>]+f[<<1>>])+0.1 (f^\[Prime])[t]+(f^ \[Prime]\[Prime])[t]==0,f[0]==0,(f^\[Prime])[0]==0},f,{t,0,300}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
*)

What am I doing wrong? Is there a better way to set up differential equations with discontinuities in the coefficients?



eqilValuethis way:eqilValue[t_?NumericQ] := (...). The need for and use of?NumericQis explained in this answer to the "Pitfalls" question and its links. – Michael E2 Nov 17 '14 at 21:15For, which lets folks transition more easily. But they don't work as well with the rest of M as other tools. – Michael E2 Nov 18 '14 at 13:00DSolve. – Michael E2 Nov 18 '14 at 15:01