Euler’s method with Mathematica

We use Euler’s method to find an approximate solution to
\displaystyle \frac{dP}{dt} = (1+t)P^2
with initial value P(0)=3

First, we set \Delta t = 0.05

deltat = 0.05;

(The semi colon just suppresses the output.)

Next we define the function f(t,P) = (1+t)P^2

f[t_, P_] := (1 + t) P^2;

Now we set up the recursion relation. We want the initial values to be
t_0 = 0 and P_0 = 3
and the rest of the values to be given by
t_{k+1} = t_k + \Delta t and P_{k+1} = P_k + \Delta t * f(t_k, P_k)
The code which makes a table of such values is this

values = RecurrenceTable[{
   t[k + 1] == t[k] + deltat,
   P[k + 1] == P[k] + deltat*f[t[k], P[k]],
   t[0] == 0,
   P[0] == 3
   }, {t, P}, {k, 0, 10}]

Here we only have the first 10 time steps.

To make a list of values, we use the code

Grid[values]

To make a plot, use the code

ListLinePlot[values, PlotMarkers -> Automatic]
Advertisements
This entry was posted in Differential equations, Mathematica. Bookmark the permalink.

Leave a comment here

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s