Re: multistep iterative methods
- To: mathgroup at smc.vnet.net
- Subject: [mg40383] Re: [mg40324] multistep iterative methods
- From: Omega Consulting <info at omegaconsultinggroup.com>
- Date: Thu, 3 Apr 2003 01:44:15 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
At 03:52 AM 4/1/2003, Selwyn Hollis wrote:
>I'd like to throw this out as a challenge to the group: What's the most
>efficient way to implement in Mathematica an explicit multistep
>iterative method such as, say, the 4-step Adams-Bashforth method for
>solving y' = f(t,y):
>
>y[k+1]:= y[k] + (h/24)*(55*f[k] - 59*f[k-1] + 37*f[k-2] - 9*f[k-3])
>
>where y[0], y[1], y[2], y[3] are "given," and f[i] denotes f[t0 +i*h,
>y[i]]. The desired output would be the list
>
>{y[0], y[1], y[2], ... , y[n]}.
>
>A suitable toy problem is
>
>y' = -2t*y^2, y(0) = 1,
>
>with h = 0.01, n = 1000 (?), and the starting values taken from the
>exact solution y = 1/(1+t^2):
>
>y[0]=1, y[1] = 0.9999, y[2] = 0.9996, y[3] = .999101.
>
>Thanks in advance.
>
>-------
>Selwyn Hollis
I don't know about most efficient, but this does the trick.
In[1]:= h=.01;
Here is a function definition that uses caching. When it encounters a new
y[k] it calculates the value and then saves it. When you reuse the value,
it doesn't get recalculated. That makes it faster but it uses more memory.
In[2]:= y[k_] := y[k] = y[k-1]+(h/24)*(55*f[k-1]-59*f[k-2]+37*f[k-3]-9*f[k-4])
In[3]:= f[k_] := -2 h k y[k]^2
In[4]:=
y[0]=1;
y[1]=.9999;
y[2]=.9996;
y[3]=.999101;
In[8]:= Timing[lst=Table[y[k],{k,1000}];]
Out[8]= {0.15 Second,Null}
Compare the values:
In[9]:= y[1000]
Out[9]=0.00990099
In[10]:=1./(1+(h 1000)^2)
Out[10]=0.00990099
--------------------------------------------------------------
Omega Consulting
"The final answer to your Mathematica needs"
http://omegaconsultinggroup.com