Uploaded by aroob sheraz

programs of lab

advertisement
How to Generate Random Number?
int rnd;
Random obj = new Random();
for (int i = 0; i < 100; i++)
{
rnd= obj.Next(1, 7);
textBox1.Text = textBox1.Text + "
rnd.ToString();
"+
Average:
Random obj = new Random();
double sum = 0; double min = 100.05, max = 1005.25;
for (int i = 0; i < 100; i++)
{
sum = sum + (min + (max - min) * obj.NextDouble());
Generate:
Random obj = new Random();
int num = 100;
for (int i = 0; i < num; i++)
{
textBox1.Text = textBox1.Text + " " + obj.Next(1, 7);
textBox1.Refresh();
System.Threading.Thread.Sleep(20);
Min and Max:
Random obj = new Random();
double min = 100.05, max = 1005.25;
int num = 100;
for (int i = 0; i < num; i++)
{
textBox1.Text = textBox1.Text + " " + (min + (max - min) *
obj.NextDouble());
textBox1.Refresh();
System.Threading.Thread.Sleep(20);
1-D Random Walk:
(add in the top ) Using System.Drawing;
int nsteps = int.Parse(textBox1.Text);
int nwalks = int.Parse(textBox2.Text);
//declare one d array
float[] x2ave=new float[nsteps];
float x,delx=3;
double prob;
Graphics gg = CreateGraphics();
SolidBrush br = new SolidBrush(Color.Red);
SolidBrush bb = new SolidBrush(Color.Purple);
Random obj = new Random();
//start the walk
for (int w = 0; w < nwalks; w++)
{
x = 0;
for (int s = 0; s < nsteps; s++)
{
prob = obj.NextDouble();
if (prob<0.5)
{
x = x + delx;
}
else
{
x = x - delx;
x2ave[s] = x2ave[s]+x * x;
}
}//end of single walk
}//end of all walks
//cpmpute the mean square displacement
for (int s = 0; s < nsteps; s++)
{
x2ave[s] = x2ave[s] / nwalks;
//for mean square displacement
gg.FillEllipse(br, 200 + s * 10, 400 - x2ave[s], 4, 4);
//for root mean square displacement
gg.FillEllipse(br, 200 + s * 10, 400 - (float)Math.Sqrt(x2ave[s]), 4, 4);
Thread.Sleep(20);
}
2-D Random Walk:
this.Refresh();
int nsteps = int.Parse(textBox1.Text);
int nwalks = int.Parse(textBox2.Text);
//declare one d array
float[] r2ave = new float[nsteps];
float x, delx = 10;float y,dely=10;
double prob;
Graphics gg = CreateGraphics();
SolidBrush br = new SolidBrush(Color.Red);
SolidBrush bb = new SolidBrush(Color.Purple);
Random obj = new Random();
//start the walk
for (int w = 0; w < nwalks; w++)
{
x = 0; y = 0;
for (int s = 0; s < nsteps; s++)
{
prob = obj.NextDouble();
if (prob < 0.25)
{
x = x + delx;
}
if (prob >=0.25&&prob<0.5)
{
x = x - delx;
}
if (prob >=0.5&&prob<0.75)
{
y = y - dely;
}
if (prob >=0.75)
{
y = y+ dely;
r2ave[s] = r2ave[s] + x * x + y * y;
gg.FillEllipse(bb, 400 + x, 400 - y, 10, 10);
Thread.Sleep(10);
this.Refresh();
}
}//end of single walk
}//end of all walks
//cpmpute the mean square displacement
for (int s = 0; s < nsteps; s++)
{
r2ave[s] = r2ave[s] / nwalks;
//for mean square displacement
gg.FillEllipse(br, 200 + s * 10, 400 - r2ave[s], 4, 4);
//for root mean square displacement
gg.FillEllipse(br, 200 + s * 10, 400 - (float)Math.Sqrt(r2ave[s]), 4, 4);
}
Distance Using Random Numbers:
this.Refresh();
// THIS DIStance is parallel to x axis
float x1 = float.Parse(textBox1.Text);
float x2 = float.Parse(textBox2.Text);
float actualDistance =x2-x1;
float randomd;double r;
float xmin = x1 - 10, xmax = x2 + 10;
int n = 1000, n1 = 0;
Random obj = new Random();
Graphics gg = CreateGraphics();
SolidBrush rb = new SolidBrush(Color.Red);
SolidBrush bb = new SolidBrush(Color.Blue);
for (int i = 0; i < n; i++)
{r=xmin+(xmax-xmin)*obj.NextDouble();
if (r>=x1&&r<=x2)
{
n1++;
gg.FillEllipse(rb, 200 + (float)r*5, 400, 5, 5);
}
else
{
gg.FillEllipse(bb, 200 + (float)r*5, 400, 5, 5);
}
Thread.Sleep(10);
textBox3.Text = "counter="+(i++);
textBox3.Refresh();
}
randomd = (float)n1 / (float)n * (xmax - xmin);
float error = Math.Abs(actualDistance - randomd);
MessageBox.Show("the absolutue error is =" +error);
Area of Circle:
this.Refresh();
double xmax = double.Parse(textBox1.Text);
double ymax= double.Parse(textBox2.Text);
double x, y,xmin=0,ymin=0;
double randoma=0;
double Areaofsq = (xmax-xmin) * (ymax-ymin);
double n = 6000, num = 0;
Random obj = new Random();
Graphics gg = CreateGraphics();
SolidBrush rb = new SolidBrush(Color.Red);
SolidBrush bb = new SolidBrush(Color.Blue);
for (int i = 0; i < n; i++)
{
x =xmin+(xmax-xmin) * obj.NextDouble();
y = ymin+(ymax-ymin) * obj.NextDouble();
if (((x-5)*(x-5)+(y-5)*(y-5)) <= (xmax/2*xmax/2) )
{
num++;
gg.FillEllipse(rb, 400 + (float)x*15,300-(float)y*15,5,5);
}
else
gg.FillEllipse(bb, 400 + (float)x * 15, 300 - (float)y * 15, 5, 5);
textBox3.Text = "counter=" + (num);
textBox3.Refresh();
}
randoma = num /n * Areaofsq;
MessageBox.Show("the areaofcirc is =" + randoma);
double error = Math.Abs(Areaofsq - randoma);
MessageBox.Show("the absolutue error is =" + error);
Diffusion Process:
In button:
this.Refresh();
Graphics gg = CreateGraphics();
SolidBrush bb = new SolidBrush(Color.Red);
Density__Distribution obj = new Density__Distribution(gg, this, bb);
In class:
//first of all data
double xmin, xmax, delt, delx, D, sigma,t,x;
double [] rho;
int num;//number of points between xmin and xmax
//define needed functions
//first of all constructor
public Density(Graphics gg,Form1 frm,SolidBrush b)
{
t = 0.0001; xmin = -2; xmax = 2; D = 1; x = xmin;
delt = 0.5;sigma = Math.Sqrt(2 * D * t);
num = 1000; delx = (xmax - xmin) / (num- 1);
rho = new double[num];
float xc = frm.ClientSize.Width / 2;
//store the starting values of density
for (int i = 0; i < num; i++)
{
x = xmin+i*delx;
rho[i] = 1 / sigma * Math.Exp(-x * x / (2 * sigma * sigma));
gg.FillEllipse(b, xc + (float)x*400,
400 - (float)rho[i], 5, 5);
}
DLA:
In Button:
Single seed:
this.Refresh();
DLA obj = new DLA(this,0);
obj.Growth(false);
x-axis seed:
this.Refresh();
DLA obj = new DLA(this,1);
obj.Growth(false);
y-axis seed:
this.Refresh();
DLA obj = new DLA(this,2);
obj.Growth(false);
Circular seed:
this.Refresh();
DLA obj = new DLA(this,3);
obj.Growth(false);
In class:
//declare data variables
int size = 200, psize = 5;
int[,] cluster;
float x0, y0, dx, dy;//coordinates of centre form
int x, y;
double prob;
bool check;
Random obj;
//center coordinates of the textbox1
//Graphical setup
Graphics gg;
SolidBrush bb, br;
Pen bl;
//cluster represents the cluster region
//now define constuctor
public DLA(Form1 frm,int flag)
{
//gg = frm.textBox1.CreateGraphics();
//x0=(float)frm.textBox1.Width/2;
//y0=(float)frm.textBox1.Height/2;
//dx=(float)frm.textBox1.Width/(2*size);
//dy=(float)frm.textBox1.Height/(2*size);
gg = frm.CreateGraphics();
x0 = (float)frm.Width / 2;
y0 = (float)frm.Height / 2;
dx = (float)frm.Width / (2 * size);//step size in the direction of x axis(no of
intervals)
dy = (float)frm.Height / (2 * size);//step size in the direction of y axis(no of
intervals)
obj = new Random();
bb = new SolidBrush(Color.Blue);
br = new SolidBrush(Color.Red);
bl = new Pen(Color.Black);
cluster = new int[2 * size + 1, 2 * size + 1];// because the centre comes
from even numbers this is why we take odd numbers of array
//intialization of the cluster array
for (int i = 0; i < (2 * size + 1); i++)
{
for (int j = 0; j < (2 * size + 1); j++)
{
cluster[i, j] = 0;
gg.DrawEllipse(bl, j * dx, i * dy, psize, psize);
if (flag == 0)
{
if (i == size && j == size)
{
cluster[i, j] = 1;
gg.FillEllipse(br, j * dx, i * dy, psize, psize);
}
}
if (flag == 1)
{
if (i == size )
{
cluster[i, j] = 1;
gg.FillEllipse(br, j * dx, i * dy, psize, psize);
}
}
if (flag == 2)
{
if (j == size)
{
cluster[i, j] = 1;
gg.FillEllipse(br, j * dx, i * dy, psize, psize);
}
}
if (flag == 3)
{
if (Math.Abs((i - size) * (i - size) + (j - size) * (j - size) - 100) < 0.1)
{
cluster[i, j] = 1;
gg.FillEllipse(br, j * dx, i * dy, psize, psize);
}
}
//seed has been placed
}
}
}//end of constructor
//now define other functions
public void Growth(bool filled)
{
while (!filled)
{
for (int i = 1; i < (2 * size); i++)
{
for (int j = 1; j < (2 * size); j++)
{
x = obj.Next(1, 2 * size);
y = obj.Next(1, 2 * size);
if (cluster[x, y] == 0)
{
if (decide(x, y))
{
cluster[x, y] = 1;
//Thread.Sleep(1);
gg.FillEllipse(br, y * dx, x * dy, 4, 4);
}
else
{
while (!decide(x, y))
{
prob = obj.NextDouble();
if (prob <= 0.25)
if (x < 2 * size - 1)
x++;
if (prob > 0.25 && prob <= 0.5)
if (x > 1)
x--;
if (prob > 0.5 && prob <= 0.75)
if (y < 2 * size - 1)
y++;
if (prob > 0.75)
if (y > 1)
y--;
}
cluster[x, y] = 1;
//Thread.Sleep(1);
gg.FillEllipse(br, y * dx, x * dy, 4, 4);
}
}
// Thread.Sleep(10);
}
}
}
}
//end of growth function
public bool decide(int x, int y)
{
if (cluster[x - 1, y] == 1) check = true;
else if (cluster[x + 1, y] == 1) check = true;
else if (cluster[x, y - 1] == 1) check = true;
else if (cluster[x, y + 1] == 1) check = true;
else check = false;
return check;
}
EDEN Cluster:
In class:
class EdenCluster
{
//declare data variables
int size=30,psize=5;
int[,] cluster;
float x0, y0,dx,dy;
double prob;
Random obj;
//center coordinates of the textbox1
//Graphical setup
Graphics gg;
SolidBrush bb, br;
Pen bl;
//cluster represents the cluster region
//now define constuctor
public EdenCluster(Form1 frm)
{
//gg = frm.textBox1.CreateGraphics();
//x0=(float)frm.textBox1.Width/2;
//y0=(float)frm.textBox1.Height/2;
//dx=(float)frm.textBox1.Width/(2*size);
//dy=(float)frm.textBox1.Height/(2*size);
gg = frm.CreateGraphics();
x0 = (float)frm.Width / 2;
y0 = (float)frm.Height / 2;
dx = (float)frm.Width / (2 * size);
dy = (float)frm.Height / (2 * size);
obj = new Random();
bb=new SolidBrush(Color.Blue);
br=new SolidBrush(Color.Red);
bl = new Pen(Color.Black);
cluster = new int[2 * size + 1, 2 * size + 1];
//intialization of the cluster class
for (int i = 0; i < (2 * size + 1); i++)
{
for (int j = 0; j < (2 * size + 1); j++)
{
cluster[i, j] = 0;
gg.DrawEllipse(bl, j * dx, i * dy, psize, psize);
if (i == size && j == size)
{
cluster[i, j] = 1;
gg.FillEllipse(br, j * dx, i * dy, psize, psize);
}
//seed has been placed
}
}
}//end of constructor
//now define other functions
public void Growth(bool filled)
{
while (!filled)
{
for (int i = 1; i < (2 * size); i++)
{
for (int j = 1; j < (2 * size); j++)
{
prob = obj.NextDouble();
if (cluster[i, j] == 1)
{
if (prob > 0.25 && prob <= 0.5)
{
if (cluster[i, j - 1] == 0)
cluster[i, j - 1] = 1;
gg.FillEllipse(br, (j-1) * dx, i * dy, psize, psize);
}
if (prob <= 0.25)
{
if (cluster[i, j + 1] == 0)
cluster[i, j + 1] = 1;
gg.FillEllipse(br, (j+1) * dx, i * dy, psize, psize);
}
if (prob > 0.75)
{
if (cluster[i - 1, j] == 0)
cluster[i - 1, j] = 1;
gg.FillEllipse(br, j * dx, (i-1) * dy, psize, psize);
}
if (prob > 0.5 && prob <= 0.75)
{
if (cluster[i + 1, j] == 0)
cluster[i + 1, j] = 1;
gg.FillEllipse(br, j * dx, (i+1) * dy, psize, psize);
}
}
// Thread.Sleep(200);
In Button:
textBox1.Refresh();
EdenCluster cluster = new EdenCluster(this);
cluster. Growth(false);
Download