0

Suppose I want to solve the following equation.

NDSolve[{y''[x] + x^2*y[x] == 0, y[0] == 1, y'[0] == 1}, 
 y[x], {x, 0, 10}]

I want to monitor the stepsize $\Delta x$ Mathematica is using at different x positions to solve this equation in whatever method it is using, be it Runge Kutta or BDF. How to monitor the timestep?

user157588
  • 261
  • 1
  • 6
  • 3
    What do you mean by monitor? Do you want to change something during the integration, or do you just want to know all the steps that were taken after the integration is complete? The latter can be inspected thus: sol = NDSolve[eqns, y (* not y[x]!!! *), {x, 0, 10}]; steps = y["Grid"] /. First[sol] // Flatten; stepsizes = Differences[steps]; – Michael E2 Mar 28 '21 at 16:40
  • 1
    Possibly related: https://mathematica.stackexchange.com/questions/102704/inspecting-step-size-and-order-of-tt-ndsolve – Michael E2 Mar 28 '21 at 17:08
  • 2
    Another meaning, akin to Monitor, that I use frequently: PrintTemporary@Dynamic@{Clock[Infinity], currenttime}; NDSolve[..., {x, 0, 10}, StepMonitor :> (currenttime = x)], although the OP's example will finish too quickly for this to be useful. – Michael E2 Mar 28 '21 at 17:17
  • 2
    ..or StepMonitor :> (currenttime = {x, x - First[#]} &@currenttime), which will give the current time and previous step size. – Michael E2 Mar 28 '21 at 18:21

1 Answers1

3

May be you could try this to print x and after that calculate the delta:

NDSolve[{y''[x] + x^2*y[x] == 0, y[0] == 1, y'[0] == 1}, 
 y[x], {x, 0, 10}, StepMonitor :> Print[{x, y[x]}]]
  • I'll just drop a little caveat that if one wants to apply this to another ODE, sometimes a solution takes hundreds or thousands of steps, and Print will lock up your front end for while and swamp your notebook with hundreds or thousands of new cells. – Michael E2 Mar 30 '21 at 19:17