43
Index S.N o Name Of Program Signatur e 1. Generate Uniformly Distributed Random Numbers (Repetitive or Non-Repetitive) using Quick and Dirty method. 2. Generate Uniformly Distributed Random Numbers (Repetitive or Non-Repetitive) using Power Residue method. 3. Generate Non Uniformly Distributed Random Numbers from an Exponential Distribution using Inverse Transformation. 4. Generate Non Uniformly Distributed Random Numbers from a Normal Distribution using Box- Muller Transformation. 5. Generate Non Uniformly Distributed Random Numbers from Poisson Distribution for any specified lambda. 6. Generate Non Uniformly Distributed Random Numbers from an Erlang-m Distribution with a mean time (m*beta) units. 7. Simulation of Inventory System. 8. Simulation of a Pure-Pursuit Problem. 9. Find the Square-Root of 3 using Monte-Carlo Simulation.

Simulation Programs (2)

Embed Size (px)

Citation preview

Page 1: Simulation Programs (2)

Index

S.No Name Of Program Signature

1.Generate Uniformly Distributed Random Numbers (Repetitive

or Non-Repetitive) using Quick and Dirty method.

2.Generate Uniformly Distributed Random Numbers (Repetitive

or Non-Repetitive) using Power Residue method.

3.Generate Non Uniformly Distributed Random Numbers from an

Exponential Distribution using Inverse Transformation.

4.Generate Non Uniformly Distributed Random Numbers from a

Normal Distribution using Box-Muller Transformation.

5.Generate Non Uniformly Distributed Random Numbers from

Poisson Distribution for any specified lambda.

6.Generate Non Uniformly Distributed Random Numbers from an

Erlang-m Distribution with a mean time (m*beta) units.

7. Simulation of Inventory System.

8. Simulation of a Pure-Pursuit Problem.

9. Find the Square-Root of 3 using Monte-Carlo Simulation.

10. To simulate chemical reactor

11. Simulation of CPM method

12.Simulation of Second Order Servo System using Runge Kutta

Integration formula

13. Implement Chi Square test

14. Simulation of A Single-Server Queuing System

Page 2: Simulation Programs (2)

Program #1AIM: Generate Uniformly Distributed Random Numbers (Repetitive or Non-Repetitive) using Quick and Dirty method.

INPUT PARAMETERS: (Limits) a, b; (Number of Random Numbers) n

OUTPUT PARAMETERS: (Random Numbers Array) random

ROUTINES USED: rand()

SOURCE CODE:

#include <stdio.h>

float seed;

float rand(){

int temp;seed = (seed + 3.14159) * (seed + 3.14159);temp = seed;seed = seed - temp;return (seed);

}

int main(){

int n,a,b;int choice,i,j,*random;while (1){

printf ("Options:\n");printf ("1. Repetitive\n");printf ("2. Non Repetitive\n");printf ("3. Exit\n");printf ("Enter your choice\t");scanf ("%d", &choice);if (choice == 3)

return 0;if ((choice < 1) || (choice > 3)){

printf ("Wrong choice...Enter Again.\n");continue;

}printf ("\nEnter the required number of random numbers:\t");

Page 3: Simulation Programs (2)

scanf ("%d", &n);random = (int*) malloc(n);printf ("Enter the limits:\t");scanf ("%d%d", &a, &b);seed= 0.714563;printf (“Random Number are:\n");for (i=0; i<n; i++){

random[i] = a + ((b-a) * rand());if (choice = = 2){

for (j=0; j<i; j++){

if (random[i] == random[j]){

i--;break;

}}

}}for (i=0; i<n; i++){

printf ("%d\n",random[i]);}free (random);

}

}

OUTPUT

Options:1. Repetitive2. Non Repetitive3. ExitEnter your choice 1

Enter the required number of random numbers: 20Enter the limits: 25 75Random Number are:68294772

Page 4: Simulation Programs (2)

62342948305044477369364661737046

