#!/bin/bash


gcc -o problem4_1_1.exe problem4_1_1.c dSFMT.c

echo ""
./problem4_1_1.exe


gnuplot <<EOF
set terminal postscript eps enhanced color
set output "XY.eps"
set size 0.6,0.6
set xlabel "X"
set ylabel "Y"
set xrange [-8:8]
plot "XY.dat" u 1:2 w p pt 5 noti
EOF

sort -k1g XY.dat |\
awk '
BEGIN{
	dx = 1;
}
{
	tmp=int($1);
	if( tmp != before && NR != 1 ){
		ave = sum1/ count;
		std = sqrt( sum2/count - ave * ave );
		print( before*dx , ave ,std );
	}
	sum1 += $2;
	sum2 += $2 * $2;
	count += 1;
	before=tmp;
}
' > XY_average.dat



gnuplot <<EOF
set terminal postscript eps enhanced color
set output "XY_average.eps"
set size 0.6,0.6
set xlabel "X"
set ylabel "Y"
set xrange [-8:8]
plot "XY_average.dat" u 1:2:3 w yerrorbars noti
EOF