program GenData

! generuje soubor dat zatizenych nahodnymi chybami pro T_smooth

use std_type
use RandomNum

integer,parameter :: bodu = 50				! bodu na interval [xmin,xmax]
real(kind=DP),parameter :: 	xmin = 0.0_DP,&
							xmax = 6.0_DP, &
							sko = 0.05		 !stredni kvadr.odchylka pro chyby
integer :: i
real(kind=DP) :: dx
real(kind=DP),dimension(bodu) :: x,y,chyby


dx = (xmax-xmin)/(bodu-1)
x(1) = xmin
x(bodu) = xmax
do i=1,bodu-2
	x(i+1)=x(i)+dx
end do

	! presne hodnoty
do i=1,bodu
	y(i)=F(x(i))
end do

	! zapis presnych hodnot do souboru "presny dat"
open (unit=11,file="presny.dat",status="replace",action="write")
write (unit=11,fmt="(a,i4)")'#',bodu	! # aby necetl gnuplot
do i=1,bodu
	write(unit=11,fmt="(f20.10,f22.18)")x(i),y(i)
end do
close(unit=11)	

	! Naplneni pole norm. rozlozenych chyb
chyby = sko*RandomNormal_A(bodu)

	! Zapis souboru vystupnich dat x(i) y(i)+NormRnd(i)
open (unit=11,file="smooth.dat",status="replace",action="write")
write (unit=11,fmt="(a,i4)"),'#',bodu  ! # aby se necetl v gnuplot
do i=1,bodu
	write(unit=11,fmt="(f20.10,f22.18)")x(i),y(i)+chyby(i)
end do
close(unit=11)

! graf s gnuplot
open(unit=11,file="graf.gp",status="replace",action="write")
write(unit=11,fmt=*)'reset'
write(unit=11,fmt=*)"plot 'presny.dat' with lines,'smooth.dat'"
!using 1:2:(50) smooth acsplines"
write(unit=11,fmt=*)"pause -1"
close(unit=11)

call system('wgnuplot graf.gp') 

contains

function F(x) result(y)
	real(DP),intent(in) :: x
	real(DP) :: y
	y = sin(x)+cos(2*x)
end function F

end program GenData


