0
$\begingroup$

I am attempting to write a code which brackets the minimum of a unimodal function, using the Golden Section Method Below. I am have serious issues debugging my program though, any help will be appreciated.

(* Golden Ratio Search Method*)

GoldenSearch[a0_, b0_, ϵ_] :=
  Module[{a = N[a0], b = N[b0], bleft, bright, ρ, x1, x2},
   ρ = 
    N[Abs[(3 - Sqrt[5])/2] , 6];(*evaluates value of golden ration*)
   bleft = N[a, 6];(*left boundary point*) 
   bright = N[b, 6]; (*right boundary point*)

   x1 = N[bleft + ρ*(bright - bleft), 
     6];(*possible new left value*)
   x2 = N[bleft + (1 - ρ) (bright - bleft), 
     6];(*possible new right value*)


   While[ (b - a) > ϵ, If[ f[x2] > f[x1],
     Module[{},
      bright = x2;
      b = x2;
      x2 = x1;
      (*bleft = bleft*)
      x1 = bleft + ρ*(bright - bleft);],
     Module[{},
        bleft = x1;
        a = x1;
        x1 = x2;
        (*bright=bright*)
        x2 = bleft + (1 - ρ) (bright - bleft);
        ;], ;];
    Print["f[", PaddedForm[{a, x1, x2, b}, {7, 6}] "]", ;];];

   ];

An example function is:

f[x_] := x^2 + 4*Cos[x]; [1,2,0.2]
$\endgroup$
15
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Apr 21, 2015 at 18:48
  • $\begingroup$ You can format inline code and code blocks by selecting it and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. $\endgroup$
    – Michael E2
    Apr 21, 2015 at 18:48
  • 2
    $\begingroup$ I fully support what @MichaelE2 said, but I don't see a question being asked here. Are you asking the community to inspect your code, familiarize itself with the topic you are interested in and proceed to solve the problem ? It is best if you can narrow down the problem to a minimum and ask a specific question about it. $\endgroup$
    – Sektor
    Apr 21, 2015 at 19:00
  • 1
    $\begingroup$ What is the bad behavior? What behavior are you expecting? What is the result you intend to return (if any)? $\endgroup$ Apr 21, 2015 at 19:40
  • 1
    $\begingroup$ Please pay attention to how to format your posts. You should also edit the question itself to clarify the problem (instead of just in the comments). Providing an example function f might be helpful, too, whether it is simple or complicated. $\endgroup$
    – Michael E2
    Apr 21, 2015 at 20:11

1 Answer 1

1
$\begingroup$

The Print statement requires that the output from PaddedForm be converted to a String. Additionally, the second and third Module statements cause problems and seem unnecessary. With them deleted, the second half of the code can be rewritten as

 While[ (b - a) > ϵ, If[ f[x2] > f[x1],
       bright = x2; b = x2; x2 = x1; x1 = bleft + ρ*(bright - bleft),
        bleft = x1; a = x1; x1 = x2; x2 = bleft + (1 - ρ) (bright - bleft) ]];

 Print["f[", ToString[PaddedForm[{a, x1, x2, b}, {7, 6}]], "]"] ]

Then, with f[x_] := x^2 + 4*Cos[x] as suggested in the Question, GoldenSearch produces results.

GoldenSearch[1, 2, .2]
f[{ 1.854102,  1.909830,  1.944272,  2.000000}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.