Options:1. Repetitive2. Non Repetitive3. ExitEnter your choice 2

Enter the required number of random numbers: 20Enter the limits: 25 75Random Number are:6829477262344830504473693646617066742563

Page 5: Simulation Programs (2)

Program #2AIM: Generate Uniformly Distributed Random Numbers (Repetitive or Non-Repetitive) using Power Residue method.

INPUT PARAMETERS: (Limits) A, B; (Number of Random Numbers) n

OUTPUT PARAMETERS: (Random Numbers Array) random

SOURCE CODE:

#include <stdio.h>#include <math.h>#include <limits.h>

int main(){

int n, A, B;int a, b;long int m, seed;int choice, i, j, *random;double rand;while (1){

printf ("Options:\n");printf ("1. Repetitive\n");printf ("2. Non Repetitive\n");printf ("3. Exit\n");printf ("Enter your choice\t");scanf ("%d", &choice);if (choice == 3)

return 0;if ((choice < 1) || (choice > 3)){

printf ("Wrong choice...Enter Again.\n");continue;

}printf ("\nEnter the required number of random numbers:\t");scanf ("%d", &n);random = (int*) malloc(n);printf ("Enter the limits:\t");scanf ("%d%d", &A, &B);b = sizeof(int)*8;a = pow(2,(b/2)) + 3;m = pow(2,b-1);seed = m-1;

Page 6: Simulation Programs (2)

printf ("Random Number are:\n");for (i=0; i<n; i++){

seed = (seed * a) % m;rand = (double)seed / m;random[i] = A + ((B-A) * rand);if (choice == 2){

for (j=0; j<i; j++){

if (random[i] == random[j]){

i--;break;

}}

}}for (i=0; i<n; i++){

printf ("%d\n",random[i]);}free (random);

}}

OUTPUT:

Options:1. Repetitive2. Non Repetitive3. ExitEnter your choice 1

Enter the required number of random numbers: 20Enter the limits: 25 75Random Number are:7472643266542830

Page 7: Simulation Programs (2)

284828376872717241476253

Options:1. Repetitive2. Non Repetitive3. ExitEnter your choice 2

Enter the required number of random numbers: 20Enter the limits: 25 75Random Number are:7472643266542830483768714147625339594961

Page 8: Simulation Programs (2)

Program #3AIM: Generate Non Uniformly Distributed Random Numbers from an Exponential Distribution using Inverse Transformation.

INPUT PARAMETERS: lambda; (Number of Random Numbers) n

OUTPUT PARAMETERS: (Random Numbers) random

ROUTINES USED: rand()

SOURCE CODE:

#include <stdio.h>#include <math.h>

float seed = 0.714563;

float rand(){

int temp;seed = (seed + 3.14159) * (seed + 3.14159);temp = seed;seed = seed - temp;return (seed);

}

