       Re: Programming Euler's Method into Mathematica

• To: mathgroup at smc.vnet.net
• Subject: [mg82181] Re: [mg82167] Programming Euler's Method into Mathematica
• From: Murray Eisenberg <murray at math.umass.edu>
• Date: Sun, 14 Oct 2007 06:09:21 -0400 (EDT)
• Organization: Mathematics & Statistics, Univ. of Mass./Amherst
• References: <200710130803.EAA17824@smc.vnet.net>

```The following code, which uses a slightly different programming
paradigm, implements Euler's Method for a system:

euler[F_, a_, Y0_, b_, n_] :=
Module[{t, Y, h = (b - a)/n, i},
t = a; Y = Y0;
Do[
t[i] = a + i h;
Y[i] =  Y[i - 1] + h F[t[i - 1], Y[i - 1]],
{i, 1, n}
];
Table[{t[i], Y[i]}, {i, 0, n}]
]

And the usage messsage is:

euler::usage = "euler[F, t0, Y0, b, n] gives the numerical solution\n
to {Y' == F[t, Y], Y[t0] == Y0} over the interval\n
[t0, b] by the n-step Euler's method.  The result\n
is in the form of a table of {t, Y} pairs.";

Note that this function uses an exact increment h rather than converting
it explicitly to numeric form using N.  Of course you can readily change
that or accomplish the corresponding thing by giving an approximate
number for one of the endpoints.

Tara.Ann.Lorenz at gmail.com wrote:
> I am trying to use Euler's Update Method to Numerically Integrate a
> Function (I know how to use NDSolve for this case, but I want to
> demonstrate an approximation method as well).  For those familiar with
> neuroscience, I am trying to model the Hodgkin Huxley Equations.  I
> have four ordinary differential equations (ODE) that I have written in
> discrete form  using Euler's Method.  I am trying to solve for one of
> these discrete equations, V, which is a function of the other three
> differential equations (n, m, and h). All four equations are also a
> function of time. Essentially, the command for NDSolve for this case
> is in the following form: NDSolve[{ODE1, ODE2, ODE3, ODE4,
> V==0,n==1, m==2, h==3},{V,n,m,h},{t,0,50},MaxSteps->1000]
>
> Instead of the above ODE input, I want to use Euler's Method. (i.e. V(t
> +delta_t)=V(t)+delta_t("stuff").  Now, the code below works for a
> function of x and y. MY QUESTION IS: how would I change this code to
> apply to my situation in which I want to solve for V[t] but it is a
> function of n[t], m[t], and h[t].
>
> I would appreciate any hints/clues/suggested programs.  I know that
> Mathematica has a built-in "Explicit Euler" Method, but I would like
> to develop the program myself. I believe I am making a small mistake
> that is preventing my success.
>
> Thank you,
> Tara
>
> The following program code works for a function f(x,y):
> euler[f_,{x_,x0_,xn_},{y_,y0_},steps_]:=
>
> Block[{	xold=x0,
>         yold=y0,
>         sollist={{x0,y0}},
>         x,y,h
>       },
>
>       h=N[(xn-x0)/steps];
>
>       Do[ xnew=xold+h;
>           ynew=yold+h*(f/.{x->xold,y->yold});
>           sollist=Append[sollist,{xnew,ynew}];
>           xold=xnew;
>           yold=ynew,
>           {steps}
>       ];
>
>       Return[sollist]
>
>

--
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305

```

• Prev by Date: Re: import function
• Next by Date: Re: Can Integrate[expr,{x,a,b}] give an incorrect result?
• Previous by thread: Programming Euler's Method into Mathematica
• Next by thread: Re: Programming Euler's Method into Mathematica