<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Wolves And Rabbits</title>
	<atom:link href="http://programmingpraxis.com/2009/12/01/wolves-and-rabbits/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/12/01/wolves-and-rabbits/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: neurosis</title>
		<link>http://programmingpraxis.com/2009/12/01/wolves-and-rabbits/#comment-848</link>
		<dc:creator><![CDATA[neurosis]]></dc:creator>
		<pubDate>Thu, 17 Dec 2009 22:04:04 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1706#comment-848</guid>
		<description><![CDATA[here&#039;s some Matlab code to solve the problem using the 2nd order Runge-Kutta
you can use a variable time step if you like (deltaT)



function [t, wolves, rabbits]=rkWR(R0, W0, Rg, Wg, Rd, Wd, deltaT, maxT )


h=deltaT;
tmax= maxT;
t=0:h:tmax;
nsteps = tmax/h;

wolves = zeros(nsteps+1,1);
rabbits = zeros(nsteps+1,1);
wolves(1)=W0;
rabbits(1)=R0;


for i=1:nsteps;
    
    rk1 = h*rabbitsFunc(Rg,Rd,  rabbits(i), wolves(i));
    wk1 = h*wolvesFunc(Wg,Wd, rabbits(i), wolves(i));
    rk2 = h*rabbitsFunc(Rg,Rd, rabbits(i)+rk1/2, wolves(i)+wk1/2);
    wk2 = h*wolvesFunc(Wg,Wd, rabbits(i)+rk1/2, wolves(i)+wk1/2);
    rabbits(i+1)=rabbits(i)+rk2;
    wolves(i+1)=wolves(i)+wk2;
    
end

end

function [dWdt] = rabbitsFunc(Rg, Rd, R, W )
    dWdt =Rg*R-Rd*R*W;
end

function [dRdt] = wolvesFunc(Wg, Wd, R, W)
    dRdt = Wg*R*W-Wd*W;
end



to run for this example

 [t,w,r] = rkWR(40, 15, 0.1, 0.005, 0.01, 0.1,1, 200);
figure, plot(t, w,&#039;-+r&#039;);
hold on, plot(t, r, &#039;-ob&#039;);]]></description>
		<content:encoded><![CDATA[<p>here&#8217;s some Matlab code to solve the problem using the 2nd order Runge-Kutta<br />
you can use a variable time step if you like (deltaT)</p>
<p>function [t, wolves, rabbits]=rkWR(R0, W0, Rg, Wg, Rd, Wd, deltaT, maxT )</p>
<p>h=deltaT;<br />
tmax= maxT;<br />
t=0:h:tmax;<br />
nsteps = tmax/h;</p>
<p>wolves = zeros(nsteps+1,1);<br />
rabbits = zeros(nsteps+1,1);<br />
wolves(1)=W0;<br />
rabbits(1)=R0;</p>
<p>for i=1:nsteps;</p>
<p>    rk1 = h*rabbitsFunc(Rg,Rd,  rabbits(i), wolves(i));<br />
    wk1 = h*wolvesFunc(Wg,Wd, rabbits(i), wolves(i));<br />
    rk2 = h*rabbitsFunc(Rg,Rd, rabbits(i)+rk1/2, wolves(i)+wk1/2);<br />
    wk2 = h*wolvesFunc(Wg,Wd, rabbits(i)+rk1/2, wolves(i)+wk1/2);<br />
    rabbits(i+1)=rabbits(i)+rk2;<br />
    wolves(i+1)=wolves(i)+wk2;</p>
<p>end</p>
<p>end</p>
<p>function [dWdt] = rabbitsFunc(Rg, Rd, R, W )<br />
    dWdt =Rg*R-Rd*R*W;<br />
end</p>
<p>function [dRdt] = wolvesFunc(Wg, Wd, R, W)<br />
    dRdt = Wg*R*W-Wd*W;<br />
end</p>
<p>to run for this example</p>
<p> [t,w,r] = rkWR(40, 15, 0.1, 0.005, 0.01, 0.1,1, 200);<br />
figure, plot(t, w,&#8217;-+r&#8217;);<br />
hold on, plot(t, r, &#8216;-ob&#8217;);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Posts about Programming from google blogs as of December 2, 2009 &#171; tryfly.com</title>
		<link>http://programmingpraxis.com/2009/12/01/wolves-and-rabbits/#comment-806</link>
		<dc:creator><![CDATA[Posts about Programming from google blogs as of December 2, 2009 &#171; tryfly.com]]></dc:creator>
		<pubDate>Wed, 02 Dec 2009 23:55:01 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1706#comment-806</guid>
		<description><![CDATA[[...]  [...]]]></description>
		<content:encoded><![CDATA[<p>[...]  [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark VandeWettering</title>
		<link>http://programmingpraxis.com/2009/12/01/wolves-and-rabbits/#comment-805</link>
		<dc:creator><![CDATA[Mark VandeWettering]]></dc:creator>
		<pubDate>Wed, 02 Dec 2009 16:26:35 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1706#comment-805</guid>
		<description><![CDATA[I must admit that I cheated a tiny bit.  I took a stare at your Haskell solution, and basically did a straightforward port to Python (my &quot;just muck around&quot; language of choice for tiny problems like that).   I used generators in lieu of laziness, but other than that, you should recognize it.   It&#039;s been quite some time since I had to implement Runge-Kutta integrators (the most recent application of this kind of stuff that I&#039;ve done would have been simple particle systems where just Euler steps are okay for most of my purposes) so it was a nice review.

Anyway, here&#039;s the code:

[sourcecode lang=&quot;python&quot;]
#!/usr/bin/env python

# http://programmingpraxis.com/2009/12/01/wolves-and-rabbits/

def population(r, w, rg, wg, rd, wd):
    def dr(x, y):
        return rg * x - rd * x * y
    def dw(x, y):
        return wg * x * y - wd * x
    while True:
        yield r, w
        rh = r + dr(r, w) / 2.
        wh = w + dw(w, r) / 2.
        r = r + dr(rh, wh)
        w = w + dw(wh, rh)

g = population(40, 15, 0.1, 0.005, 0.01, 0.1)

for x in range(201):
        r, w = g.next()
        print r, w
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>I must admit that I cheated a tiny bit.  I took a stare at your Haskell solution, and basically did a straightforward port to Python (my &#8220;just muck around&#8221; language of choice for tiny problems like that).   I used generators in lieu of laziness, but other than that, you should recognize it.   It&#8217;s been quite some time since I had to implement Runge-Kutta integrators (the most recent application of this kind of stuff that I&#8217;ve done would have been simple particle systems where just Euler steps are okay for most of my purposes) so it was a nice review.</p>
<p>Anyway, here&#8217;s the code:</p>
<pre class="brush: python;">
#!/usr/bin/env python

# http://programmingpraxis.com/2009/12/01/wolves-and-rabbits/

def population(r, w, rg, wg, rd, wd):
    def dr(x, y):
        return rg * x - rd * x * y
    def dw(x, y):
        return wg * x * y - wd * x
    while True:
        yield r, w
        rh = r + dr(r, w) / 2.
        wh = w + dw(w, r) / 2.
        r = r + dr(rh, wh)
        w = w + dw(wh, rh)

g = population(40, 15, 0.1, 0.005, 0.01, 0.1)

for x in range(201):
        r, w = g.next()
        print r, w
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Cowan</title>
		<link>http://programmingpraxis.com/2009/12/01/wolves-and-rabbits/#comment-804</link>
		<dc:creator><![CDATA[John Cowan]]></dc:creator>
		<pubDate>Tue, 01 Dec 2009 23:00:09 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1706#comment-804</guid>
		<description><![CDATA[Arrgh, indentation lost.  Well, you can read it at http://www.masswerk.at/algol60/report.htm .]]></description>
		<content:encoded><![CDATA[<p>Arrgh, indentation lost.  Well, you can read it at <a href="http://www.masswerk.at/algol60/report.htm" rel="nofollow">http://www.masswerk.at/algol60/report.htm</a> .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Cowan</title>
		<link>http://programmingpraxis.com/2009/12/01/wolves-and-rabbits/#comment-803</link>
		<dc:creator><![CDATA[John Cowan]]></dc:creator>
		<pubDate>Tue, 01 Dec 2009 22:59:27 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1706#comment-803</guid>
		<description><![CDATA[The reason, if you are curious, that the Runge-Kutta example appears in the Scheme reports is that it appeared in the Algol 60 report (which is also responsible for the title template &quot;Revised^N Report on the Algorithmic Language X&quot;, though neither Algol 60 nor Algol 68 never got beyond Revised^1).

The Algol 60 version is no beauty by modern standards, but here it is for comparison:

procedure RK (x,y,n,FKT,eps,eta,xE,yE,fi); value x,y; integer n;
Boolean fi; real x,eps,eta,xE; array y,yE; procedure FKT;
comment RK integrates the system y&#039;k=fk(x,y1,y2,...,yn)(k=1,2,...n)
of differential equations with the method of Runge-Kutta with automatic
search for appropriate length of integration step. Parameters are: The
initial values x and y[k] for x and the unknown functions yk(x). The
order n of the system. The procedure FKT(x,y,n,z) which represents the
system to be integrated, i.e. the set of functions fk. The tolerance values eps
and eta which govern the accuracy of the numerical integration. The end
of the integration interval xE; The output parameter yE which represents
the solution x=xE. The Boolean variable fi, which must always be given 
the value true for an isolated or first entry into RK. If however the functions
y must be available at several meshpoints x0,x1,...,xn, then the procedure
must be called repeatedly (with x=xk, xE=x(k+1), for k=0,1,...,n-1)
and then the later calls may occur with fi=false which saves computing
time. The input parameters of FKT must be x,y,z,n, the output parameter z
represents the set of derivatives z[k]=fk(x,y[1],y[2],...,y[n]) for x and
the actual y&#039;s. A procedure comp enters as a non-local identifier;

begin
    array z,y1,y2,y3[1:n]; real x1,x2,x3,H; Boolean out;
    integer k,j; own real s,Hs;
    procedure RK1ST (x,y,h,xe,ye); real x,h,xe; array y,ye;
        comment RK1ST integrates one single Runge-Kutta step with
        initial values x, y[k] which yields the output parameters xe=x+h
        and ye[k], the latter being the solution at xe.  Important: the
        parameters n, FKT, z enter RK1ST as nonlocal entities;
        begin
            array w[1:n], a[1:5]; integer k,j;
            a[1]:=a[2]:=a[5]:=h/2; a[3]:=a[4]:=h;
            xe:=x;
            for k:=1 step 1 until n do ye[k]:=w[k]:=y[k];
            for j:=1 step 1 until 4 do
                begin
                    FKT(xe,w,n,z);
                    xe:=x+a[j];
                    for k:=1 step 1 until n do
                    begin
                        w[k]:=y[k]+a[j]TIMESz[k];
                        ye[k]:=ye[k]+a[j+1]TIMESz[k]/3
                    end k
                end j
        end RK1ST;

Begin of program:

    if fi then begin H:=xE-x; s:=0 end else H:=Hs;
    out:=false;

AA: if (x+2.01TIMESH-xE)&gt;0) EQUIVALENCE (H&gt;0) then
    begin Hs:=H; out:=true; H:=(xE-x)/2 end if;
    RK1ST (x,y,2TIMESH,x1,y1);

BB: RK1ST (x,y,H,x2,y2); RK1ST (x2,y2,H,x3,y3);
    for k:=1 step 1 until n do
        if comp (y1[k],y3[k],eta)&gt;eps then goto CC;
    comment comp(a,b,c) is a function designator, the value of
    which is the absolute value of the difference of the mantissae of a
    and b, after the exponents of these quantities have been made equal
    to the largest of the exponents of the originally given parameters
    a, b, c;
    x:=x3; if out then goto DD;
    for k:=1 step 1 until n do y[k]:=y3[k];
    if s=5 then begin s:=0; H:=2TIMESH end if;
    s:=s+1; goto AA;

CC: H:=0.5TIMESX; out:=false; x1:=x2;
    for k:=1 step 1 until n do y1[k]:=y2[k];
    goto BB;

DD: for k:=1 step 1 until n do yE[k]:=y3[k]
end RK]]></description>
		<content:encoded><![CDATA[<p>The reason, if you are curious, that the Runge-Kutta example appears in the Scheme reports is that it appeared in the Algol 60 report (which is also responsible for the title template &#8220;Revised^N Report on the Algorithmic Language X&#8221;, though neither Algol 60 nor Algol 68 never got beyond Revised^1).</p>
<p>The Algol 60 version is no beauty by modern standards, but here it is for comparison:</p>
<p>procedure RK (x,y,n,FKT,eps,eta,xE,yE,fi); value x,y; integer n;<br />
Boolean fi; real x,eps,eta,xE; array y,yE; procedure FKT;<br />
comment RK integrates the system y&#8217;k=fk(x,y1,y2,&#8230;,yn)(k=1,2,&#8230;n)<br />
of differential equations with the method of Runge-Kutta with automatic<br />
search for appropriate length of integration step. Parameters are: The<br />
initial values x and y[k] for x and the unknown functions yk(x). The<br />
order n of the system. The procedure FKT(x,y,n,z) which represents the<br />
system to be integrated, i.e. the set of functions fk. The tolerance values eps<br />
and eta which govern the accuracy of the numerical integration. The end<br />
of the integration interval xE; The output parameter yE which represents<br />
the solution x=xE. The Boolean variable fi, which must always be given<br />
the value true for an isolated or first entry into RK. If however the functions<br />
y must be available at several meshpoints x0,x1,&#8230;,xn, then the procedure<br />
must be called repeatedly (with x=xk, xE=x(k+1), for k=0,1,&#8230;,n-1)<br />
and then the later calls may occur with fi=false which saves computing<br />
time. The input parameters of FKT must be x,y,z,n, the output parameter z<br />
represents the set of derivatives z[k]=fk(x,y[1],y[2],&#8230;,y[n]) for x and<br />
the actual y&#8217;s. A procedure comp enters as a non-local identifier;</p>
<p>begin<br />
    array z,y1,y2,y3[1:n]; real x1,x2,x3,H; Boolean out;<br />
    integer k,j; own real s,Hs;<br />
    procedure RK1ST (x,y,h,xe,ye); real x,h,xe; array y,ye;<br />
        comment RK1ST integrates one single Runge-Kutta step with<br />
        initial values x, y[k] which yields the output parameters xe=x+h<br />
        and ye[k], the latter being the solution at xe.  Important: the<br />
        parameters n, FKT, z enter RK1ST as nonlocal entities;<br />
        begin<br />
            array w[1:n], a[1:5]; integer k,j;<br />
            a[1]:=a[2]:=a[5]:=h/2; a[3]:=a[4]:=h;<br />
            xe:=x;<br />
            for k:=1 step 1 until n do ye[k]:=w[k]:=y[k];<br />
            for j:=1 step 1 until 4 do<br />
                begin<br />
                    FKT(xe,w,n,z);<br />
                    xe:=x+a[j];<br />
                    for k:=1 step 1 until n do<br />
                    begin<br />
                        w[k]:=y[k]+a[j]TIMESz[k];<br />
                        ye[k]:=ye[k]+a[j+1]TIMESz[k]/3<br />
                    end k<br />
                end j<br />
        end RK1ST;</p>
<p>Begin of program:</p>
<p>    if fi then begin H:=xE-x; s:=0 end else H:=Hs;<br />
    out:=false;</p>
<p>AA: if (x+2.01TIMESH-xE)&gt;0) EQUIVALENCE (H&gt;0) then<br />
    begin Hs:=H; out:=true; H:=(xE-x)/2 end if;<br />
    RK1ST (x,y,2TIMESH,x1,y1);</p>
<p>BB: RK1ST (x,y,H,x2,y2); RK1ST (x2,y2,H,x3,y3);<br />
    for k:=1 step 1 until n do<br />
        if comp (y1[k],y3[k],eta)&gt;eps then goto CC;<br />
    comment comp(a,b,c) is a function designator, the value of<br />
    which is the absolute value of the difference of the mantissae of a<br />
    and b, after the exponents of these quantities have been made equal<br />
    to the largest of the exponents of the originally given parameters<br />
    a, b, c;<br />
    x:=x3; if out then goto DD;<br />
    for k:=1 step 1 until n do y[k]:=y3[k];<br />
    if s=5 then begin s:=0; H:=2TIMESH end if;<br />
    s:=s+1; goto AA;</p>
<p>CC: H:=0.5TIMESX; out:=false; x1:=x2;<br />
    for k:=1 step 1 until n do y1[k]:=y2[k];<br />
    goto BB;</p>
<p>DD: for k:=1 step 1 until n do yE[k]:=y3[k]<br />
end RK</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2009/12/01/wolves-and-rabbits/#comment-800</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Tue, 01 Dec 2009 12:42:04 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1706#comment-800</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2009/12/01/programming-praxis-wolves-and-rabbits/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
pops :: Fractional a =&gt; a -&gt; a -&gt; a -&gt; a -&gt; a -&gt; a -&gt; [(a, a)]
pops r w rg wg rd wd = (r, w) : pops r&#039; w&#039; rg wg rd wd where
    dr x y = rg*x - rd*x*y
    dw x y = wg*x*y - wd*x
    rh     = r + dr r w / 2
    wh     = w + dw w r / 2
    r&#039;     = r + dr rh wh
    w&#039;     = w + dw wh rh
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2009/12/01/programming-praxis-wolves-and-rabbits/" rel="nofollow">http://bonsaicode.wordpress.com/2009/12/01/programming-praxis-wolves-and-rabbits/</a> for a version with comments):</p>
<pre class="brush: css;">
pops :: Fractional a =&gt; a -&gt; a -&gt; a -&gt; a -&gt; a -&gt; a -&gt; [(a, a)]
pops r w rg wg rd wd = (r, w) : pops r' w' rg wg rd wd where
    dr x y = rg*x - rd*x*y
    dw x y = wg*x*y - wd*x
    rh     = r + dr r w / 2
    wh     = w + dw w r / 2
    r'     = r + dr rh wh
    w'     = w + dw wh rh
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