int main(){

int n,a,b;int i;float lambda,random,u;clrscr();printf ("\nEnter the required number of random numbers:\t");scanf ("%d", &n);printf ("Enter the value of lambda\t");scanf ("%f", &lambda);printf ("Random Numbers from an Exponential Distribution using Inverse

Transformation are:\n");

for (i=0; i<n; i++){

u = rand();random = ((-1) / lambda) * log(u);printf ("%f\n",random);

}

Page 9: Simulation Programs (2)

getch();return 0;

}

OUTPUT:

Enter the required number of random numbers: 10Enter the value of lambda 3.8Random Numbers from an Exponential Distribution using Inverse Transformation are:0.0366730.6273710.2059160.0130030.0736900.4378900.6183780.1942460.6021180.173405

Page 10: Simulation Programs (2)

Program #4AIM: Generate Non Uniformly Distributed Random Numbers from a Normal Distribution using Box-Muller Transformation.

INPUT PARAMETERS: (Expected Mean) mu; (Standard Deviation) sigma, (Number of Random Numbers) n

OUTPUT PARAMETERS: (Random Numbers) x

ROUTINES USED: rand1(), rand2()

SOURCE CODE:

#include <stdio.h>#include <math.h>

float seed1 = 0.714563;float seed2 = 0.714567;

float rand1(){

int temp;seed1 = (seed1 + 3.14159) * (seed1 + 3.14159);temp = seed1;seed1 = seed1 - temp;return (seed1);

}

float rand2(){

int temp;seed2 = (seed2 + 3.14159) * (seed2 + 3.14159);temp = seed2;seed2 = seed2 - temp;return (seed2);

}int main(){

int n,a,b;int i;float mu,sigma,random,u1,u2,x;clrscr();printf ("\nEnter the required number of random numbers:\t");scanf ("%d", &n);printf ("Enter the Expected Mean(Mu) and Standard Deviation(Sigma):\n");

Page 11: Simulation Programs (2)

scanf ("%f%f", &mu, &sigma);printf ("Random Numbers from a Normal Distribution using Box-Muller

Transformation are:\n");for (i=0; i<n; i++){

u1 = rand1();u2 = rand2();random = pow( ( (-2) * log(u1) ),0.5 ) * cos(6.283 * u2);x = sigma * random + mu;printf ("%f\n",x);

}getch();return 0;

}

OUTPUT:

Enter the required number of random numbers: 10Enter the Expected Value(Mu) and Standard Deviation(Sigma):35 10Random Numbers from a Normal Distribution using Box-Muller Transformation are:38.61206153.25641622.90625838.05959739.35877251.23300613.81494022.87518123.96037344.462170

Page 12: Simulation Programs (2)

Program #5AIM: Generate Non Uniformly Distributed Random Numbers from Poisson Distribution for any specified lambda.

INPUT PARAMETERS: lambda, (Number of Random Numbers) n

OUTPUT PARAMETERS: (Random Numbers) k

ROUTINES USED: rand()

SOURCE CODE:

#include <stdio.h>#include <math.h>

float seed = 0.714563;

float rand(){

int temp;seed = (seed + 3.14159) * (seed + 3.14159);temp = seed;seed = seed - temp;return (seed);

}

int main(){

int n,k;int i;float lambda,product,z;clrscr();printf ("\nEnter the required number of random numbers:\t");scanf ("%d", &n);printf ("Enter the value of lambda\t");scanf ("%f", &lambda);z = exp( (-1) * lambda);printf ("Random Samples from a Poisson Distribution are:\n");for (i=0; i<n; i++){

k = 0;product = 1 * rand();while (product >= z){

k++;

Page 13: Simulation Programs (2)

product *= rand();}printf ("%d\n", k);

}getch();return 0;

}

OUTPUT:

Enter the required number of random numbers: 10Enter the value of lambda 6Random Samples from a Poisson Distribution are:6786843726

Page 14: Simulation Programs (2)

Program #6AIM: Generate Non Uniformly Distributed Random Numbers from an Erlang-m Distribution with a mean time (m*beta) units.

INPUT PARAMETERS: (No. of services) m, (Average service time) beta, (Number of Random Numbers) n

OUTPUT PARAMETERS: (Random Numbers) erlang

ROUTINES USED: rand()

SOURCE CODE:

#include <stdio.h>#include <math.h>

float seed = 0.714563;

float rand(){

int temp;seed = (seed + 3.14159) * (seed + 3.14159);temp = seed;seed = seed - temp;return (seed);

}

int main(){

int n,m,erlang;int i,j;float beta,product;clrscr();printf ("\nEnter the required number of random numbers:\t");scanf ("%d", &n);printf ("Enter the number of services and average service time:\t");scanf ("%d%f", &m, &beta);printf ("Random Samples from an Erlang-m Distribution are:\n");for (i=0; i<n; i++){

product = 1;for (j=0; j<m; j++){

product *= rand();}

Page 15: Simulation Programs (2)

erlang = (-1) * beta * log(product);printf ("%d\n", erlang);

}getch();return 0;

}

OUTPUT:

Enter the required number of random numbers: 10Enter the number of services and average service time: 3 7Random Samples from an Erlang-m Distribution are:231337161187292211

Page 16: Simulation Programs (2)

Program #7AIM: Simulation of Inventory System.

INPUT PARAMETERS: (Re-Order Point) point, (Re-Order Quantity) quantity

OUTPUT PARAMETERS: (Total Cost Incurred) cost

ROUTINES USED: rand()

SOURCE CODE:

#include <stdio.h>#include <math.h>

float seed = 0.7614593;

float rand(){

int temp;seed = (seed + 3.14159) * (seed + 3.14159);temp = seed;seed = seed - temp;return (seed);

}

int main(){

int point, quantity;int stock, equi_stock, units_due, due_date, demand;float cost;int i;

/*initialize values*/cost = 0;stock = 115;units_due = 0;due_date = 0;

printf ("Enter Re-Order Point & Re-Order Quantity\n");scanf ("%d%d", &point, &quantity);/*Simulate for specified number of days(180)*/for(i=1; i<=180; i++){

/* If today(i) is due date..add quantity ordered to stock. No more units due.. */

Page 17: Simulation Programs (2)

if (due_date == i){

stock += quantity;units_due = 0;

}

demand = (int) rand() * 100;

/* If demand is more than stock,, add loss of gud-will cost to cost*/if (demand > stock){

cost += ((demand - stock) * 18.0);stock = 0;

}/* else add carrying cost to cost */else{

cost += (stock * 0.75);stock -= demand;

}

equi_stock = stock + units_due;/* If equivalent stock is less than equal to Reorder Point,, Reorder

Quantity */if (equi_stock <= point ){

units_due = quantity;due_date += 3;/* add cost of placing reorder to cost */cost += 75;

}}

printf (" Total Cost incurred: %f", cost);getch();return 0;

}

OUTPUT:

Enter Re-Order Point & Re-Order Quantity300 350Total Cost incurred: 142098.000000

Page 18: Simulation Programs (2)

Program #8AIM: Simulation of a Pure-Pursuit Problem.

INPUT PARAMETERS: (Path of bomber) xb, yb ; (Intial Position of Fighter) xf[0], yf[0]; (Velocity of Fighter) vf

OUTPUT PARAMETERS: (Time and Position at which target is caught) t, dist

SOURCE CODE:

#include <stdio.h>#include <math.h>

int main(){

float xb[15],yb[15], xf[15],yf[15], vf;int t,i;float dist;

printf ("Enter the path of Bomber in xy-coordinates..\n");for (t=0; t<=12; t++){

printf ("At time %d:\t",t);scanf ("%f%f", &xb[t], &yb[t]);

}

printf ("Enter the Initial Position of Fighter in xy-coordinates..\n");scanf("%f%f", &xf[0], &yf[0]);

printf ("Enter velocity of Fighter plane..\t");scanf ("%f", &vf);

/*Calculate distance for each new position of fighter plane*/for (t=0; t<=12; t++){

dist = sqrt( pow((yb[t] - yf[t]) ,2) + pow((xb[t] - xf[t]) ,2) );/* If distance between Fighter and Bomber is less than 10kms, fire...*/if (dist < 10){

printf ("Target caught at time: %d mins and %5.3f mts apart\n", (t+1), dist);

return 0;}

xf[t+1] = xf[t] + vf * (xb[t] - xf[t]) / dist;

Page 19: Simulation Programs (2)

yf[t+1] = yf[t] + vf * (yb[t] - yf[t]) / dist;}/* Target is not caught till end of specified time...pursuit is abondoned!! */printf ("Target Escaped..\n");getch();return 0;

}

OUTPUT:

Enter the path of Bomber in xy-coordinates..At time 0: 80 0At time 1: 90 -2At time 2: 99 -5At time 3: 108 -9At time 4: 116 -15At time 5: 125 -18At time 6: 133 -23At time 7: 141 -29At time 8: 151 -28At time 9: 160 -25At time 10: 169 -21At time 11: 179 -20At time 12: 180 -17Enter the Initial Position of Fighter in xy-coordinates..0 50Enter velocity of Fighter plane.. 20Target caught at time: 10 mins and 2.969 mts apart

Page 20: Simulation Programs (2)

Program #9AIM: Find the Square-Root of 3 using Monte-Carlo Simulation.

INPUT PARAMETERS: (Size of random sample) n

OUTPUT PARAMETERS: (Square-Root of 3) sqrt3

ROUTINES USED: rand()

SOURCE CODE:

#include <stdio.h>#include <math.h>

float seed = 0.714565;

float rand(){

int temp;seed = (seed + 3.14159) * (seed + 3.14159);temp = seed;seed = seed - temp;return (seed);

}

int main(){

int i,n,p=0;float random,sqrt3;clrscr();printf ("Enter the size of random sample you want to generate..\t");scanf ("%d", &n);for (i=0; i<n; i++){

random = rand() + 1;/* Count No. of samples satisfying the boundary condition..*/if ( (random*random) < 3)

p++;}sqrt3 = 1 + (float)p/n;printf ("\n The Square root of 3 is: %5.3f\n", sqrt3);getch();return 0;

}

Page 21: Simulation Programs (2)

OUTPUT:

Enter the size of random sample you want to generate.. 1250

The Square root of 3 is: 1.727

Page 22: Simulation Programs (2)

Program #10AIM: To simulate chemical reactorINPUT PARAMETERS: Initial amounts of a and bOUTPUT PARAMETERS: The amount of a,b and c at different time instantsSOURCE CODE: //chemical reactor void main(){ int i; float t=0; float a[50],b[50],c[50],delta; float k1,k2; a[0] = 100.0; b[0]= 50.0; c[0] = 0.0; delta = .1; k1 = .008; k2 = .002; printf("TIME A[I] B[I] C[I]\n"); for( i=0;i<50;i++) { printf(" %f %f %f %f\n",t,a[i],b[i],c[i]); a[i+1] = a[i] +( k2*c[i] - k1*a[i]*b[i])*delta; b[i+1] = b[i] +( k2*c[i] - k1*a[i]*b[i])*delta; c[i+1] = c[i] + (2*(k1*a[i]*b[i]) -k2*c[i])*delta; t = t+ 0.1; } getch(); }

OUTPUT

TIME A[I] B[I] C[I] 0.000000 100.000000 50.000000 0.000000 0.100000 96.000000 46.000000 8.000001 0.200000 92.468803 42.468800 15.064001 0.300000 89.330185 39.330181 21.344252 0.400000 86.523758 36.523750 26.961378 0.500000 84.001015 34.001003 32.012260 0.600000 81.722519 31.722511 36.575649 0.700000 79.655876 29.655872 40.716244 0.800000 77.774208 27.774204 44.487724 0.900000 76.055016 26.055008 47.935013 1.000000 74.479309 24.479303 51.096008 1.100000 73.030968 23.030962 54.002911

Page 23: Simulation Programs (2)

1.200000 71.696190 21.696184 56.683270 1.300000 70.463097 20.463093 59.160786 1.400000 69.321411 19.321411 61.455982 1.500000 68.262192 18.262192 63.586712 1.600000 67.277618 17.277615 65.568581 1.700000 66.360817 16.360811 67.415298 1.800000 65.505730 15.505721 69.138962 1.900000 64.706985 14.706978 70.750275 2.000000 63.959820 13.959812 72.258759 2.100000 63.259979 13.259971 73.672897 2.200000 62.603653 12.603644 75.000282 2.300000 61.987427 11.987417 76.247734 2.400000 61.408222 11.408212 77.421394 2.500000 60.863258 10.863250 78.526802 2.600000 60.350025 10.350017 79.568970 2.700000 59.866241 9.866231 80.552452 2.799999 59.409828 9.409818 81.481392 2.899999 58.978897 8.978886 82.359550 2.999999 58.571716 8.571706 83.190384 3.099999 58.186707 8.186696 83.977043 3.199999 57.822418 7.822406 84.722420 3.299999 57.477516 7.477502 85.429169 3.399999 57.150772 7.150757 86.099747 3.499999 56.841057 6.841040 86.736404 3.599999 56.547321 6.547306 87.341217 3.699999 56.268604 6.268588 87.916122 3.799999 56.004009 6.003992 88.462898 3.899998 55.752705 5.752686 88.983200 3.999998 55.513920 5.513900 89.478569 4.099998 55.286938 5.286917 89.950432 4.199998 55.071091 5.071069 90.400116 4.299998 54.865757 4.865734 90.828865 4.399998 54.670353 4.670330 91.237839 4.499998 54.484337 4.484314 91.628120 4.599998 54.307201 4.307180 92.000717 4.699998 54.138474 4.138452 92.356575 4.799998 53.977707 3.977683 92.696587 4.899998 53.824482 3.824458 93.021576

Page 24: Simulation Programs (2)

Program #11AIM: Simulation of CPM method

INPUT PARAMETERS: s[8],f[8],t[8]

OUTPUT PARAMETERS: ENT[6],EST[8],EFT[8],tmin,LNT[6],LST[8],LFT[8]

SOURCE CODE:#include<iostream.h>#include<stdio.h>#include<conio.h>#include<math.h>int main(){

int n=7,m=5;double s[8],f[8];double t[8],ENT[6],EST[8],EFT[8],tmin;double LNT[6],LST[8],LFT[8];int mark[8];int snode,fnode,sn,fn;clrscr();cout<<"****CRITICAL PATH METHOD****\n\n";cout<<"Read Starting nodes for each activity"<<endl;for(int i=1;i<=7;i++){

cout<<"Starting node for Activity"<<i<<":";cin>>s[i];cout<<"\n";

}

cout<<"\n Read Finishing nodes for each activity"<<endl;for(i=1;i<=7;i++){

cout<<"Finishing node for Activity"<<i<<":";cin>>f[i];cout<<"\n";

}

cout<<"\n Read Duration for Activity"<<endl;for(i=1;i<=7;i++){

cout<<"Duration for Activity"<<i<<":";cin>>t[i];cout<<"\n";

}

Page 25: Simulation Programs (2)

for(i=1;i<=5;i++){

ENT[i]=0;}

int k=1;while(k<=n){

snode=s[k];fnode=f[k];EST[k]=ENT[snode];EFT[k]=EST[k]+t[k];

if(EFT[k]>ENT[fnode]){

ENT[fnode]=EFT[k];}k=k+1;

}

tmin=ENT[m];cout<<"TMIN"<<":"<<tmin<<endl;cout<<"EST"<<"=(";

for(int j=1;j<=7;j++){

cout<<EST[j]<<";";}cout<<")";

cout<<endl<<"EFT"<<"=(";for(j=1;j<=7;j++){

cout<<EFT[j]<<";";}cout<<")";

cout<<endl<<"ENT"<<"=(";for(j=1;j<=5;j++){

cout<<ENT[j]<<";";}cout<<")";

Page 26: Simulation Programs (2)

for(int q=1;q<=5;q++){

LNT[q]=tmin;

}int d=7;while(d>0){

sn=s[d];fn=f[d];

LFT[d]=LNT[fn];LST[d]=LFT[d]-t[d];mark[d]=0;

if(LST[d]<LNT[sn]){

LNT[sn]=LST[d];}

d=d-1;}

cout<<"\n LST"<<"=(";for(j=1;j<=7;j++){

cout<<LST[j]<<";";}cout<<")";

cout<<endl<<"LFT"<<"=(";for(j=1;j<=7;j++){

cout<<LFT[j]<<";";}cout<<")";

cout<<endl<<"LNT"<<"=(";for(int l=1;l<=5;l++){

cout<<LNT[l]<<";";}cout<")";double h;

cout<<"\n Critical path is"<<endl;

Page 27: Simulation Programs (2)

for(int e=1;e<=7;e++){ h=abs(LST[e]-EST[e]);

if(h==0) {

mark[e]=1; cout<<e;

}

}getch();

return 0;

}

