#!/usr/bin/perl -w
#iterate some, well, iteration formula for several start values to produce data points for plotting (or whatever)
#by Thomas Orgis <thomas@orgis.org>, licensed under the Artistic License

use lib $ENV{HOME}.'/lib/perl';
use Param;

my $const = Param::Parse
(
	{'info', 'I will iterate a given formula given times with start values that I iterate, too. You give me parameters (some iteration formula would be good), I write data points to files named with the filename body and a startvalue iteration count plus a csv ending that you may plot with Gnuplot or whatever sane program is out there for that stuff.' },
	[
		'begin',0,'b','start value',
		'step',0.1,'s','step for start values',
		'end',1,'e','last start value (abort when we get over this)',
		'file','logabb','F','filename body',
		'nit',10,'n','number of iterations',
		'formula','2.8*$x*(1-$x)','f','the formula in PERL syntax with $x being the last x value'
	]
);

my $o = 0;
for(my $s = $const->{begin}; $s <= $const->{end}; $s = $const->{begin}+$o*$const->{step})
{
	open(FILE,">$const->{file}.".sprintf("%04d",++$o).".csv") or die "Cannot write to file!\n";
	my $x = $s;
	print FILE "0\t$x\n";
	for(my $i = 1; $i <= $const->{nit}; ++$i)
	{
		$x = eval($const->{formula});
		print FILE "$i\t$x\n";
	}
	close(FILE);
}
