Show results of Runge-Kutta Solution

• To: mathgroup at smc.vnet.net
• Subject: [mg70218] Show results of Runge-Kutta Solution
• From: nausea1995 at gmail.com
• Date: Sat, 7 Oct 2006 07:09:00 -0400 (EDT)

```Hi all.  I am trying to solve a DE with Exact, Euler, RK 2nd order, and
RK 4th order.

I have been able to solve the equation ( y'[t] = 3 Cos[t] - 2 y[t],
y[0]=0 ) using all of these methods with code provided by my
instructor, but now I am trying to NestList these results for h= .01
and n=30 for all 4 methods, and create a table.

I can plot the four solutions together, the problem comes when I try to
assemble a NestList for use in a table.  The code I was given...

--------
tablestep[{t_, a1_, a2_}] := {t + h, exactsol[t + h], Eulerstep[{t,
a2}][[2]]}
tablevals = NestList[tablestep, {0, .5, .5}, 30]
--------

and then to create the table...

--------
TableForm[tablevals[[{1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23,
24, 25, 26, 27, 28, 29, 30}]], TableHeadings -> {{"h=" <>
ToString[h]}, {"t", "Exact",
"Euler"}}]
--------

Works perfect, for those two solutions, but I am not sure how to add my
solutions RK4step and Heunstep.  I've tried several variations but
either get 'RK4Step[*]' in the solution or a list of two values for a
solutions such as {1, 2, {0, 1}} or something along those lines.  I
guess I don't really understand the syntax of the Eulerstep[{t, a2} and
how that needs to be setup for extra solutions.  I gave up on it and
decided I'd place the values into excel and concatenate it into a list
I could put back into Mathematica, but even then, when I ran the
code...
---------
RK4step[{t_, y_}] := Module[{k1, k2, k3, k4, hh = .5h},
k1 = f[t, y];
k2 = f[t + hh, y + hh k1];
k3 = f[t + hh, y + hh k2];
k4 = f[t + h, y + h k3];
\!\(\(\({t + h, \ y + h\/6\ \((k1\  + \ 2  k2 + 2  k3 +
k4)\)}\)\(]\)\)\)

h = 0.1;

RK4points = NestList[RK4step, {0, 0}, 30];
--------
I don't get a list like I do when I did the NestList for the Euler
solution.  I can plot it fine, but if I have to export it to excel and
create my final list and put it back, I need to somehow get the
calculated values.

Any help would be greatly appreciated.

```

• Prev by Date: Re: JLink OutOfMemoryError