OUTPUT:****CRITICAL PATH METHOD****

Read Starting nodes for each activityStarting node for Activity1:1Starting node for Activity2:1Starting node for Activity3:2Starting node for Activity4:2Starting node for Activity5:3Starting node for Activity6:3Starting node for Activity7:4

Read Finishing nodes for each activityFinishing node for Activity1:2Finishing node for Activity2:3Finishing node for Activity3:4Finishing node for Activity4:3Finishing node for Activity5:5Finishing node for Activity6:4Finishing node for Activity7:5

Read Duration for ActivityDuration for Activity1:5.1Duration for Activity2:7.2Duration for Activity3:6.0Duration for Activity4:4.5Duration for Activity5:15.8Duration for Activity6:0

Page 28: Simulation Programs (2)

Duration for Activity7: 2.5

TMIN: 25.4EST= (0; 0; 5.1; 5.1; 9.6; 9.6; 11.1)EFT= (5.1; 7.2; 11.1; 9.6; 25.4; 9.6; 13.6)ENT= (0; 5.1; 9.6; 11.1; 25.4)LST= (0; 2.4; 16.9; 5.1; 9.6; 22.9; 22.9)LFT= (5.1; 9.6; 22.9; 9.6; 25.4; 22.9; 25.4)LNT= (0; 5.1; 9.6; 22.9; 25.4)Critical path is 1 4 5

