19PH40012 Assignment-9 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Assignment-9

Name:Durgesh joshi
Roll:19PH40012
Q1. Random number generation
Python Code:
import random
fin=open("plot1.txt","w")
for i in range(10000):
x=random.random()
fin.write(str(x)+"\n")
fin.close()
Gnuplot code:
set grid
set key off
set ylabel 'Number of Counts'
set xlabel 'Grouped Random Numbers'
set boxwidth 0.05 absolute
set style fill solid 1.0 noborder
bin_width = 0.1
bin_number(x) = floor(x/bin_width)
rounded(x) = bin_width * ( bin_number(x) + 0.5 )
set terminal jpeg
set output 'randfirst.jpeg'
plot 'plot1.txt' using (rounded($1)):(1) smooth frequency with boxes lc 3

Output:
Q2. Gaussian random number
Python code:
import random
fin=open('random2.txt', 'w')
for x in range(5*pow(10,4)):
fin.write(str(random.gauss(0, 5))+'\n')
fin.close()
Gnuplot code:
set grid
set key off
set ylabel 'Number of Counts'
set xlabel 'Grouped Random Numbers'
set boxwidth 0.05 absolute
set style fill solid 1.0 noborder
bin_width = 0.1
bin_number(x) = floor(x/bin_width)
rounded(x) = bin_width * ( bin_number(x) + 0.5 )
set terminal jpeg
set output 'randsecond.jpeg'
plot 'random2.txt' using (rounded($1)):(1) smooth frequency with boxes lc 3
Output:

Q3:Brownian motion:
Python code:

import random
from math import sqrt
rx, ry = 0, 0
dt=0.1
t=0
fin=open('brownian.txt', 'w')
fin.write(str(t)+'\t'+str(rx)+'\t'+str(ry)+'\n')
for i in range(1000):
t=t+dt
rx=rx+sqrt(dt)*random.gauss(0,1)
ry=ry+sqrt(dt)*random.gauss(0,1)
fin.write(str(t)+'\t'+str(rx)+'\t'+str(ry)+'\n')
fin.close()

Gnuplot code:
set terminal gif animate size 800, 600
set output 'brownfinal.gif'
set grid
set xlabel 'x co-ordinate'
set ylabel 'y co-ordinate'
set xrange [-10:1]
set yrange [-6:6]
t=0
do for [ii=1:1001]{
plot 'brownian.txt' u 2:3 every ::1::ii title sprintf("t=%4.3f",t) w lp pt 7 lc 3
t=t+0.1
pause 0.05
}

Output:

You might also like