#!/usr/bin/ruby -w =begin /*************************************************************************** * Copyright (C) 2008 by Paul Lutus * * lutusp@arachnoid.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ =end histogram = true plot = true # c = cicadas # p = predators maxc = 25 # cicada test domain maxp = 25 # predator test domain sample_years = 1000 total = Array.new(maxc+1,0) # cicada reproductive cycle for c in 1..maxc cicadas = 2 # predator reproductive cycle for p in 1..maxp for y in 1..sample_years # if cicadas and predators appear at once if((y % c == 0) && (y % p == 0)) # predators attack cicadas cicadas -= 54 # no fewer than zero cicadas cicadas = (cicadas < 0)?0:cicadas end cicadas += 1 end end total[c] = cicadas / (c+1) end if histogram # show text histogram puts "Year Survivors" for i in 1..maxc printf("%4d %8d %s\n",i,total[i],'*' * (total[i] / 20)) end end if plot # create gnuplot histogram gnuplot_data=<<-__EOF__ set style data histogram set style fill solid border -1 set xlabel "Years in Cycle" set ylabel "Survivors" set xrange [2:25] set yrange [0:1000] unset xtics set title "Cicada Survival Rate vs. Cycle Years" plot '-' using 2 lc rgb '#c0e0ff' notitle with boxes,\ '-' using 0:(\$2+30):1 notitle with labels __EOF__ s = "" for i in 0...maxc # dummy data to provide an empty left margin v = (i > 2)?total[i]:"?" s += "#{i} #{v}\n" end s += "e\n" # insert two copies of the data set for the 2 plots gnuplot_data.gsub!(%r{},s + s) IO.popen("gnuplot -persist","w") { |p| p.write gnuplot_data } end