Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

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

Search the Archive

NestList with two lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg131840] NestList with two lists
  • From: Joao <joaopereira9 at gmail.com>
  • Date: Wed, 16 Oct 2013 04:59:36 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net

Hi 

I use a runge kutta 4 algorithm and apply it to a discretized problem in which "time input" is given as a the number of the node (integer).

As the rk4 evaluates a function also in the mid of the interval my input "time" is a even number of nodes. 
0, 2, 4, ......In order to allow evaluation at 1, 3 ...
I do not have the function defined outside the nodes and the nodes are prefixed.

I do need however, in another part of my model to have the function evaluated in all nodes (even  and odd). I performed a change to my algorithm (Source: jason cantarella's  http://www.jasoncantarella.com/)

This version I have know is working but I wonder if it could be done better by using NestList more effectively.

rk4new[f_, tini_, tf_, xini_, n_] := Module[{h, list, step, oddnodes, evennodes},
Clear[h, list, step, oddnodes, evennodes];

h = (tf - tini)/n;
list = {{tini, xini}}

step[{t_, x_}] := Module[{k1, k2, k3, k4},
k1 = h * f[t, x];
k2 = h * f[t + h / 2, x + k1 / 2];
k3 = h * f[t + h / 2, x + k2 / 2];
k4 = h * f[t + h, x + k3];
oddnodes = AppendTo[list, {t + h / 2, x + 1 / 24 (5 k1 + 4 k2 + 4 k3 - k4)}];
list = oddnodes;
{t + h, x + 1 / 6 (k1 + 2 k2 + 2 k3 + k4)}];

evennodes = NestList[step, {tini, xini}, n];

Drop[Riffle[oddnodes, evennodes], 1]
];

I would like the NestList function produce the oddnodes and evennodes simultaneously, I thought of defining the final line in step as two lists, the problem is that both evaluate with the same (single) argument. 

I haven't been able so far to figure out a way to do it. I believe the speed may by increased slightly and since this algorithm is going to be ran thousands of times it could be useful.

I would appreciate some help on this issue.

Thanks in advance

Joao



  • Prev by Date: Exporting "Speak" to an audio file?
  • Next by Date: How do I share Mathematica notebooks in Skype or Google Video?
  • Previous by thread: Re: Exporting "Speak" to an audio file?
  • Next by thread: How do I share Mathematica notebooks in Skype or Google Video?