## 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 time for the time axis in…

## a script for a sound wave source made up of two different masses ##seperated in the middle. Let the rigth part is lighter than the
##left one that ensures that the ligther is faster, and so has
##greater r since r=cdt/dx which is dimensionless(ratio).
dx=1e-2; ## Increment in the horizontal distance (m)
c1=300; ## speed of the left one (m/s)
c2=500; ## Speed of the rigth one (m/s)
dt=dx/max(c1,c2); ## Increment in time (s). The max c, the min dt.
r1=c1*dt/dx; ## Ratio r for the left one
r2=c2*dt/dx; ## Ration r for the right one
x=-1:dx:1; ## same as the string_fixed
l=length(x);
x0=0.5;
k=1e2;
## Inital profile(return to guassuian distibution)
y=initial_profile(x,x0,k);
axis([-1.05,1.05,-1.1,1.1])
plot(x,y,'r;;',[0 0],[-1.1 1.1],'r-;;')
pause
## Boundary conditions while t=dtn=0
ynow=y;
yprev=y;
N=100;
for n=1:N
ynext=propagate_two_parts(ynow,yprev,r1,r2); ## calling the prepared
## function
plot(x,ynext,';;',[0 0],[-1.1 1.1],'r-;;')
axis([-1.05,1.…

## A 'realistic', 'non-elastic' string, which responses to any
## bending and has stifness. This script takes in the previous
## and the present profiles and iterates to find
## the profile in the next time step. The ratio 'r' is not 1
## like in the 'non-realistic' string since the speed of the wave
## always less than the speed of the string it should be less than 1
## for best and most stable solution
##constants
dx=1e-2 ## Spatial increment (m)
L=2 ## Length of the string (m)
M=L/dx ## Dimensionless partition
r=0.25 ## Famous dimensionless ratio
E=1e-4 ## Dimensionless stiffnes
x=-1:dx:1;
l=length(x);
x0=0.5;
k=1e2;
## Set up the initial profile
y=initial_profile(x,x0,k);
plot(x,y)
pause
## Impose the time boundary condition
ynow=y;
yprev=y;
ynow(1)=ynow(2)=0
Nsteps=2000;
for n=3:Nsteps
ynow(Nsteps-1)=ynow(Nsteps-2)=0;
ynext=propagate_stiff(ynow,yprev,r);
plot(x,ynext,';;')
axis([-1.05,1.05,-1.1,1.1])
pause(0);
endfor

## Script that simulates a traveling wave on an nonrealistic string.
##The one end of the string is driven sinusoidally
## while the other end is kept fixed.
dx=1e-2; ## increment in horizontal displacement(m)
c=300; ## speed of the wave (m/s)
dt=dx/c; ## Increment in time (s)
r=c*dt/dx; ## Ratio of speed of wave to
## the speed of the string
## Initial profile
x=-1:dx:1;
y=zeros(size(x)); ## Change the gaussian distribution
## to get zero vertical
##displacements for all parts of
##the string.(purely flat initially)
plot(x,y)
pause
##For the sinusoidally driven end, given constans are:
A=0.5; ##amplitude(m)
rate=100; ## inverse of the period (cycles/s)
omega=2*pi*rate ; ## angular frequency(Radians/s)
## Time boundary conditions. Get previous and
## present displacement as initial profile
ynow=y;
yprev=y;
Nsteps=2000;
## all steps are the same for the loop as the string_fixed
## apart from the function ynext
for n=1:Nsteps
ynext=propagate_driven(ynow,yprev,r,omega,A,n,dt);
plot(x,yn…

/*A C code that returns the magnetic field at a mesh of points
*on the xy-plane (xp,yp) for a straight wire segment through which
*current passes. The total magnetic field used in this code
*has the form (cos(theta1)-cos(theha2))/(yp*r_mag)
*ignoring the constants permeability, pi and current I and
*taking them together as 1. Thus no need for trapezoid since
*the integral of Bio-Savart can be tractable!!!
*r_mag represents the distance between the dl segment and
*the point (xp,yp) where we will find the B*/
#include <stdio.h>
#include <math.h>
/*Global constant*/
#define L 3.0 /* length of the wire*/
double r_mag(double x, double xp, double yp) {
double rx;
rx=xp-x; /* x is any point on xprime*/
return sqrt(rx*rx+yp*yp);
}
main()
{ int i;
double xp,yp;
double B; /*the z component of mag-field that is total field.*/
FILE *fid;
fid=fopen("mag-field","w");
/*loop over xp and yp values*/
for (yp=-2.0;yp<=2.0;yp=yp+0.1)
for (xp=-1.5;xp<=1.5;xp=xp+0.1)…

## A for good and evil as well as loopless
## total displacement function of random walk
## that takes in the step numbers as an
## argument and returns the displacement
## of the rnd walker.Note that the probabilities
## to turn rigth and left are equal but the left
## step is twice the rigth step.
## Usage : rw_uneven(N)
function rw_uneven(N)
rn=rand(N,1); ## If the element of a random vector
r=-(rn<0.5); ## is smaller than 0.5, then return -1,
li=find(r == 0); ## Else return 2(equal probability).
r(li)=2;
x=sum(r) ## total displacement
endfunction

## This function
## provides us with very simple
## random walk generator compared
## to the function rand_disc_loop
## which is time-consuming, in operation,
## long, and unuseful for other than the
## coin toss simulatons
function xy=rand_disc(N); ## RW generator.
r=floor(rand(N,1)*4); ## Random coulumn vector of N
## integer elements
## multiplied by 4 to widen the
## interval from [0,1] to [0,3].
x=y=zeros(size(r)); ## The coulumn vectors
## of N elements with all elements zero.
x(find(r==0)) = 1; ## The elements of x,
## the vector function
## which takes in the row #
## of the zero elements of
## the vector r as an argument,
## is assigned to 1.
x(find(r==1)) =-1; ## The elements of x,
## the vector function
## which takes in the row # of the
## elements of 1 of the vector r
## as an argument,
## is assigned to -1.
y(find(r==2)) = 1; ## The elements of y,
## the vector function
## which takes in the row # of the
## elements of 2 of the vector r
## as an argument,
## is assigned…

## The string is made up of two different mass density strings
## seperated in the middle. Choose the right one to be ligther
## than the left. So, since velocity is inversely propotional
## to the mass density, than the rigth one is faster also.
## r1=c1*dt/dx and r2=c1*dt/dx
function ynext=propagate_two_parts(ynow,yprev,r1,r2)
## initiallization for, take y as previos one
ynext=ynow;
## we have two parts, hence two loops are needed
## the first part is between x=0 and
## let y(now)/2 for better visiulation.
## floor(x) returns the largest integer not greater than x
## length(x) determines the number of column
## or rows in matrix or vector
##I started the 'for' from x=0 but didn't work
##then started from x=1
## but in this case the
## string_two_parts didn't work
## let x0=i0=2
for i=2:floor(length(ynow)/2)
ynext(i) = 2*(1-r1^2)*ynow(i)-yprev(i)+r1^2*(ynow(i+1)+ynow(i-1));
endfor
## the second part is between x0=(L1+L2) /2 and L1+L2 where
## L1 and L2 are string le…

## A 'realistic', 'non-elastic' string, which responses to any
## bending and has stifness. This script takes in the previous
## and the present profiles and iterates to find
## the profile in the next time step. The ratio 'r' is not 1
## like in the 'non-realistic' string since the speed of the wave
## always less than the speed of the string it should be less than 1
## for best and most stable solution
##constants
dx=1e-2 ## Spatial increment (m)
L=2 ## Length of the string (m)
M=L/dx ## Dimensionless partition
E=1e-4 ## Dimensionless stiffnes
function ynext=propagate_stiff(ynow,yprev,r)
## Quick and dirty way to fix boundary conditions -- for each step
## they are the same as the previous step.
ynext=ynow;
ynow(1)=ynow(2)=0;
##Entering the loop
for i=3:length(ynow)-1
## boundary condition
ynow(length(ynow)-1)=ynow(length(ynow)-2)=0;
## Divide the ynext with many terms into three parts for easiness
ynext(i)=(2−(2*r^2)−(6*E*(r^2)*(M^2)))*ynow(i)−yprev…

## Takes in the previous and the present profiles of the string and
## iterates to find the profile in the next time step.
function ynext=propagate_driven(ynow,yprev,r,omega,A,n,dt)
## initialization, boundary condition
ynext=ynow;
## takes in length(ynow) as an argument
## since one end of the string is driven sinuosidaly
## omega is the angular frequency of the driven force
## n is the time index, n*dt is t(n)
ynext(length(ynow))=A*sin(omega*n*dt);
## entering the loop
for i=2:length(ynow)-1
ynext(i) = 2*(1-r^2)*ynow(i)-yprev(i)+r^2*(ynow(i+1)+ynow(i-1));
endfor
endfunction

## 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

## Newton-Rapson Method to the smallest non negative root
## of the 8th degree Legendre Polynomial
## P8(x)=(1/128)(6435x^8-12012x^6+6930x^4-1260x^2+35)
## where -1<=x<=1.
## for the smallest non negative root, we can ignore
## all the terms except the last two by truncated
## the function to be zero and find
## x=0.167 as the initial smallest non negative
## root.
##Constants and initializations
x=[]; ## Empty array for the iterated x roots
x(1)=0.16700000; ## Initial guess to begin the iteration for the
## smallest non-negative root.
L8=[]; ## Empty array for the Legendre polynomial
L8p=[]; ## Empty array for the derivative of the Legendre polynomial
for i=1:100
##The value of the function at x
L8(i)=(1/128)*(6435*x(i)^8-12012*x(i)^6+6930*x(i)^4-1260*x(i)^2+35);
##The value of the derivative of the function at x
L8p(i)=(1/128)*(6435*8*x(i)^7-12012*6*x(i)^5+6930*4*x(i)^3-1260*2*x(i));
x(i+1)=x(i)-L8(i)/L8p(i); ## the iteration
endfor
## For plot let's define a new variable…

## A function takes in the number of diffusive particle
## as an argument and gets the initial positions in 2D
## as an outcome by inserting them in a square.
function p=initial_positions7(Nwalkers)
dt=3; ## to extend the interval of axes.
a=sqrt(Nwalkers); ## step size.
n=(a-1)/2; ## max range in the x and y axes.
if(rem(a,2)~=1) ## Nwalkers must be odd squared.
printf("The number you have entered is not an odd squared. Exiting...\n");
return
endif
n=(a-1)/2; ## max range in the x and y axes.
x=[-n:n]'; ## integer interval in the x axis.
p=zeros(a,2); ## initialize the positions of 'a' walkers.
p(:,1)=x; ## equate the column of p to x vector.
p=repmat(p,a,1); ## replicate positions of 'a' walkers to get a*a walkers.
for i=1:a:Nwalkers ## the loop through the number walkers.
m=(i+a-1)/a; ## index get the entries of x resp.
p(i:i+a-1,2)=x(m) ; ## y components are a-fold degenerate.
endfor
plot(p(:,1),p(:,2),'b*;;')
axis(dt*[-n, n,-n,n])
endfunction

## 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

## interaction energy for ising system between the spins seperated with distance r.
## J0 and alpha can be varied with respect to the ising system
function J=coupling(r,J0,alpha)
J=J0*exp((-r-1)/alpha);
Endfunction

## A function of the solution of the path
## equation of a proton under the inverse
## square atraction field of an electron
## that takes in the initial seperation distance
## or the position r0 wrt center of the electron
## as an argument and returns the total time
## ttotal it takes for it to reach within 1.0m
## of the electron and plots the velocity v
## vs time graph. usage ttotal=coulomb(r0).
function coulomb(r0)
## constants and initializations
k=9e+9; ## coulomb field constant [Nm^2/C]
q=1.6e-19; ## electronic charge [C]
mp=1.7e-27; ## proton mass [m]
dt=1e-4;; ## increment in time [sec]
r=r0; ## initial seperation[m]
t=0; ## initial time [s]
v0=0; ## initial velocity [m/s]
v=v0; ## 1st entry of v array [m/s]
n=1; ## initialization of loop index
## since we have already n=0 in argument r0.
while(r(n)>0.1);
dr=v(n)*dt; ## increment that is decrease in r
## since v(n) will be negative below.
r=[r;r(n)+dr]; ## decreases r in each step and
## accumulates the results in r array…