Page 29: Simulation Programs (2)

Program #12AIM: Simulation of Second Order Servo System using Runge Kutta Integration formula

OUTPUT PARAMETERS: t,y1,y2

SOURCE CODE:

#include<iostream.h>#include<conio.h>#include<stdio.h>int main(){

float h,t,y1,y2,n,a;float u11,u12,u21,u22,u31,u32,u41,u42;h=0.1;t=0;y1=1;y2=0;n=20;a=0.1;clrscr();cout<<"****SIMULATION OF SERVO SYSTEM****\n";cout<<"Time(t)"<<"\t\t"<<"Y1"<<"\t\t"<<"Y2"<<endl;for(int i=1;i<=n;i++){ u11=h*y2; u12=h*(a*(1-(y1*y1))*y2-y1); u21=h*(y2+(0.5*u12)); u22=h*(a*(1-(y1+(0.5*u11))*(y1+(0.5*u11)))*(y2+(0.5)*u12)-(y1+(0.5*u11))); u31=h*(y2+(0.5*u22)); u32=h*(a*(1-(y1+(0.5*u21))*(y1+(0.5*u21)))*(y2+(0.5)*u22)-(y1+(0.5*u21))); u41=h*(y2+u32); u42=h*(a*(1-(y1+u31)*(y1+u31))*(y2+u32)-(y1+u31)); y1=y1+((u11+(2*u21)+(2*u31)+u41)/6); y2=y2+((u12+(2*u22)+(2*u32)+u42)/6); t=t+h; printf("%.2f\t\t%.2f\t\t%.2f\n",t,y1,y2);}getch();return 0;

}

