5 editing answer, improved grammer
source | link

This is not optimized or even correct, as I don't know the method so I just tried to fix the syntax. By comparing myYour code with yours, you will find out what has been changed butthree major changes areflaws:

  1. Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions.
  2. Print[k] should be used before changing the "the number of iterations" (k=k+1) .

  3. Your code does not allow enough iterations to bracket the minimum (n is small).

1) UsingYou may also use internal Fibonacci function.

2) Since a, b , c , d are numbers you cannot pass the stepinstead of manually enter values for kF to them. This syntax is for functions.

f[x_] := x^2 + 4*Cos[x];
FibonacciSearch[a0_, b0_, eps_] := Module[{a = N[a0], b = N[b0], n = 10}, 
k = 0;
While[(b - a) > eps,
     frac = (Fibonacci[n - k + 1])/(Fibonacci[n - k + 2]);
     c = a + (b - a) (1 - frac);
     d = a + (b - a)*frac;
       If[f[c] <= f[d], 
         b = d; k = k + 1;,
         a = c; Print[{k, PaddedForm[a, {7, 6}], PaddedForm[b, {7, 6}]}]; k = k + 1;]
 ]
];

Run it as:

FibonacciSearch[1, 2, 0.01]
(* {9, 1.893569, 1.901699} *)

Indeed f[x] has a minimum in the range $(1.893569, 1.901699)$,

enter image description here

This is not optimized or even correct, as I don't know the method so I just tried to fix the syntax. By comparing my code with yours, you will find out what has been changed but major changes are:

1) Using internal Fibonacci function.

2) Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions.

f[x_] := x^2 + 4*Cos[x];
FibonacciSearch[a0_, b0_, eps_] := Module[{a = N[a0], b = N[b0], n = 10}, 
k = 0;
While[(b - a) > eps,
     frac = (Fibonacci[n - k + 1])/(Fibonacci[n - k + 2]);
     c = a + (b - a) (1 - frac);
     d = a + (b - a)*frac;
       If[f[c] <= f[d], 
         b = d; k = k + 1;,
         a = c; Print[{k, PaddedForm[a, {7, 6}], PaddedForm[b, {7, 6}]}]; k = k + 1;]
 ]
];

Run it as:

FibonacciSearch[1, 2, 0.01]
(* {9, 1.893569, 1.901699} *)

Indeed f[x] has a minimum in the range $(1.893569, 1.901699)$,

enter image description here

Your code has three major flaws:

  1. Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions.
  2. Print[k] should be used before changing the "the number of iterations" (k=k+1) .

  3. Your code does not allow enough iterations to bracket the minimum (n is small).

You may also use internal Fibonacci function, instead of manually enter values for F.

f[x_] := x^2 + 4*Cos[x];
FibonacciSearch[a0_, b0_, eps_] := Module[{a = N[a0], b = N[b0], n = 10}, 
k = 0;
While[(b - a) > eps,
     frac = (Fibonacci[n - k + 1])/(Fibonacci[n - k + 2]);
     c = a + (b - a) (1 - frac);
     d = a + (b - a)*frac;
       If[f[c] <= f[d], 
         b = d; k = k + 1;,
         a = c; Print[{k, PaddedForm[a, {7, 6}], PaddedForm[b, {7, 6}]}]; k = k + 1;]
 ]
];

Run it as:

FibonacciSearch[1, 2, 0.01]
(* {9, 1.893569, 1.901699} *)

Indeed f[x] has a minimum in the range $(1.893569, 1.901699)$,

enter image description here

4 edited body
source | link

This is not optimized or even correct, as I don't know the method so I just tried to fix the syntax. By comparing my code with yours, you will find out what has been changed but major changes are:

1) Using internal Fibonacci function.

2) Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions.

f[x_] := x^2 + 4*Cos[x];
FibonacciSearch[a0_, b0_, eps_] := Module[{a = N[a0], b = N[b0], n = 10}, 
k = 0;
While[(b - a) > eps,
     frac = (Fibonacci[n - k + 1])/(Fibonacci[n - k + 2]);
     c = a + (b - a) (1 - frac);
     d = a + (b - a)*frac;
       If[f[c] <= f[d], 
         b = d; k = k + 1;,
         a = c; Print[{k, PaddedForm[a, {7, 6}], PaddedForm[b, {7, 6}]}]; k = k + 1;]
 ]
];

Run it as:

FibonacciSearch[1, 2, 0.05]01]
(* {69, 1.875000893569, 1.909722901699} *)

Indeed f[x] has a minimum in the range $(1.875000, 1.909722)$$(1.893569, 1.901699)$,

enter image description here

This is not optimized or even correct, as I don't know the method so I just tried to fix the syntax. By comparing my code with yours, you will find out what has been changed but major changes are:

1) Using internal Fibonacci function.

2) Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions.

