Posts

Showing posts with the label Optimization

Optimization: Interval Halving Method

Image
1. Interval Halving Method Algorithm in C #include<stdio.h> double myFun(double x); int main() {  double a, b, y, x, xm, x1, x2, fx1, fx2, fxm, L, t=0.01;  printf("Enter a:");  scanf("%lf",&a);   //Upper Bound  printf("Enter b:");  scanf("%lf",&b);    //Lower Bound  xm = (a + b)/2;  jump:  L = b-a;  x1 = a + (L/4);  x2 = b - (L/4);  printf("\n\na = %.2lf",a);  printf("\nb = %.2lf",b);  printf("\nx1 = %.2lf",x1);  printf("\nx2 = %.2lf",x2);  printf("\nxm = %.2lf",xm);  printf("\nL = %.2lf",L);  fx1 = myFun(x1);  printf("\nf(x1) = %.2lf",fx1);  fx2 = myFun(x2);  printf("\nf(x2) = %.2lf",fx2);  fxm = myFun(xm);  printf("\nf(xm) = %.2lf",fxm);  if(L > t)  {   if (fx1 < fxm)   {    a = a;    b = xm;    xm = x1;    got...

Optimization: Newton Raphson Method

Image
Newton Raphson Method Algorithm in C #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> float myFun(float x); int main() {  float e = 0.01,a,aw = 0,bw = 1,Lw,b,x1,x2,fxa,fxb,fxz,z,j=0;  int i=1,k;  float x[100],dx[100],fdx[100],fddx[100];  x[1] = 1;  dx[1] = (x[i]*1)/100;  begin:  if(j<100)  {   dx[i] = (x[i]*1)/100;   fdx[i] = ((myFun(x[i] + dx[i])) - (myFun(x[i] - dx[i])))/(2*dx[i]);   fddx[i] = ((myFun(x[i] + dx[i])) - (2*myFun(x[i])) + (myFun(x[i] - dx[i])))/(dx[i]*dx[i]);   printf("\nf'(%d) = %.4f",i,fdx[i]);   printf("\nf''(%d) = %.4f",i,fddx[i]);   k = i+1;   x[k] = (x[i]  - (fdx[i] / fddx[i]));   printf("\n\nf(%d) = %.4f",k,x[k]);   dx[k] = (x[k]*1)/100;   fdx[k] = ((myFun(x[k] + dx[k])) - (myFun(x[k] - dx[k])))/(2*dx[k]);   if (fdx[k] < 0)  ...

Optimization: Exhaustive Search Method

Image
Exhaustive Search Method Opimization Algorithm in C #include<stdio.h> #include<conio.h> double myFun(double x); int main() {  double a, b, N, y, x, x1, x2, x3, fx1, fx2, fx3, D,i=0;  printf("Enter a:");  scanf("%lf",&a);  printf("Enter b:");  scanf("%lf",&b);  printf("Enter no. of N:");  scanf("%lf",&N);  D = (b-a)/N;  printf("D = %.2lf",D);  x1=a;  x2=x1+D;  x3=x2+D;  jump:  if (i < 300)  {   printf("\n\nx1 = %.2lf",x1);   printf("\nx2 = %.2lf",x2);   printf("\nx3 = %.2lf",x3);   fx1 = myFun(x1);   printf("\nf(x1) = %.2lf",fx1);   fx2 = myFun(x2);   printf("\nf(x2) = %.2lf",fx2);   fx3 = myFun(x3);   printf("\nf(x3) = %.2lf",fx3);   if (fx1 >= fx2 && fx2<= fx3)   {    printf("\nCondition met");    g...

Optimization: Bound Phase Method

Image
Bound Phase Method Algorithm in C #include<stdio.h> #include<math.h> double myFun(double x); int main() {  double xw,a,b,fa,fb,fxw,D,y ;  int k,o,z;  float x[100],fx[100];  k = 0;  start:  printf("Enter x[0]:");  scanf("%lf",&xw);  printf("Enter D:");  scanf("%lf",&D);  a = xw + D;  b = xw - D;  fxw = myFun(xw);  printf("\nf(x0) = %.2lf",fxw);  fa = myFun(a);  printf("\nf(x0 + D) = %.2lf",fa);  fb= myFun(b);  printf("\nf(x0 - D) = %.2lf",fb);  printf("\nx[%d] = %.2lf",k,xw);  x[0] = xw;  fx[k] = fxw;  o = k+1;  if (fb <= fxw && fxw <= fa)  {   D = -D;   goto jump1;  }  else if (fb >= fxw && fxw >= fa)  {   goto jump1;  }  else  {   printf("\n\n\tChange Initial Guess...\n\n");   goto start;  } ...

Popular posts from this blog

Verilog: 8 to 1 Multiplexer (8-1 MUX) Dataflow Modelling with Testbench Code

VLSI: 1-4 DEMUX (Demultiplexer) Dataflow Modelling with Testbench

VLSI: 4-1 MUX Dataflow Modelling with Testbench

VLSI: Half Subtractor and Full Subtractor Gate Level Modelling

Full Subtractor Verilog Code in Structural/Gate Level Modelling with Testbench