Page 30: Simulation Programs (2)

OUTPUT:

****SIMULATION OF SERVO SYSTEM****Time(t) Y1 Y20.00 1.00 0.000.10 1.00 -0.100.20 0.98 -0.200.30 0.96 -0.300.40 0.92 -0.390.50 0.88 -0.480.60 0.82 -0.570.70 0.76 -0.650.80 0.70 -0.730.90 0.62 -0.801.00 0.54 -0.861.10 0.45 -0.911.20 0.35 -0.961.30 0.26 -1.001.40 0.15 -1.031.50 0.05 -1.051.60 -0.06 -1.061.70 -0.16 -1.061.80 -0.27 -1.051.90 -0.37 -1.03

Page 31: Simulation Programs (2)

Program #13AIM: Implement Chi Square test

INPUT PARAMETERS: cl,ex[10],ob[10] OUTPUT PARAMETERS: a[11][2] SOURCE CODE:

#include<stdio.h>#include<conio.h>#include<math.h>void main(){

float a[11][2]={{99.5,1.73},{99,2.09},

{95,3.33}, {90,4.17}, {75,5.90}, {50,8.34}, {25,11.4}, {10,14.7}, {5,16.9}, {1,21.7}, {.5,23.6}};

float ob[10]={0},ex[10]={0},chi_sq,temp=0.000000,pj;int cl,i;printf("\nenter how many classes are there ");scanf("%d",&cl);for(i=0;i<cl;i++){ printf("\nenter expected and observed value of class %d ",i+1); scanf("%f%f",&ex[i],&ob[i]);}for(i=0;i<cl;i++){ pj=ob[i]-ex[i];

pj=pj*pj; temp+=pj/ex[i];

}for(i=0;i<11;i++){ if(temp<a[i][1]) {

break; }

Page 32: Simulation Programs (2)

}printf("\n%f level of confidence that random no's are uniformly distributed ",a[i]

[0]);getch();

}

