Thursday, March 31, 2011

Gnuplot: save plot in jpeg

This are some useful commands:

set terminal jpeg

set output "fig.jpeg"

replot

set terminal x11

Friday, March 25, 2011

Log scale in gnuplot plots

The corresponding command is:

set log xy

Monday, January 17, 2011

Very important commands in Gnuplot

Plot two or more plots in the same window:

set multiplot

Plots with lines:

plot "file.dat" w l

Plot with yerrorbars. The third column of the input data is the lenght of the errorbar:

plot "file.dat" with errorbars

Hide the legend:

unset key

Tuesday, November 16, 2010

Fortran program to calculate an integral using "crude" Monte Carlo

The following program calculates the integral I

using "crude" Monte Carlo. In this case, n=3, xia=0, xib=1 and f(x)=exp(a·x).


Program intmc

double precision fsum,fsum2,a,xa,xb,V,x,f,I,IA,varf
integer M,n,j,k
dimension a(3),xa(3),xb(3),x(3)
c n: dimension of the array x
c M: number of random numbers we use
c V: volum of the hipervolume
n=3
M=1000000
V=1
do j=1,n
a(j)=rand()
xa(j)=0
xb(j)=1
V=V*(xb(j)-xa(j))
enddo
c let's find the value of the integral that we get using monte-carlo
fsum=0
c fsum2 will help us to find the error
fsum2=0
do k=1,M
do j=1,n
x(j)=rand()
enddo
call func(a,x,f,n)
fsum=fsum+f
fsum2=fsum2+f**2
enddo
c I is the value of the integral we wanted to find
c IA is the value of the integral calculated analiticaly
I=V*fsum/M
varf=(fsum2/M-((fsum/M)**2))
sigma=sqrt(varf/M)
IA=1
do j=1,n
IA=IA*((exp(a(j))-1)/a(j))
enddo
write(*,*) I,sigma*3,IA
stop
end


SUBROUTINE func(a,x,f,n)
double precision a,x,f,ax
integer n
dimension a(n),x(n)
ax=0
do j=1,n
ax=ax+a(j)*x(j)
enddo
f=exp(ax)
end

Fortran program to calculate an integral using the method of trapezes

The following program calculates the integral I

using the trapezes method. In this case, n=3, xia=0, xib=1 and f(x)=exp(a·x).


Program inttr
double precision isum,x,xa,xb,h,f,I,a,IA,error,e
integer j,p,k1,k2,k3,n,l
dimension x(3),xa(3),xb(3),h(3),a(3)
n=3
p=300
do j=1,n
a(j)=rand()
xa(j)=0
xb(j)=1
h(j)=(xb(j)-xa(j))/p
enddo
isum=0
error=0
c we have to use n do's one inside the other. In this case, n=3.
c to calculate the error we need to compute the second partial
c derivate of F
do k1=1,p
x(1)=xa(1)+(k1-1/2)*h(1)
do k2=1,p
x(2)=xa(2)+(k2-1/2)*h(2)
do k3=1,p
x(3)=xa(3)+(k3-1/2)*h(3)
call func(a,x,f,n)
isum=isum+f
do l=1,n
error=error+f*(a(l)**2)
enddo
enddo
enddo
enddo
I=isum*(h(1)*h(2)*h(3))
e=(p**(-2/n))*error/(24*isum)
IA=1
do j=1,n
IA=IA*((exp(a(j))-1)/a(j))
enddo
write(*,*) I,e,IA
stop
end



SUBROUTINE func(a,x,f,n)
double precision a,x,f,ax
integer n,j
dimension a(n),x(n)
ax=0
do j=1,n
ax=ax+a(j)*x(j)
enddo
f=exp(ax)
end

Metropolis Algorithm

A very interesting link where we can understand easily the Metropolis Algorithm:

http://www.childrens-mercy.org/stats/weblog2007/MetropolisAlgorithm.asp

Saturday, November 6, 2010

Calculate PI with a simple fortran program

In this link (Fer problemes), you can find a montecarlo problem that allows us to find the number PI.

Here, you have one code that could be implemented to simulate this program. It is done in fortran.

PROGRAM calculpi

integer b,d,R
real acirc, aquad
double precision div
b=12345
d=76545
R=1
acirc=0
aquad=0
do i=1,1000000
a=rand(b)
c=rand(d)
b=a
d=c
if ((a**2+c**2)
acirc=acirc+1
endif
if ((a
aquad=aquad+1
endif
div=(acirc/aquad)*4

if (mod(i,10)==0)then
write (*,*) acirc, aquad, div
endif
enddo
STOP
END