Skip to main content

DIFFUSION-RANDOM WALK

## the function that takes in the initial positions
## of random walkers and the number of steps as
## an argument and returns the final positions
## in 2D. The figure of the walkers after the comple-
## tion of the walk also plotted.
function f=diffusion(Nsteps)
f=[];
Nwalkers=9;
for i=1:Nsteps ## The loop through the number
## of steps each in walk.
p=initial_positions(Nwalkers); ## Initial squared location of all of the
## walkers at all steps.
for m=1:Nwalkers ## The loop through the desired
## number of walkers.
r=rand_disc_rev(N); ## Calling the rw generator
## function which will give column vector
f=[f;p+r] ## of each elements are either 1 or -1.
## Total displacement whose
## elements stems from the vector r.
endfor
f
endfor
plot(f(:,1),f(:,2),'r*;Dif;')
endfunction

Comments

Popular posts from this blog

FACTORIAL

## Function that calculates the factorial of a number ## Usage : f=factorial(n) function f=factorial(n) ## Initialize the output f=1; ## Check whether the input is correct if ( (n<0) || (rem(n,1)~=0) ) printf("n cannot be a negative number. Exiting...\n"); return endif for num=1:n f*=num; endfor endfunction

FREE FALL

## A function, free fall, that takes in h (in metres), and ## returns the final velocity of the ball at the ## time step just before it touches the ground and makes ## a plot of velocity versus time. function freefall(h) ## constants and initializations v=0; ## initial velocity(at rest) [m/sec] y=h; ## initial altitude of the ball from the ground [m] t=0; ## initial time[sec ] B1=0.05; ## Coeff of the term prop to v [kg/sec] B2=6E-4; ## Coeff of the term prop to v^2 [kg/m] m=0.25; ## Mass of the ball [kg] g=9.8; ## Gravitational acceleration [m/sec^2] dt=0.1; ## Time increment [sec] n=1; ## Initialize the loop index[dimensionless] ## Run the loop or iterate until the vertical component is ## smaller than zero. while (y(n)>0); ## decrease the altitude in each step y=[y;y(n)+v(n)*dt]; ## and accumlate the results in the array ## of the vertical displacement. ## increase the time in each step t=[t;t(n)+dt]; ## and accumlate the results in the array ## of the tim...

PRIME OR NOT

## A funtion that takes a number ## in as an argument and returns ## the output p=1 if it is prime ## otherwise returns p=0. function prime_or_not(num) p=1; ## assume the number is prime. div=2; ## the smallest divisor. ## since 2*num/2> num. Forbidden Nsteps=num/2 ; ## Interval of division: the half ## of the num end itself for i= div:Nsteps ## to divide al the numbers less than ## num/2. if(rem(num,i)==0) ## any prime num cannot be ## divided with an integer. p=0; ## otherwise exit the loop. break; endif endfor if(p == 1) ## prints the result p=1 else p=0 endif endfunction