3
$\begingroup$

I am an educator trying to teach Euler's method to numerically approximate ODEs. I want to use Mathematica to generate somewhat small examples, i.e. a large step size. When I run

f = NDSolveValue[{y'[x] == y[x]^2, y[0] == 1}, y, {x, 0, 2}, StartingStepSize -> .5, Method -> {"ExplicitEuler"}, StepMonitor :> Print[{x, y[x]}]] 

I get the output

{0.2,1.2} {0.4,1.488} {0.6,1.93083} {0.8,2.67645} {1.,4.10912} {1.2,7.48611} {1.4,18.6945} {1.6,88.591} {1.8,1658.26} {2.,551627.} 

In other words, Mathematica appears to override my intentional choice of step size 0.5 with its own step size of 0.2. Is there any way to force Mathematica to use my desired step size?

By the way, when I make the step size smaller than 0.2, it works as expected:

f = NDSolveValue[{y'[x] == y[x]^2, y[0] == 1}, y, {x, 0, 2}, StartingStepSize -> .1, Method -> {"ExplicitEuler"}, StepMonitor :> Print[{x, y[x]}]] 

Gives

{0.1,1.1} {0.2,1.221} {0.3,1.37008} {0.4,1.5578} {0.5,1.80047} {0.6,2.12464} {0.7,2.57605} {0.8,3.23965} {0.9,4.28919} {1.,6.1289} {1.1,9.88524} {1.2,19.657} {1.3,58.2969} {1.4,398.15} {1.5,16250.5} {1.6,2.64241*10^7} {1.7,6.98233*10^13} {1.8,4.87529*10^26} {1.9,2.37685*10^52} {2.,5.64941*10^103} 
$\endgroup$
5
  • $\begingroup$ Maybe method is not stable for large step size. $\endgroup$ Commented Oct 10, 2023 at 23:15
  • 1
    $\begingroup$ Thank you for the comments. I'm not sure I understand what I should look at in the documentation of "FixedStep". I see the following sentence "..."ExplicitEuler" method has no adaptive step size control. Therefore, the integration is already carried out using fixed step sizes so the specification of "FixedStep" is unnecessary..." The example it gives is NDSolve[system, StartingStepSize -> 1/10, Method -> "ExplicitEuler"]; which is what I was trying to follow. $\endgroup$ Commented Oct 11, 2023 at 1:47
  • $\begingroup$ @ can be used to notify a user that you replied. For example @xzczd . $\endgroup$ Commented Oct 11, 2023 at 2:19
  • $\begingroup$ Oh… so I didn't remember it right, sorry for that. (Close vote retracted. ) $\endgroup$ Commented Oct 11, 2023 at 4:57
  • $\begingroup$ I also use the following code to plot the first-order interpolation (rather than the default 3rd order): f = NDSolveValue[{y'[x] == y[x]*Cos[x], y[0] == 1}, y, {x, 0, 2}, StartingStepSize -> .5, MaxStepFraction -> 1, Method -> {"ExplicitEuler"}]; data = Table[{x, f[x]}, {x, f["Coordinates"][[1]]}]; F = Interpolation[data, InterpolationOrder -> 1]; G = DSolveValue[{y'[x] == y[x]*Cos[x], y[0] == 1}, y, x]; Show[ VectorPlot[{1, y*Cos[x]}, {x, 0, 2}, {y, 0, 3}, VectorColorFunction -> None, VectorStyle -> {Black, Arrowheads[0]} ], Plot[{G[x], F[x]}, {x, 0, 2}] ] $\endgroup$ Commented Oct 11, 2023 at 16:02

1 Answer 1

4
$\begingroup$

Looking at help it seems you also need MaxStepFraction

f = NDSolveValue[{y'[x] == y[x]^2, y[0] == 1}, y, {x, 0, 2}, StartingStepSize -> .5, MaxStepFraction -> 1/4, Method -> {"ExplicitEuler"}, StepMonitor :> Print[{x, y[x]}]] 

Mathematica graphics

Used 1/4 since the length is 2 and you wanted each step to be 0.5, so this means max step fraction is 1/4 of total length.

$\endgroup$
2
  • $\begingroup$ Interesting. I didn't know MaxStepFraction overrides MaxStepSize and StartingStepSize. $\endgroup$ Commented Oct 11, 2023 at 5:02
  • $\begingroup$ Great! As a bad hack I can also set MaxStepFraction -> 1 and don't have to change MaxStep Fraction even to do weird step sizes. $\endgroup$ Commented Oct 11, 2023 at 5:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.