OUTPUT:

enter how many classes are there 10enter expected and observed value of class 1 500 468enter expected and observed value of class 2 500 519enter expected and observed value of class 3 500 480enter expected and observed value of class 4 500 495enter expected and observed value of class 5 500 508enter expected and observed value of class 6 500 426enter expected and observed value of class 7 500 497enter expected and observed value of class 8 500 515enter expected and observed value of class 9 500 463enter expected and observed value of class 10 500 52999.5 level of confidence that random no's are uniformly distributed

Page 33: Simulation Programs (2)

Program #13AIM: Simulation of A Single-Server Queuing System

OUTPUT PARAMETERS: I,cat[20],cdt[20],ql[20],wt[20],i,id[20]

SOURCE CODE:

#include<stdio.h>#include<conio.h>void main(){

int at[8]={0,10,15,35,30,10,5,5};int st[8]={20,15,10,5,15,15,10,10};int i,cdt[20],cat[20],wt[20],id[20],ql[20],dif;cat[0]=0;cdt[0]=20;ql[0]=0;id[0]=0;for(i=1;i<=7;i++)cat[i]=cat[i-1]+at[i];for(i=1;i<=8;i++){

dif=cdt[i-1]-cat[i];if(dif>0){

ql[i]=ql[i-1]+1;id[i]=0;wt[i]=dif;

}else{

ql[i]=ql[i-1]-1;id[i]=-dif;wt[i]=0;

}cdt[i]=cat[i]+wt[i]+st[i];

}for(i=0;i<=7;i++)

printf("\ni=%d\tcat=%d\tcdt=%d\tid=%d\tql=%d\twt=%d",i,cat[i],cdt[i],id[i],ql[i],wt[i]);

getch();}

Page 34: Simulation Programs (2)

OUTPUT:

***** 1 SERVER *****

JOB 1AT=0 CAT=0 ST=20 CDT=20 IDT=0 QL=0JOB 2AT=10 CAT=10 ST=15 CDT=35 IDT=0 QL=1JOB 3AT=15 CAT=25 ST=10 CDT=45 IDT=0 QL=1JOB 4AT=35 CAT=60 ST=5 CDT=65 IDT=15 QL=0JOB 5AT=30 CAT=90 ST=15 CDT=105 IDT=25 QL=0JOB 6AT=10 CAT=100 ST=15 CDT=120 IDT=0 QL=1JOB 7AT=5 CAT=105 ST=10 CDT=130 IDT=0 QL=1JOB 8AT=5 CAT=110 ST=10 CDT=140 IDT=0 QL=2