Aim - Write A Program To Simulate Pure-Pursuit Problem of Continuous System Simulation. Code

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Aim – Write a program to simulate Pure-Pursuit Problem of continuous system simulation.

Code:

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<math.h>
void main()
{ int i,v=20,time=12,xb[15]=
{80,90,99,108,116,125,133,141,151,160,169,179,180};
int yb[15]= {0,-2,-5,-9,-15,-18,-23,-29,-28,-25,-21,-20,-17};
float yf[15],xf[15],dist,sqy,sqx,sin,cos;
xf[0]=0;
yf[0]=50;
int gap,duration;
printf("\t\t\tpure pursult problem\n");
for(i=1; i<=time; i++)
{ sqy=(yb[i]-yf[i])*(yb[i]-yf[i]);
sqx=(xb[i]-xf[i])*(xb[i]-xf[i]);
dist=sqrt(sqx+sqy);
printf("\ndistance between fighter and bomber: %.5f",dist);
if(dist<=10)
{
printf("\n\ndestroyed at (%d,%d) of bomber & (%.5f,%.5f)of fighter", xb[i],
yb[i], xf[i], yf[i]);
break;
}

sin=(yb[i]-yf[i])/dist;
cos=(xb[i]-xf[i])/dist;
xf[i+1]=xf[i]+v*(cos);
yf[i+1]=yf[i]+v*sin;
}
if(i>time)
{
printf("\n\nbomber escaped!");
}
getch();}
Aim: Write a program to determine the approximate value of √2 using 1000 random
numbers.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float i, a, d;
float b=sqrt(2);
float c=roundf(b);
for(i=0.414;i<1.4142;i=i+0.001)
{
a=i;
if(roundf(a)==c)
{
d=a;
}
}
printf("Value of square root of 2 :%f",d);
return 0;
}
Aim: Write a program to select a policy among different given policies with minimum total
cost of an inventory system.

#include<stdio.h>
#include<cstdlib>
int m=0;
int main()
{ while(m!=5)
{ int p,q,units=0,eqstock,start=1,due=0,demand,cost=0,stock=115;
{ printf("\n Enter reorder point:");
scanf("\n %d",&p);
printf("\n Enter reorder quantity:");
scanf("\n %d",&q);
while(start<=180)
{ if(due==start)
{ stock+=q;
units=0;}
demand=rand()%100;
if(demand<=stock)
{ stock=stock-demand;
cost=cost+stock*0.75; }
else {
cost+=(demand-stock)*18;
stock=0; }
eqstock=stock+units;
if(eqstock<=p)
{ units=q;
cost+=75;
due=start+3; }
start++; }
printf("\n Reorder point:%d",p);
printf("\n Reorder Quantity:%d",q);
printf("\n Total cost is:%d",cost);
}
m++; }
return 0; }
Aim: Write a program to simulate water reservoir problem.

