Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #16066
| From | Joao <joaopereira9@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | NestList with two lists |
| Date | 2013-10-16 08:59 +0000 |
| Message-ID | <l3lkh4$k5e$1@smc.vnet.net> (permalink) |
| Organization | Time-Warner Telecom |
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
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
NestList with two lists Joao <joaopereira9@gmail.com> - 2013-10-16 08:59 +0000
csiph-web