(* Some examples of solving Ordinary Differential Equations (ODE) *) (* Example 1. Population dynamics. Most simple model. increase rate ~ current population, birth rate = const *) solution = NDSolve[{x'[t] == x[t], x[1] == 1}, x, {t, 0, 10}] Plot[Evaluate[x[t] /. solution ], {t, 0, 10}, PlotLabel -> FontForm["solution", {"Courier", 15}], AxesLabel -> {t, x[t]}] (* As usual, trouble is right around the corner *) (* First try x[-2] == -2.719, then set x[-2] == -2.717 and see what happens *) (* Very unstable solution, a good model for "Why we can't predict the weather longer than 5 days ahead" *) solution1 = NDSolve[{x'[t] == x[t]*x[t] - t, x[-2] == -2.719}, x, {t, -2, 6}] Plot[Evaluate[x[t] /. solution1 ], {t, -2, 6}, AxesLabel -> {t, x[t]}] (* Here is a better population growth model. Saturation effects included, birth rate decreases as the population increases: "overcrowding". This is the so-called "Logistic equation" *) (* Set the re-normalized birth rate a= *) a=1.0 solution2 = NDSolve[{x'[t] == a(x[t] - x[t]*x[t]), x[0] == 0.1}, x, {t, 0, 5}] plot2 = Plot[Evaluate[x[t] /. solution2 ], {t, 0, 5}, AxesLabel -> {t, x[t]}] (* Note that regardless of the initial condition, solutions of the logistic equation above asymptotically approach the same value x =1. This is said to be a "stable fixed point" or "attractor" or "stable stationary point". A "fixed point" is the one for which x' = 0; it may be stable (solutions converge to it) or unstable (solutions diverge away from it *) (* Even for something as simple and stable as the above, one has to choose numerical method carefully: for example, the simple Euler's method may give a completely wrong solution. In modern practice, this method is almost never used as a stand-alone routine. However, it is very useful from the pedagogical stand point. *) solution3 = NDSolve[{x'[t] == a(x[t] - x[t]*x[t]), x[0] == 0.1}, x, {t, 0, 5}, Method->ExplicitEuler] plot3 = Plot[Evaluate[x[t] /. solution3 ], {t, 0, 5}, AxesLabel -> {t, x[t]}] Show[plot2, plot3 ]