#include<stdio.h>
#include<conio.h>
int main()
{ float tc=10000,dem,river,rain,prevol=0,sage=0,vol,tloss,diff,spill,vnet;
int ch;
do
{ printf("\n enter the river and rain water:\n");
scanf("%f%f",&river,&rain);
printf("\nenter the total loss:\n");
scanf("%f",&tloss);
printf("\nprevious volume is %.2f ",prevol);
vol=river+rain;
printf("\nnet water in the tank is %.2f ",vol);
if (tloss>=vol)
{ printf("YOU CAN'T SUPPLY WATER");
break; }
else
{ vnet=vol-tloss;
vnet+=prevol;
printf("\n water remain in the tank after loss is:\t%.2f ",vnet);
printf("\nenter the demand of water:\n");
scanf("%f",&dem);
if (dem>=vnet)
{ printf("\n\tYOU CAN'T SUPPLY WATER\n");
sage=dem-vnet;
printf("\nshortage of water is %.2f ",sage);
vnet=prevol;
printf(" volume remain in tank is %.2f",prevol);
}
else
{ printf("\n\tyou can supply the water\t");
prevol=vnet-dem;
printf("\n\t volume remain in tank is %.2f",prevol);
}
} ch--;
} while(ch!=2);}
Aim: Write a program to simulate single server queuing system.
#include <stdio.h> #include <stdlib.h> #include <math.h>
#include <limits.h>
#define NEW(type) (type *) malloc(sizeof(type))
#define ARRIVAL 1
#define DEPARTURE 2
int gmt,q,narr, q_sum,iat,service_time,total_events;
unsigned int seed; typedef struct schedule_info
{ int time, event_type;
struct schedule_info *next; }
EVENTLIST;
struct schedule_info *head, *tail;
int act(void);
int negexp(int);
void arrival(void);
void departure(void);
void schedule(int, int);
void sim_init(void);
main()
{ sim_init();
while (narr < total_events)
{ switch (act())
{ case ARRIVAL:
arrival();
break;
case DEPARTURE:
departure();
break;
default:
printf("error in act procedure\n");
exit(1);
break;
}}
printf("The mean queue length seen by arriving customers is: %8.4f\n", ((float) q_sum) / narr);
return(0); }
int negexp(int mean) /* returns a negexp rv with mean `mean' */
{ return ( (int)(- log((float) rand() / RAND_MAX) * mean + 0.5) ); }
void arrival() /* a customer arrives */
{ narr += 1; /* keep tally of number of arrivals */
q_sum += q;
schedule(negexp(iat), ARRIVAL); /* schedule the next arrival */
q += 1;
if (q == 1)
schedule(negexp(service_time), DEPARTURE); }
void departure() /* a customer departs */
{ q -= 1;
if (q > 0)
schedule(negexp(service_time), DEPARTURE); }
void schedule(int time_interval, int event)
{ int event_time;
struct schedule_info *x,*t;
event_time = gmt + time_interval;
t = NEW(EVENTLIST);
for(x=head ; x->next->time<event_time && x->next!=tail ; x=x->next);
t->time = event_time;
t->event_type = event;
t->next = x->next;
x->next = t;
}
int act() /* find the next event and go to it */
{
int type;
struct schedule_info *x;
gmt = head->next->time; /* step time forward to the next event */
type = head->next->event_type; /* Record type of this next event */
x = head->next; /* Delete event from linked list */
head->next = head->next->next;
free(x);
return type; /* return value is type of the next event */
}
void sim_init()
{ printf("\nenter the mean interarrival time and the mean holding time\n");
scanf("%d%d", &iat, &service_time);
printf("enter the total number of customers to be simulated\n");
scanf("%d", &total_events);
printf("enter the seed\n");
scanf("%ld", &seed);
srand(seed);
head = NEW(EVENTLIST);
tail = NEW(EVENTLIST);
head->next = tail;
tail->next = tail;
q = 0;
narr = 0;
q_sum = 0;
schedule(negexp(iat), ARRIVAL); }
Aim: Write a program to simulate two server queuing system.
Code:
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
main()
{ int i,count=0,konta=0,kontb=0,qa=0,qb=0;
float r, sea=0,seb=0,iat,nat=0,wta=0,wtb=0,clock=0,ita=0,itb=0;
float delt=0.1,run;
float mue,meana,meanb,siga,sigb,time,sum,sta,stb;
int qamax=0,qbmax=0;
mue=1/5;meana=4.0;siga=2.0;meanb=10;sigb=5;
printf("\n\n mean of expo. processing time of stationI=%6.2f",mue);
printf("\n processing time of station A: mean=%6.2f SD=%6.2f",meana,siga);
printf("\n processing time of station B : mean%6.2f SD=%6.2f",meanb,sigb);
printf("\n enter the value of run ");
scanf("%f",&run);
while(clock<=run)
{ if(clock<=run)
{ count=count+1;
r=rand()/32768;
iat=(-1/mue)*log(1-r);
nat=clock+iat;
r=rand()/32768;
if(r<=0.9)
qa=qa+1;
else qb=qb+1;
if(qa>qamax)
qamax=qa;
if(qb>qbmax)
qbmax=qb;
}
if(clock>=sea)
{ wta=wta+qa+delt;
if(qa>=1)
{ qa=qa-1;
sum=0;
for(i=1;i<=12;i++)
{ r=rand()/32768;
sum=sum+r;
} sta=meana+siga*(sum-6);
sea=clock+sta;
konta=konta+1;
}
else ita=ita+delt;
}
if (clock<sea)
wta=wta+qa*delt;
if(clock>=seb)
{ wtb=wtb+qb*delt;
if(qb>=1)
{ qb=qb-1;
sum=0;
for(i=1;i<=12;i++)
{ r=rand()/32768;
sum=sum+r;
} stb=meanb+sigb*(sum-6);
seb=clock+stb;
kontb=kontb+1;
}
else itb=itb+delt;
}
if (clock<seb)
wtb=wtb+qb*delt;
clock=clock+delt;
}
printf("\n clock =%6.2f count=%d kont A=%d kont
B=%d",clock,count,konta,kontb);
printf("\n Idle time of server A= %6.2f%",100*ita/clock);
printf("\n Idle time of server B= %6.2f%",100*itb/clock);
printf("\n Average waiting time of components A=%6.2f per components A=%6.2f
per component",wta/konta);
printf("\n Average waiting time of components B=%6.2f per components B=%6.2f
per component",wtb/kontb);
printf("\n maximum number in buffer A= %d",qamax);
printf("\n maximum number in buffer B= %d",qbmax);
}
Aim: Write a program to generate and print a sequence of 30 pseudo random numbers
between 150 to 250 by using any simulation technique.

Code :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int n,max,num,c;
printf("\n enter the numberof random number you want \n");
scanf("%d",&n);
printf("\n %d psuedo random number from 150 to 250 are:- \n",n,max);
rand();
for(c=1;c<=n;c++)
{
num=rand()% (250-150)+150;
printf("%d \n",num);
}
getch();
return 0;
}
Aim: Write a program to simulate an inventory system with the objective to determine the
re-order combination (P,Q) which yields the highest service level for a given value of
average stock.
Code:
#include<stdio.h> #include<conio.h> #include<math.h>
#include<stdlib.h>
int main()
{ int day=1, stk=10, cstk=0, ordd=0, ddue=0, dmd=0, cdmd=0, shortage=0, cshort=0,
rp=10, q=15, lt=3;
float x,y,avstk,sl;
int tdays;
printf("\n length of simulation run(tdays)=");
scanf("%d",&tdays);
printf("\n inital stock =%d",stk);
printf("\n lead time= %d",lt);
printf("\n reorder point =%d",rp);
printf("\n reorder quantity =%d",q);
for(day=1;day<=tdays;++day)
{ stk=stk-dmd;
if(stk<=0)
stk=0;
if(ddue==0)
{ stk=stk+ordd; ordd=0; }
cstk=cstk+stk;
if( ordd==0)
{ if(stk<=rp)
{ ordd=q; ddue=lt; } }
x=rand()/32768.0;
if(x<=0.20)
dmd=3;
else if ((x>0.2)&&(x<=0.50))
dmd=4;
else if((x>0.5)&&(x<=0.85))
dmd=5;
else dmd=6;
cdmd=+dmd;
if(dmd>stk)
shortage=dmd-stk;
else shortage=0;
cshort=+shortage;
if(ddue>0)
ddue=-1; }
sl=(cdmd-cshort)*100.0/cdmd;
float days=tdays;
avstk=cstk/days;
printf("\n average stock = %6.2f units service level= %6.2f%",avstk,sl); }
Aim: Write a program to generate a sample of pseudo random values from a given non-
uniform distribution.

Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main()
{ int k,j,x,nn;
float lemda,cumprob,fact,prob,y,mean;
printf("\n value of mean (lemda)=");
scanf("%f",&lemda);
printf("\n number of variates to be generated=");
scanf("%d",&nn);
mean = lemda;
for(j=0;j<nn;j++)
{ fact=1; x=0; cumprob=0;
y=rand()/32768.0;
while(y>cumprob)
{ prob=pow(2.7182,-mean)*pow(mean,x)/fact;
cumprob=cumprob+prob;
x=x+1;
fact=fact*x;
}
printf("%4d",x); }
}
Aim: Write a program to solve the differential equation by using 4th order
Runge-Kutta method dy/dx = -2x-y, with intial condition y = -2 and x = 0.

#include<stdio.h>
#include<conio.h>
#include<math.h>
float dydx(float x , float y)
{
return ((x-y)/2);
}
float rungekutta(float x1,float y1, float x, float h)
{

int n=(int)((x-x1)/h);
float k1,k2,k3,k4;
float y=y1;
int i;
for(i=0;i<n;i++)
{
k1=h*dydx(x1,y1);
k2=h*dydx(x1+0.5*h,y+0.5*k1);
k3=h*dydx(x1+0.5*h,y+0.5*k2);
k4=h*dydx(x1+h,y+k3);
}
y=y+(1.0/6.0)*(k1+2*k2+2*k3+k4);
x=x1+h;
return y;
}
int main()
{
float x1=0,y=1,x=2,h=0.2;
printf("The Value of y at x is : %f",rungekutta(x1,y,x,h));
return 0;

}
AIM: Program to simulate Chemical Reactor.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float a=100,b=50,c=0,k1=0.008,k2=0.001,t=0.1,da,db,dc,i;
cout<<"TIME"<<"\t\t"<<"A(i)"<<"\t\t"<<"B(i)"<<"\t\t"<<"C(i)"<<endl;
cout<<"\t\t"<<a<<"\t\t"<<b<<"\t\t"<<c<<"\t\t"<<endl;
for(i=1;i<=25;i++)
{
da=k2*c-k1*a*b;
db=k2*c-k1*a*b;
dc=2*k1*a*b-2*k2*c;
a=a+da*t;
b=b+db*t;
c=c+dc*t;
cout<<i<<"\t\t"<<a<<"\t\t"<<b<<"\t\t"<<c<<endl;
}
getch();
return 0;
}
Program 4.1: Congruential Method
/*program for generating uniform random numbers by Congruential method*/
#include <iostream.h>
#include<stdlib.h>
#include <math.h>
main( )
{
int a,b,k,j,i,m,nn,seed,r[50];
cout<<”\nEnter the integer value of a,b,m, leave space between a,b,m”;
cin>>a>>b>>m;
cout<<”\nEnter the integer value of seed”;
cin>>seed;
cout<<”\nEnter the number of Random numbers to be generated”;
cin>>nn;
r[0]=seed;
for(i=0; i<=nn; ++i)
{
r[i]=(a*r[i-1]+b)%m;
cout<<r[i];
}}
PROGRAM TO SIMULATE PERT
#include<stdio.h>
#include<conio.h>
struct rec
{ char From;
char To;
long NorTime;
long EarTime;
long LstTime;
long Float;
}task[30];
void input();
int count = 0;
char first, last, ans;
long max, min;
int main()
{ int i, j, k;
clrscr();
input();
clrscr();
for (i=0; i<count; i++)
if (task[i].From == first)
task[i].EarTime = task[i].NorTime;
for (i=0; i<count; i++)
{ max = 0;
for (k=0; k<=i; k++)
if (max<task[k].EarTime && task[i].To == task[k].To)
max = task[k].EarTime;
for (j=0; j<count; j++)
if (task[i].To == task[j].From)
task[j].EarTime = task[j].NorTime + max; }
max = 0;
for (i=0; i<count; i++)
if (task[i].To == last)
if (max < task[i].EarTime)
max = task[i].EarTime;
printf("\n\n\tThe length of the Critical Path = ");
printf("%ld", max);
for (i=0; i<count; i++)
if (task[i].To == last)
{ task[i].LstTime = max;
task[i].Float = task[i].LstTime - task[i].EarTime; }
for (i = count-1; i>=0; i--)
{ min = 99999;
for (k = count-1; k>i; k--)
if (task[i].To == task[k].From && min > task[k].LstTime - task[k].NorTime)
{ min = task[k].LstTime - task[k].NorTime;
task[i].LstTime = min;
task[i].Float = min - task[i].EarTime;
}}
printf("\n\n\tThe Critical Path is : \n\n");
printf("\t\t%c", first);
for (i=0; i<count; i++)
if (task[i].Float == 0)
printf("-->%c", task[i].To);
getch();
return 0;
}

void input()
{
long o, p, m;
printf("\nEnter the following data for the given network : \n\n");
do
{
fflush(stdin);
printf("\n\tFrom : ");
scanf("%c", &task[count].From);
fflush(stdin);
printf("\n\tTo : ");
fflush(stdin);
scanf("%c", &task[count].To);
printf("\tEnter Optimistic, Most Liely and Pessimistic Time : ");
scanf("%ld %ld %ld", &o, &m, &p);
task[count].NorTime = (o + 4 * m + p)/6;

printf("More ? Press<y> for Yes <n> for No.");


fflush(stdin);
scanf("%c", &ans);
count++;
}while(ans != 'n');
printf("\nEnter the Initial Node : ");
fflush(stdin);
scanf("%c", &first);
printf("\nEnter the Final Node : ");
fflush(stdin);
scanf("%c", &last);
}
FORWARD AND BACKWARD PASS CPM AND TRAVERSING NETWORK
#include<stdio.h>
#include<conio.h>
struct rec
{ char From;
char To;
long NorTime;
long EarTime;
long LstTime;
long Float;
}task[30];
void input();
int count = 0;
char first, last, ans;
long max, min;
int main()
{ int i, j, k;
clrscr();
input();
clrscr();
for (i=0; i<count; i++)
if (task[i].From == first)
task[i].EarTime = task[i].NorTime;
for (i=0; i<count; i++)
{ max = 0;
for (k=0; k<=i; k++)
if (max<task[k].EarTime && task[i].To == task[k].To)
max = task[k].EarTime;
for (j=0; j<count; j++)
if (task[i].To == task[j].From)
task[j].EarTime = task[j].NorTime + max; }
max = 0;
for (i=0; i<count; i++)
if (task[i].To == last)
if (max < task[i].EarTime)
max = task[i].EarTime;
printf("\n\n\tThe length of the Critical Path = ");
printf("%ld", max);
for (i=0; i<count; i++)
if (task[i].To == last)
{ task[i].LstTime = max;
task[i].Float = task[i].LstTime - task[i].EarTime;
}
for (i = count-1; i>=0; i--)
{
min = 99999;
for (k = count-1; k>i; k--)
if (task[i].To == task[k].From && min > task[k].LstTime -
task[k].NorTime)
{
min = task[k].LstTime - task[k].NorTime;
task[i].LstTime = min;
task[i].Float = min - task[i].EarTime;
}
}
printf("\n\n\tThe Critical Path is : \n\n");
printf("\t\t%c", first);
for (i=0; i<count; i++)
if (task[i].Float == 0)
printf("-->%c", task[i].To);
getch();
return 0;
}

void input()
{
//long o, p, m;
printf("\nEnter the following data for the given network : \n\n");
do
{
fflush(stdin);
printf("\n\tFrom : ");
scanf("%c", &task[count].From);
fflush(stdin);
printf("\n\tTo : ");
fflush(stdin);
scanf("%c", &task[count].To);
printf("\tEnter the normal time : ");
scanf("%ld", &task[count].NorTime);
fflush(stdin);
printf("More ? Press<y> for Yes <n> for No.");
fflush(stdin);
scanf("%c", &ans);
count++;
}while(ans != 'n');
printf("\nEnter the Initial Node : ");
fflush(stdin);
scanf("%c", &first);
printf("\nEnter the Final Node : ");
fflush(stdin);
scanf("%c", &last);
}

You might also like