# Plotting several numerical solutions plus the analytic solution of ODE in one plot

I want to be able to plot several numerical solutions of an ODE, plus its analytical solution in one plot, in order to see how the numerical solutions converge towards the analytical one with respect to the number of steps. The method I'm using is Euler's method for the equation $y'(t) = 1-t +4y(t), y(0)=1$

The code I have so far is:

y[0]=1;
Do[y[n+1]=y[n]+0.01(1-0.01n+4y[n]), {n,0,99}]
y[100]


Is this doable? Thanks in advance for any help :)

• Both Plot and ListPlot will accept lists of functions/lists to plot. Nov 12, 2012 at 16:23
• And you can use Show to combine plots
– ssch
Nov 12, 2012 at 16:38
• For Euler's method: see this related question. Nov 12, 2012 at 16:49

Sascha showed you how to use the built-in "ExplicitEuler" option. You mention

I want to be able to plot several numerical solutions of an ODE, plus its analytical solution in one plot, in order to see how the numerical solutions converge...

Here's one way to generate "Lady Windermere's fan":

yTrue = y /. First @ DSolve[{y'[t] == 1 - t + 4 y[t], y[0] == 1}, y, t];

pts = Table[
With[{ya = y /. First @
NDSolve[{y'[t] == 1 - t + 4 y[t], y[0] == 1}, y, {t, 0, 1},
Method -> "ExplicitEuler", StartingStepSize -> 2^-k]},
Transpose[Append[ya["Coordinates"], ya["ValuesOnGrid"]]]], {k, 1, 5}];

Show[Plot[yTrue[t], {t, 0, 1}, PlotStyle -> Directive[Thick, Dashed, Gray]],
ListPlot[pts, Joined -> True, Mesh -> All, PlotMarkers -> Automatic],
Axes -> None, Frame -> True]


Here, the gray dashed curve is the solution obtained from DSolve[].

Instead of implementing an explicit Euler method on your own, you could as well use the built-in option of NDSolve: (the value following StartingStepSize is your actual step size for the whole method since "ExplicitEuler" has no adaptive step size):

AnalyticalSolution = DSolve[{y'[t] == 1 - t + 4*y[t], y[0] == 1}, y, t]
NumericalSolution = NDSolve[{y'[t] == 1 - t + 4*y[t], y[0] == 1}, y, {t, 0, 10},
Method -> "ExplicitEuler", StartingStepSize -> 0.01]
Plot[{y[t] /. NumericalSolution, y[t] /. AnalyticalSolution}, {t, 0, 10}]

• Awesome, thanks! Nov 12, 2012 at 16:55
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://mathematica.stackexchange.com/questions/14458/plotting-several-numerical-solutions-plus-the-analytic-solution-of-ode-in-one-pl", "fetch_time": 1713965927000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-18/segments/1712296819273.90/warc/CC-MAIN-20240424112049-20240424142049-00440.warc.gz", "warc_record_offset": 346623004, "warc_record_length": 40237, "token_count": 693, "char_count": 2185, "score": 3.5, "int_score": 4, "crawl": "CC-MAIN-2024-18", "snapshot_type": "latest", "language": "en", "language_score": 0.8277794122695923, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00061-of-00064.parquet"}