[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/WXb79n5K    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Nov 22:
; wolves and rabbits

(define (zip . xss) (apply map list xss))

(define (wolves-rabbits init-rabbit rabbit-growth rabbit-death
                        init-wolf wolf-growth wolf-death limit)
  (let loop ((ts (list 0)) (rs (list init-rabbit)) (ws (list init-wolf)))
    (if (= (car ts) limit)
        (apply zip (map reverse (list ts rs ws)))
        (let* ((rabbit-deriv
                 (- (* rabbit-growth (car rs))
                    (* rabbit-death (car rs) (car ws))))
               (wolf-deriv
                 (- (* wolf-growth (car ws) (car rs))
                    (* wolf-death (car ws))))
               (rabbit-half
                 (+ (car rs) (/ rabbit-deriv 2)))
               (wolf-half
                 (+ (car ws) (/ wolf-deriv 2)))
               (rabbit-half-deriv
                 (- (* rabbit-growth rabbit-half)
                    (* rabbit-death rabbit-half wolf-half)))
               (wolf-half-deriv
                 (- (* wolf-growth wolf-half rabbit-half)
                    (* wolf-death wolf-half))))
          (loop (cons (+ (car ts) 1) ts)
                (cons (+ (car rs) rabbit-half-deriv) rs)
                (cons (+ (car ws) wolf-half-deriv) ws))))))

(for-each
  (lambda (x) (display x) (newline))
  (wolves-rabbits 40 0.1 0.01 15 0.005 0.1 200))


Output:
(0 40 15)
(1 37.7575 16.49625)
(2 35.11682084652719 17.920285997048907)
(3 32.21720532523942 19.196647945977375)
(4 29.21033531051106 20.25857350383283)
(5 26.23820534547707 21.057736744807745)
(6 23.415861799464672 21.56967069728412)
(7 20.822806519331223 21.793876151526966)
(8 18.503023993420193 21.749641689147552)
(9 16.470881928145833 21.46968844995898)
(10 14.719473738577925 20.99368109147781)
(11 13.228764049700231 20.362900634750563)
(12 11.972149492833305 19.61655446155968)
(13 10.921066961160475 18.789634740647625)
(14 10.047860543671682 17.911977234902242)
(15 9.327333067242062 17.00812728968524)
(16 8.737411478755451 16.097680743398186)
(17 8.259270851985331 15.195859458540726)
(18 7.877160975883269 14.314166108211221)
(19 7.578093294383847 13.461027624579781)
(20 7.351482612034223 12.642380777785995)
(21 7.188795539357152 11.862180856135353)
(22 7.0832311230685425 11.122830252350234)
(23 7.029443515187421 10.425532027568593)
(24 7.023307967920859 9.770577198878412)
(25 7.061727148748936 9.157575556540735)
(26 7.142472951721272 8.585639502530345)
(27 7.264058515118986 8.053529449096446)
(28 7.425635372310665 7.559768140294456)
(29 7.626911177213464 7.102730080706874)
(30 7.868084047174081 6.680711179142483)
(31 8.149790145131579 6.291982782095642)
(32 8.473061628106526 5.9348334892930605)
(33 8.839292499981438 5.607601502394691)
(34 9.250210218349501 5.308699741780687)
(35 9.707851119696608 5.0366355578025335)
(36 10.21453784774572 4.79002654550115)
(37 10.77285699865535 4.567613731510012)
(38 11.385635133398154 4.368273227297239)
(39 12.055911147829482 4.191027325427242)
(40 12.786902726126453 4.035055949097478)
(41 13.581964220024027 3.899709346148276)
(42 14.444532775929208 3.7845229454589213)
(43 15.378058850428056 3.689235366263714)
(44 16.385916382562197 3.6138106907613214)
(45 17.47128679517714 3.5584662790154273)
(46 18.63700964334766 3.5237076227146775)
(47 19.88539108674788 3.5103719966599902)
(48 21.21795942471929 3.519682960039074)
(49 22.63515472958583 3.5533180510828606)
(50 24.135937260886347 3.613492241938142)
(51 25.71729711025712 3.7030597494431046)
(52 27.373645953795048 3.8256364058688144)
(53 29.096071870427476 3.985743593308202)
(54 30.871441646828163 4.188972095917026)
(55 32.68134467445579 4.442159115639927)
(56 34.50089288363699 4.753562623893956)
(57 36.297428596121904 5.133002117321202)
(58 38.02925515470363 5.591911249262623)
(59 39.64460283861595 6.143213624398119)
(60 41.08118048200765 6.800888525265766)
(61 42.266833844334506 7.579045145035263)
(62 43.12199738040103 8.490291744129255)
(63 43.564695744139996 9.543210539863953)
(64 43.518665966114284 10.738893422361365)
(65 42.92453086249879 12.066824904419057)
(66 41.75274048948661 13.50092819627547)
(67 40.01540930405119 14.997174110371919)
(68 37.77294516538183 16.49442459746863)
(69 35.131624860419365 17.919672536445532)
(70 32.23076873625329 19.19736840615228)
(71 29.22221573405276 20.2606372965997)
(72 26.248157138747843 21.061042020117643)
(73 23.423830202839465 21.574027975720007)
(74 20.828887857346977 21.799044392374636)
(75 18.507411534368416 21.755365023575994)
(76 16.47381457384929 21.47572518536432)
(77 14.72119703751402 20.999821768778112)
(78 13.229505688103478 20.36897596291352)
(79 11.97210703892448 19.622435877970563)
(80 10.920404141663191 18.795230586389792)
(81 10.0467085698989 17.91722692703266)
(82 9.325794429647855 17.012995060351454)
(83 8.73556456800443 16.102149820713564)
(84 8.257174493998306 15.199927079847807)
(85 7.874858451971537 14.317839464113385)
(86 7.575615702900747 13.46432067872754)
(87 7.348851578151621 12.645311841443947)
(88 7.186025347304032 11.864770779842418)
(89 7.080330367044505 11.125101104838663)
(90 7.026416363996226 10.42750615007845)
(91 7.020155127262144 9.772276543373549)
(92 7.058446591027632 9.159021235411087)
(93 7.139060469556071 8.586851493703646)
(94 7.260508142850493 8.054526413777959)
(95 7.421939710112114 7.560567321030874)
(96 7.623061644806446 7.103347255551193)
(97 7.864071088379436 6.681160653832906)
(98 8.14560340023154 6.2922774067653355)
(99 8.468690087972885 5.934984689644238)
(100 8.834724653899286 5.60761931777668)
(101 9.245434206254066 5.308592864421996)
(102 9.702854899009559 5.036411368724597)
(103 10.209309385058766 4.7896911435079295)
(104 10.767384497112365 4.567171952191988)
(105 11.379907307753623 4.36772864927468)
(106 12.049917560818404 4.190382261131177)
(107 12.780634201998476 4.034311417279172)
(108 13.575413353918098 3.8988650230421644)
(109 14.437694561248517 3.783577091119184)
(110 15.370931450568353 3.688184722073233)
(111 16.378502078274195 3.6126503435081823)
(112 17.463593144468682 3.557189486307444)
(113 18.629050896993817 3.522305593966948)
(114 19.877189908793586 3.5088336236399913)
(115 21.209548973056226 3.517994491326809)
(116 22.626581155335845 3.551462706371035)
(117 24.127262684433266 3.611449765799282)
(118 25.708603122148585 3.7008059115378997)
(119 27.365037663590396 3.8231424679277803)
(120 29.08768247524729 3.9829757861706314)
(121 30.86343739783311 4.18589118765463)
(122 32.67392996085732 4.4387202113894135)
(123 34.49431489994008 4.749715428059659)
(124 36.2919806745378 5.12869201751169)
(125 38.02527730280968 5.587081751332778)
(126 39.64247732677134 6.13781086491889)
(127 41.08131956482388 6.794868759356545)
(128 42.26965613391459 7.572386132373489)
(129 43.12788954541825 8.483008612112368)
(130 43.573957933315526 9.535375671929504)
(131 43.531443619206506 10.73065790244627)
(132 42.94074279597422 12.058436088599695)
(133 41.772021939679824 13.49273498381744)
(134 40.03709461159987 14.989610565342801)
(135 37.796111386862556 16.487966412915053)
(136 35.15520043450497 17.91477042013451)
(137 32.25368482264119 19.194374386536143)
(138 29.243556580618833 20.259746743099495)
(139 26.26726496283793 21.062269806782563)
(140 23.440341280356087 21.577223316061364)
(141 20.84270546811389 21.803934185169858)
(142 18.518640741596094 21.76160834877978)
(143 16.482686795806465 21.482963463989126)
(144 14.728004006715716 21.00771590712403)
(145 13.234551330456618 20.377228582403337)
(146 11.975677607364899 19.630800934646718)
(147 10.92275290395248 18.803514200364305)
(148 10.04805041019679 17.925283055996776)
(149 9.326306359117439 17.02071876387024)
(150 8.735389258467611 16.10946989665903)
(151 8.256424996017257 15.206799078623707)
(152 7.873623046467753 14.324239555209033)
(153 7.573962357445772 13.470240520572212)
(154 7.346831793824181 12.650754429370274)
(155 7.18367735564677 11.869747164415793)
(156 7.077681722495259 11.12962784458237)
(157 7.023486012283557 10.43160336309701)
(158 7.016955038196387 9.775966436138065)
(159 7.054983038889052 9.162327005395277)
(160 7.135335033723573 8.589796521197231)
(161 7.256518489543155 8.057133675019063)
(162 7.417680205105163 7.562858969069516)
(163 7.61852383753744 7.105344326925359)
(164 7.8592441001838775 6.682882868450651)
(165 8.14047424346412 6.293743035689327)
(166 8.463243938297452 5.936210471125535)
(167 8.828945094863778 5.608620406202958)
(168 9.239303463224012 5.309382800101759)
(169 9.696354078472641 5.037002058840757)
(170 10.202418735134664 4.790092846311429)
(171 10.760083705752644 4.567393257698303)
(172 11.372175856583661 4.367776450468507)
(173 12.04173515488458 4.190261710045695)
(174 12.77198129890262 4.034025860342763)
(175 13.56627182004342 3.8984159098083944)
(176 14.428048488128166 3.7829638499364826)
(177 15.360768171054001 3.6874045954284678)
(178 16.36781343015263 3.6116981747221573)
(179 17.452377038622323 3.5560574480267815)
(180 18.61731325805351 3.520982847599333)
(181 19.864947068179866 3.507305897737033)
(182 21.196830606566067 3.516243566333127)
(183 22.613433868388626 3.5494657938414225)
(184 24.113754354711602 3.609178773356832)
(185 25.69482810630506 3.6982265925320807)
(186 27.35112295290073 3.8202134702280746)
(187 29.07379482989264 3.979647642922473)
(188 30.849791377999264 4.182105341828095)
(189 32.660796571088376 4.434408246496726)
(190 34.48203022416218 4.7447988036987825)
(191 36.280953344661626 5.123082795987834)
(192 38.015992856448506 5.58068506111925)
(193 39.63549644645469 6.130530168564577)
(194 41.07726593560271 6.7866153180602895)
(195 42.26918852363756 7.5630942293349825)
(196 43.131652417013655 8.472657800450186)
(197 43.58250667405646 9.524020067130575)
(198 43.54514589058975 10.718459818804705)
(199 42.95966760202682 12.045696991289548)
(200 41.79584144732209 13.479911834185545)


Create a new paste based on this one


Comments: