Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why do I get a smooth curve (that is obviously wrong) when I run this?

Plot[
 Evaluate[
  NDSolveValue[{x'[t] == -x[t], x[0] == 1}, {x[t]}, {t, 0, 1}, 
   StartingStepSize -> 1, Method -> {"FixedStep", Method -> "ExplicitEuler"}]],
 {t, 0, 1},
 PlotRange -> All]

Shouldn't I just get back a straight line connecting (0, 1) to (1, 0)?!

How do I make Mathematica just do a plain classic Forward Euler?! I don't want fancy smoothing...

share|improve this question
up vote 3 down vote accepted

You have to set MaxStepFraction, too, say, to 1.

Plot[
 Evaluate[
  NDSolveValue[{x'[t] == -x[t], x[0] == 1}, {x[t]}, {t, 0, 1}, 
   StartingStepSize -> 1, Method -> {"FixedStep", Method -> "ExplicitEuler"},
   MaxStepFraction -> 1]],
 {t, 0, 1},
 PlotRange -> All]

Mathematica graphics

It's not a straight line because the value of the derivatives are stored & used in the InterpolatingFunction solution. However, we can see there are only two points.

sol /. t -> "Grid"
(*  {{{0.}, {1.}}}  *)
share|improve this answer
    
+1 Ahhh! Is there any way to make it draw without smoothing then? It's so misleading the way it draws it... – Mehrdad Feb 15 at 2:41
    
@Mehrdad Oops, made a mistake editing the comment. Try sol1 = Interpolation[Table[{t, sol}, {t, Flatten[sol /. t -> "Grid"]}], InterpolationOrder -> 1]. – Michael E2 Feb 15 at 2:57
    
Awesome, got it! It ended up being sol = Evaluate[ NDSolveValue[{x'[t] == -x[t], x[0] == 1}, x[t], {t, 0, 1}, StartingStepSize -> 1, MaxStepFraction -> 1, Method -> {"FixedStep", Method -> "ExplicitEuler"}]]; sol = Interpolation[Table[{t, sol}, {t, Flatten[sol /. t -> "Grid"]}], InterpolationOrder -> 1][t]; Plot[sol, {t, 0, 1}, PlotRange -> All]. – Mehrdad Feb 15 at 3:03
    
PS, this might be more painful but for future reference it might be nice to add how to do it if we're solving for multiple values (e.g. {x[t], x'[t]}). – Mehrdad Feb 15 at 3:06
    
@Mehrdad I'm not sure what you mean, actually. But, one can plot a linear interpolation with ListLinePlot[sol = NDSolveValue[{x'[t] == -x[t], x[0] == 1}, x, {t, 0, 1}, StartingStepSize -> 1, Method -> {"FixedStep", Method -> "ExplicitEuler"}, MaxStepFraction -> 1]], even though sol is still order 3. ListPlot and ListLinePlot show only the steps, or interpolated points, in an InterpolatingFunction. – Michael E2 Feb 15 at 3:30

I would suggest (as an alternative) the following

pointsAndValues[x_InterpolatingFunction] :=
  Transpose[{First[x["Coordinates"]], x["ValuesOnGrid"]}];

ListPlot[
  pointsAndValues@
    First@NDSolveValue[{x'[t] == -x[t], x[0] == 1}, {x}, {t, 0, 3}, 
      StartingStepSize -> 1, 
      Method -> {"FixedStep", Method -> "ExplicitEuler"}, 
      MaxStepFraction -> 1], Joined -> True, PlotMarkers -> Automatic]

And to check the convergence use e.g.

ListPlot[pointsAndValues@
  First@NDSolveValue[{x'[t] == -x[t], x[0] == 1}, {x}, {t, 0, 2}, 
    StartingStepSize -> 1/#, 
    Method -> {"FixedStep", Method -> "ExplicitEuler"}, 
    MaxStepFraction -> 1] & /@ {1, 2, 4, 16}, Joined -> True, 
 PlotMarkers -> Automatic]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.