MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Programming Euler's Method into Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82186] Re: Programming Euler's Method into Mathematica
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Sun, 14 Oct 2007 06:11:55 -0400 (EDT)
  • References: <fepva6$ik4$1@smc.vnet.net>

Hi,

may be it help you:


Clear[EulerStep, EulerHeunStep, SimpleNDSolve]

EulerStep[y_] := y + h*(f @@ y)

EulerHeunStep[y_] := Module[{yast, k1},
   k1 = f @@ y;
   yast = y + h*k1;
   y + h*((f @@ yast) + k1)/2
   ]

SimpleNDSolve[eqns : {_Equal ..}, init_,
   var_List, {t_, t0_, step_, n_Integer}, method_: EulerStep] :=
  Block[{dvar, h = step, f},
   dvar = D[#, t] & /@ var;
   f = First[dvar /. Solve[eqns, dvar]];
   f = Function @@ ({var, f} /. a_[t] :> a);
   Prepend @@@ Transpose[{
      NestList[method, init, n], Table[t0 + h*i, {i, 0, n}]}]
   ]

and call it with

eusol1 = SimpleNDSolve[{q'[t] == p[t], p'[t] == -q[t]}, {0.0,
     1.0}, {q[t], p[t]}, {t, 0, 0.1, 180}, EulerStep];

Regards
   Jens


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]==0,n[0]==1, m[0]==2, h[0]==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]
> 
> 


  • Prev by Date: Re: How to do count for sub list?????
  • Next by Date: Re: import function
  • Previous by thread: Re: Programming Euler's Method into Mathematica
  • Next by thread: Inset a 3D Graphic