f[x_] := x^2 + 4*Cos[x];
FibonacciSearch[a0_, b0_, eps_] := Module[{a = N[a0], b = N[b0], n = 10}, 
k = 0;
While[(b - a) > eps,
     frac = (Fibonacci[n - k + 1])/(Fibonacci[n - k + 2]);
     c = a + (b - a) (1 - frac);
     d = a + (b - a)*frac;
       If[f[c] <= f[d], 
         b = d; k = k + 1;,
         a = c; Print[{k, PaddedForm[a, {7, 6}], PaddedForm[b, {7, 6}]}]; k = k + 1;]
 ]
];

Run it as:

FibonacciSearch[1, 2, 0.05]
(* {6, 1.875000, 1.909722} *)

Indeed f[x] has a minimum in the range $(1.875000, 1.909722)$,

enter image description here

This is not optimized or even correct, as I don't know the method so I just tried to fix the syntax. By comparing my code with yours, you will find out what has been changed but major changes are:

1) Using internal Fibonacci function.

2) Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions.

f[x_] := x^2 + 4*Cos[x];
FibonacciSearch[a0_, b0_, eps_] := Module[{a = N[a0], b = N[b0], n = 10}, 
k = 0;
While[(b - a) > eps,
     frac = (Fibonacci[n - k + 1])/(Fibonacci[n - k + 2]);
     c = a + (b - a) (1 - frac);
     d = a + (b - a)*frac;
       If[f[c] <= f[d], 
         b = d; k = k + 1;,
         a = c; Print[{k, PaddedForm[a, {7, 6}], PaddedForm[b, {7, 6}]}]; k = k + 1;]
 ]
];

Run it as:

FibonacciSearch[1, 2, 0.01]
(* {9, 1.893569, 1.901699} *)

Indeed f[x] has a minimum in the range $(1.893569, 1.901699)$,

enter image description here

3 added 151 characters in body
source | link

This is not optimized or even correct, as I don't know the method so I just tried to fix the syntax. By comparing my code with yours, you will find out what has been changed but major changes are:

1) Using internal Fibonacci function.

2) Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions.

f[x_] := x^2 + 4*Cos[x];
FibonacciSearch[a0_, b0_, eps_] := Module[{a = N[a0], b = N[b0], n = 410}, 
k = 0;
While[(b - a) > eps,
     frac = (Fibonacci[n - k + 1])/(Fibonacci[n - k + 2]);
     c = a + (b - a) (1 - frac);
     d = a + (b - a)*frac;
       If[f[c] <= f[d], 
         b = d; k = k + 1;,
         a = c; Print[{k, PaddedForm[a, {7, 6}], PaddedForm[b, {7, 6}]}]; k = k + 1;]
 ]
];

Run it as:

FibonacciSearch[1, 2, 0.05]
(* {46, 1.750000875000, 1.875000909722} *)

Indeed f[x] has a minimum in the range $(1.875000, 1.909722)$,

enter image description here

This is not optimized or even correct, as I don't know the method so I just tried to fix the syntax. By comparing my code with yours, you will find out what has been changed but major changes are:

1) Using internal Fibonacci function.

2) Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions.

f[x_] := x^2 + 4*Cos[x];
FibonacciSearch[a0_, b0_, eps_] := Module[{a = N[a0], b = N[b0], n = 4}, 
k = 0;
While[(b - a) > eps,
     frac = (Fibonacci[n - k + 1])/(Fibonacci[n - k + 2]);
     c = a + (b - a) (1 - frac);
     d = a + (b - a)*frac;
       If[f[c] <= f[d], 
         b = d; k = k + 1;,
         a = c; Print[{k, PaddedForm[a, {7, 6}], PaddedForm[b, {7, 6}]}]; k = k + 1;]
 ]
];

Run it as:

FibonacciSearch[1, 2, 0.05]
(* {4, 1.750000, 1.875000} *)

This is not optimized or even correct, as I don't know the method so I just tried to fix the syntax. By comparing my code with yours, you will find out what has been changed but major changes are:

1) Using internal Fibonacci function.

2) Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions.

f[x_] := x^2 + 4*Cos[x];
FibonacciSearch[a0_, b0_, eps_] := Module[{a = N[a0], b = N[b0], n = 10}, 
k = 0;
While[(b - a) > eps,
     frac = (Fibonacci[n - k + 1])/(Fibonacci[n - k + 2]);
     c = a + (b - a) (1 - frac);
     d = a + (b - a)*frac;
       If[f[c] <= f[d], 
         b = d; k = k + 1;,
         a = c; Print[{k, PaddedForm[a, {7, 6}], PaddedForm[b, {7, 6}]}]; k = k + 1;]
 ]
];

Run it as:

FibonacciSearch[1, 2, 0.05]
(* {6, 1.875000, 1.909722} *)

Indeed f[x] has a minimum in the range $(1.875000, 1.909722)$,

enter image description here

2 improved code
source | link
1
source | link