CFLAGS = -Wall -std=gnu11 -Ofast
LDLIBS = -lm

all:Out.txt Out.error.png

Out.error.png:out.data.txt Makefile
	echo '\
set term png ;\
set out "$@" ;\
set log y ;\
set title "Monte Carlo integration of x^2+y^2<R^2?1:0" ;\
set xlabel "number of points N" ;\
set ylabel "actual error" ;\
set format x "%g" ;\
set format y "%.0e" ;\
set xtics 25000 ;\
set mxtics 5 ;\
a=1; b=-0.5; f(x)=a*x**b ;\
c=1; d=-1.0; g(x)=c*x**d ;\
fit f(x) "$<" using 1:2 via a,b ;\
fit g(x) "$<" using 1:3 via c,d ;\
plot \
 "$<" using 1:2 with linespoints pointtype 5 title "pseudo-random" \
,"$<" using 1:3 with linespoints pointtype 4 title "quasi-random" \
, f(x) with lines title sprintf("%3.1f*n^{%3.1f}",a,b) \
, g(x) with lines title sprintf("%3.1f*n^{%3.1f}",c,d) \
	'|gnuplot

ns=$(shell seq 1000 1250 200000)
out.data.txt:main
	>$@
	@for n in $(ns); do echo n=$$n; ./main $$n >>$@; done

Out.txt:main
	./$< > $@

main:main.o plainmc.o quasimc.o

clean:
	$(RM) main *.o [Oo]out*

test:
	echo $(ns)
