Latest Post
Optimization: Interval Halving Method
- Get link
- X
- Other Apps
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;
goto jump;
}
else if (fx2 < fxm)
{
b = b;
a = xm;
xm = x2;
goto jump;
}
else if (fx1 > fxm && fx2 > fxm)
{
a = x1;
b = x2;
xm = (a+b)/2;
goto jump;
}
else
{
goto end;
}
}
else
{
goto end;
}
end:
return 0;
}
double myFun(double x) // function definition
{
double y;
y = ((x*x)+(54/x));
return y; // return statement
}
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;
goto jump;
}
else if (fx2 < fxm)
{
b = b;
a = xm;
xm = x2;
goto jump;
}
else if (fx1 > fxm && fx2 > fxm)
{
a = x1;
b = x2;
xm = (a+b)/2;
goto jump;
}
else
{
goto end;
}
}
else
{
goto end;
}
end:
return 0;
}
double myFun(double x) // function definition
{
double y;
y = ((x*x)+(54/x));
return y; // return statement
}
Output:
![]() |
Interval Halving Method Optimization Algorithm |
- Get link
- X
- Other Apps
Popular posts from this blog
VLSI: BCD to Excess 3 and Excess 3 to BCD Dataflow Modelling
module bcd_ex3_Dataflow( input a, input b, input c, input d, output w, output x, output y, output z ); assign w = (a | (b & c) | (b & d)); assign x = (((~b) & c) | ((~b) & d) | (b & (~c) & (~d))); assign y = ((c & d) | ((~c) & (~d))); assign z = ~d; endmodule Excess 3 to BCD: module ex3_to_bcd( input w, input x, input y, input z, output a, output b, output c, output d ); assign a = ((w & x) | (w & y & z)); assign b = (((~x) & (~y)) | ((~x) & (~z)) | (x & y & z)); assign c = (((~y) & z) | (y & (~z))); assign d = ~z; endmodule
VLSI: 1-4 DEMUX (Demultiplexer) Dataflow Modelling with Testbench
Verilog Code for 1-4 DEMUX Dataflow Modelling module demux_1_to_4( input d, input s0, input s1, output y0, output y1, output y2, output y3 ); assign s1n = ~ s1; assign s0n = ~ s0; assign y0 = d& s0n & s1n; assign y1 = d & s0 & s1n; assign y2 = d & s0n & s1; assign y3 = d & s0 & s1; endmodule //Testbench code for 1-4 DEMUX Dataflow Modelling initial begin // Initialize Inputs ...
VLSI: 4-1 MUX Dataflow Modelling with Testbench
Verilog Code for 4-1 MUX Dataflow Modelling module m41(out, i0, i1, i2, i3, s0, s1); output out; input i0, i1, i2, i3, s0, s1; assign y0 = (i0 & (~s0) & (~s1)); assign y1 = (i1 & (~s0) & s1); assign y2 = (i2 & s0 & (~s1)); assign y3 = (i3 & s0 & s1); assign out = (y0 | y1 | y2 | y3); endmodule //Testbench code for 4-1 MUX Dataflow Modelling initial begin // Initialize Inputs a = 1;b = 0;c = 0;d = 0;s0 = 0;s1 = 0; ...
1 to 4 DEMUX (Demultiplexer) Verilog CodeStructural/Gate Level Modelling with Testbench
Verilog Code for 1 to 4 DEMUX Structural/Gate Level Modelling 1-4 DEMUX module demux_1_to_4( input d, input s0, input s1, output y0, output y1, output y2, output y3 ); not(s1n,s1),(s0n,s0); and(y0,d,s0n,s1n),(y1,d,s0,s1n),(y2,d,s0n,s1),(y3,d,s0,s1); endmodule //Testbench code for 1 to 4 DEMUX Structural/Gate Level Modelling initial begin // Initialize Inputs d = 1; s0 = 0; s1 = 0; // Wait 100 ns for global reset to finish #100; // Add stimulus here #100;d = 1;s0 = 1;s1 = 0; #100;d = 1;s0 = ...
VLSI: 3-8 Decoder Structural/Gate Level Modelling with Testbench
Verilog Code for 3-8 Decoder Structural/Gate Level Modelling 3-8 Line Decoder module decoder3_to_8( input x, input y, input z, output d0, output d1, output d2, output d3, output d4, output d5, output d6, output d7 ); and (d0,xn,yn,zn),(d1,xn,yn,z),(d2,xn,y,zn),(d3,xn,y,z),(d4,x,yn,zn),(d5,x,yn,z),(d6,x,y,zn),(d7,x,y,z); not (xn,x),(yn,y),(zn,z); endmodule //Testbench code for 3-8 Decoder Structural/Gate Level Modelling initial begin // Initialize Inputs x = 0;y = 0;z = 0; // Wait 100 ns for global reset to finish #100; // Add stimulus here #100;x = 0;y = 0;z = 1; #100;x = 0;y = 1;z = 0; #100;x ...
Comments
Post a Comment