Sunday, October 23, 2011

Continuous distribution(weibull,uniform,triangle) implementation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void triangular();
void weibull();
void lognormal();
void uniform();
void main()
{
 int c;
 clrscr();
 printf("choose distribution system:");
 printf("\n1:Triangular distribution");
 printf("\n2:Weibull distribution ");
 printf("\n3:Lognormal distribution");
 printf("\n4:Uniform distribution");
 printf("enter ur choice:");
 scanf("%d",&c);
 clrscr();
 switch(c)
 {
 case 1:
 triangular();
 break;
 case 2:
 weibull();
 break;
 case 3:
 normal();
 break;
 case 4:
 uniform();
 break;
 default:
 printf("invalid selection");
 exit(0);
 }
 getch();
}
void triangular()
{
 double x,a,b,c;
 printf("enter d value of x, a, b & c:  ");
scanf(" %lf %lf %lf %lf",&x, &a, &b, &c);

printf(" f(%.2f) = %f",x,f(x,a,b,c));

printf(" F(%.2f) = %f",x,F(x,a,b,c));
double f(double x, double a, double b, double c)
{


if(a <= x && x <= b)
{

return (2*(x-a))/((b-a)*(c-a));
}
else if( b < x && x <= c)
{
return 2*(c-x)/((c-b)*(c-a));
}
else
return 0;

}

double F(double x, double a, double b, double c)
{
if(x<=a)
return 0;

else if( a{

return ( (x-a)*(x-a)/((b-a)*(c-a)) );

}
else if(b < x && x <= c )
{

return (1 - (c-x)*(c-x)/((c-b)*(c-a)));

}
else if(x>c)
return 1;
}
}

void weibull()
{
int x;
double v,a,b;
printf("enter d value of x, location, shape & scale:  ");
scanf(" %d %lf %lf %lf",&x, &v, &b, &a);

printf(" f(%d) = %f",x,f(x,v,a,b));

printf(" F(%d) = %f",x,F(x,v,a,b));
double f(int x, double v, double a, double b)
{

if(x< v)
return 0;

else
{
return b/a*(pow((x-v)/a,b-1))*exp(-pow((x-v)/a,b));

}

}

double F(int x, double v, double a, double b)
{
if(xreturn 0;

else
{

return (1 - exp(-pow((x-v)/a,b)));

}

}
void lognormal()
{
int sigma,x,u;
printf("enter sigma,x,u");
scanf("%d%d%d",&sigma,&x,&u);
if(x>0)
printf("pdf=%.3lf",(1/(sqrt(2*PI)*sigma*x))*exp(-pow((log(x)-u),2)/(2*sigma*sigma)));
}
void uniform()
{
int a,x,b;
printf("enter the value for a,x,b:");
scanf("%d%d%d",&a,&x,&b);
printf(" f(%d) = %f",x,f(x,a,b));

printf(" F(%d) = %f",x,F(x,a,b));
double f(int x, double v, double a, double b)
{
if(a<=x && x<=b)
return (1/(b-a));
else
return 0;
}
double F(int x, double v, double a, double b)
{
if(xreturn 0;
else if(a<=x && xreturn((x-a)/(b-a));
else if(x>=b)
return 1;
}
}

No comments:

Post a Comment