Skip to main content

Posts

STIFFNESS OF BENDING STRINGS

## 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);...

TRAVELING WAVE ON A NONREALISTIC STRING

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

BIO-SAVART LAW

/*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=-...

TOTAL DISPLACEMENT OF LOOPLESS RANDOM WALK

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

MEAN SQUARED DISPLACEMENT-1D RANDOM WALK

## Routine random walk(rw) for simulating ## one-dimensional random walk ## and calculating the mean squared displacement. x2ave=[]; ## Initial array of the squared ## displacement at initial time(meters). Nrw=3000; ## Number of walkers (Dimensionless). beg=1000; ## Minimum step number ( // ). inc=1000; ## Step number increment ( // ). Nstepsmax=10000; ## Maximum step number ( // ). dt=1; ## Time increment(second). ##Entering the nested loops. for Nsteps=beg:inc:Nstepsmax; ## The loop through the number Nsteps ## of steps each in walk. x2=0; ## Initial squared location of all of the ## walkers at all steps. for m=1:Nrw ## The loop through the desired ## number of walkers. r=rand_disc_rev(Nsteps,0.5); ## Calling the rw generator ## function which will give column vector ## of each elements are either 1 or -1. x2+=sum(r)^2; ## Total displacement squared whose ## elements stems from the vector r. endfor x2ave=[x2ave;x2/Nrw]; ## Accumulation of the squared displacem...

RANDOM WALK GENERATOR

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

PROPAGATION OF NON-UNIFORM STRING

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