5 editing answer, improved grammer edited Apr 26 '15 at 8:46 Mahdi 1,31477 silver badges2121 bronze badges 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: Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions. Print[k] should be used before changing the "the number of iterations" (k=k+1) . 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)$$, 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)$$, Your code has three major flaws: Since a, b , c , d are numbers you cannot pass the step k to them. This syntax is for functions. Print[k] should be used before changing the "the number of iterations" (k=k+1) . 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)$$, 4 edited body edited Apr 26 '15 at 8:35 Mahdi 1,31477 silver badges2121 bronze badges 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)$$, 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)$$, 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)$$, 3 added 151 characters in body edited Apr 26 '15 at 8:28 Mahdi 1,31477 silver badges2121 bronze badges 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)$$, 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)$$, 2 improved code edited Apr 23 '15 at 19:14 Mahdi 1,31477 silver badges2121 bronze badges 1 answered Apr 23 '15 at 8:50 Mahdi 1,31477 silver badges2121 bronze badges