I want to solve the following ODE using NDSolveProcessEquations but in an iterative way.

$\ddot{x}(t)+5\dot{x}(t)+3x(t)=2\cos(2\pi t)$


I wrote the below code

    dxdt0 = 0; x0 = 1; 
    ti = 0; tf = 10; \[Delta]t = 0.01; 
    ItrNo = Round[(tf - ti)/\[Delta]t]; acc = 12; 
    eq1 = Derivative[2][x][t] + 5*Derivative[1][x][t] + 3*x[t] == 2*Cos[2*Pi*t]; 
    ics = {Derivative[1][x][0] == dxdt0, x[0] == x0}; 
    StateVar = First[NDSolve`ProcessEquations[{eq1, ics},{Derivative[1][x], x}, t, MaxSteps -> Infinity,PrecisionGoal -> acc, AccuracyGoal -> acc]]; 
    solC = ConstantArray[0, ItrNo - 1];
    Do[ics = {Derivative[1][x][0] == dxdt0, x[0] == x0}; 
        NewStateVar = First[NDSolve`Reinitialize[StateVar,ics]]; 
        NDSolve`Iterate[NewStateVar, \[Delta]t]; 
        solUC = NDSolve`ProcessSolutions[NewStateVar]; 
        x0 = x[t] /. {t -> \[Delta]t} /. solUC; 
        dxdt0 = Derivative[1][x][t] /. {t -> \[Delta]t} /. solUC; 
        solC[[i]] = Flatten[{dxdt0, x0}]; 
        If[Mod[Rationalize[\[Delta]t]*i, Rationalize[1]] == 0, 
        Print["  t = ", \[Delta]t*i]], {i, ItrNo - 1}]    

The plots were generated using

tData = Table[n, {n, \[Delta]t, tf - \[Delta]t, \[Delta]t}]; 
xData = solC[[All, 2]]; 
dxdtData = solC[[All, 1]]; 
xList = Partition[Riffle[tData, xData], 2]; 
dxdtList = Partition[Riffle[tData, dxdtData], 2]; 
ListLinePlot[{xList, dxdtList}, PlotRange -> All, Frame -> True, 
   FrameStyle -> Directive[Black, Thick]]

And I got some results

enter image description here

To validate the result I checked with DSolve and NDSolve and instantly understood that they had produced the correct result, which obviously different from what NDSolveProcessEquations had produced.

   dxdt0 = 0; x0 = 1; 
    eq = Derivative[2][x][t] + 5*Derivative[1][x][t] + 3*x[t] == 
    Ics = {Derivative[1][x][0] == dxdt0, x[0] == x0}; 
    sol = First[DSolve[{eq, Ics}, x[t], t]]; 
    y[t] = D[x[t] /. sol, t]; 
    Plot[{y[t], x[t] /. sol}, {t, 0, 10}, PlotRange -> All, Frame -> True, 
       FrameStyle -> Directive[Black, Thick]]

enter image description here

sol1 = First[
   NDSolve[{eq, Ics}, {Derivative[1][x][t], x[t]}, {t, 0, 10}]]; 
Plot[{Derivative[1][x][t] /. sol1, x[t] /. sol1}, {t, 0, 10}, 
 PlotRange -> All, 
   Frame -> True, FrameStyle -> Directive[Black, Thick]]

enter image description here

What I have understood is that the term $2\cos(2\pi t)$ (where time $t$ appears explicitly ) is the main reason for two different results. In my code I have failed to incorporate the fact that with every iteration, time $t$ grows and this changes the value of function $2\cos(2\pi t)$ at every iteration steps. I did not also want to include NDSolveProcessEquations within the loop.

Looking forward for any valuable help.


closed as off-topic by xzczd, Johu, Henrik Schumacher, MarcoB, bbgodfrey Sep 29 '18 at 17:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – xzczd, Johu, Henrik Schumacher, MarcoB, bbgodfrey
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Why not skip reinitialization and use NDSolveIterate[StateVar, [Delta]t * i]` at each step? $\endgroup$ – Michael E2 Sep 26 '18 at 10:32
  • $\begingroup$ Actually, in a separate problem, I need to correct state variables, (i.e., x and dxdt) at each integration step, using some correction formula. That corrected state variables are being fed as new initial conditions at next integration step. That is why I reinitialize the state variables at each step. $\endgroup$ – Soumyajit Roy Sep 26 '18 at 10:41
  • $\begingroup$ @MichaelE2 It also did not work. The solutions became straight lines only. $\endgroup$ – Soumyajit Roy Sep 26 '18 at 10:51
  • 2
    $\begingroup$ I got this, which seems to have the derivative and x switched from your images. Sounds like you plugged in the same t at each step instead of dt * i. Your description of what's wrong suggests this immediately: At each step put the IC at the last t and iterate to t + dt. $\endgroup$ – Michael E2 Sep 26 '18 at 11:21
  • 1
    $\begingroup$ @michaelE2 is right. For completeness here's the corrected code (the corrected part has been marked): i.stack.imgur.com/XSO6X.png I'm voting to close this question because it's due to a simple mistake. $\endgroup$ – xzczd Sep 26 '18 at 13:17