Issues developing Fibonacci Search algorithm with R.











up vote
0
down vote

favorite












[edited] I compiled this code for an Operations Research class. I ended up submitting another format that relied on R packages; however, I feel that I am close to a functioning algorithm.



I derived this code from another R program I complied for the 'Golden Search' algorithm. That program conducted 'bracket' searches until it found the optimal min/max. Each time I run this algorithm, it fails to conduct a 'bracket' search. After multiple hours, I have failed to find the issue. Any help finding the error in my code would be appreciated.



My submitted (and confirmed) algorithm returned a minimum (x) value of 0.8095 with a f(x) value of -24.3445 utilizing a Fibonacci search method.



# Step One: Initialization

## Input user-defined function
f = function(x)
{
(x^4)-(14*(x^3))+(60*(x^2))-(70*(x))
}

## Input user-defined parameters
Lb <- 0 ### Lower Bound
Ub <- 2 ### Upper Bound
Tl <- .3 ### Tolerance

n <- (Ub-Lb)/Tl
n<- as.integer(n)

fibonacci.search = function(f, lower.bound, upper.bound, tolerance)
{
iteration = 0

Fn = fibonacci(n, sequence = FALSE)

### Use the fibonacci ratios to set the initial test points

x1 = lower.bound + ((fib(n-2)/fib(n)) * (upper.bound - lower.bound))
x2 = lower.bound + ((fib(n-1)/fib(n)) * (upper.bound))

### Evaluate the function at the test points
f1 = f(x1)
f2 = f(x2)

while ((lower.bound - upper.bound) > tolerance)
{
iteration = iteration + 1
n = n - 1
cat('', 'n')
cat('Iteration #', iteration, 'n')
cat('f1 =', f1, 'n')
cat('f2 =', f2, 'n')

if (f1 < f2)
{
cat('f(x1) < f(x2)', 'n')
# If f(x1) < f(x2), then the new interval is [x1,b]:
# a becomes the previous x1, b does not change

lower.bound = x1
upper.bound = upper.bound

# x1 becomes the previous x2, n = n-1

x1 = x2
f1 = f2

cat('New Upper Bound =', upper.bound, 'n')
cat('New Lower Bound =', lower.bound, 'n')
cat('New Upper Test Point = ', x1, 'n')

### Find the new x2 using the formula in Step2.
x2 = lower.bound + (fib(n-1)/fib(n)) * (upper.bound)
cat('New Lower Test Point = ', x1, 'n')
f2 = f(x2)
}
else
{
cat('f(x1) > f(x2)', 'n')
# If f(x1) > f(x2), then the new interval is [a,x2]:
# a does not change, b becomes the previous x2

upper.bound = x2
lower.bound = lower.bound

# x2 becomes the previous x1, n = n-1

x2 = x1
f2 = f1

cat('New Upper Bound =', upper.bound, 'n')
cat('New Lower Bound =', lower.bound, 'n')
cat('New Lower Test Point = ', x1, 'n')

# Find the new x1 using the formula in Step2.
x1 = lower.bound + (fib(n-2)/fib(n)) * (upper.bound - lower.bound)
cat('New Upper Test Point = ', x2, 'n')
f1 = f(x1)
}
}

### Use the mid-point of the final interval as the estimate of the optimzer
cat('', 'n')
cat('Final Lower Bound =', lower.bound, 'n')
cat('Final Upper Bound =', upper.bound, 'n')
estimated.minimizer = (lower.bound + upper.bound)/2
cat('Estimated Minimum Value (x) =', estimated.minimizer, 'n')
cat('Estimated F(x)', f(estimated.minimizer),'n')

# Step Three: Results

## Plot the curve of the user-defined function
curve(f, from = Lb, to = Ub, main = expression("(x^4)-(14*(x^3))+(60*(x^2))-(70*(x))"))

## Plot the solution as a point
points(estimated.minimizer, f(estimated.minimizer), col="red", pch=19)

## Draw a square around the optima to highlight it
rect(estimated.minimizer-0.1, f(estimated.minimizer)-01, estimated.minimizer+0.1, f(estimated.minimizer)+01, lwd=2)
}

# Step Two: Loop Function
fibonacci.search(f, Lb, Ub, Tl)









share|cite|improve this question









New contributor




Josh K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    [edited] I compiled this code for an Operations Research class. I ended up submitting another format that relied on R packages; however, I feel that I am close to a functioning algorithm.



    I derived this code from another R program I complied for the 'Golden Search' algorithm. That program conducted 'bracket' searches until it found the optimal min/max. Each time I run this algorithm, it fails to conduct a 'bracket' search. After multiple hours, I have failed to find the issue. Any help finding the error in my code would be appreciated.



    My submitted (and confirmed) algorithm returned a minimum (x) value of 0.8095 with a f(x) value of -24.3445 utilizing a Fibonacci search method.



    # Step One: Initialization

    ## Input user-defined function
    f = function(x)
    {
    (x^4)-(14*(x^3))+(60*(x^2))-(70*(x))
    }

    ## Input user-defined parameters
    Lb <- 0 ### Lower Bound
    Ub <- 2 ### Upper Bound
    Tl <- .3 ### Tolerance

    n <- (Ub-Lb)/Tl
    n<- as.integer(n)

    fibonacci.search = function(f, lower.bound, upper.bound, tolerance)
    {
    iteration = 0

    Fn = fibonacci(n, sequence = FALSE)

    ### Use the fibonacci ratios to set the initial test points

    x1 = lower.bound + ((fib(n-2)/fib(n)) * (upper.bound - lower.bound))
    x2 = lower.bound + ((fib(n-1)/fib(n)) * (upper.bound))

    ### Evaluate the function at the test points
    f1 = f(x1)
    f2 = f(x2)

    while ((lower.bound - upper.bound) > tolerance)
    {
    iteration = iteration + 1
    n = n - 1
    cat('', 'n')
    cat('Iteration #', iteration, 'n')
    cat('f1 =', f1, 'n')
    cat('f2 =', f2, 'n')

    if (f1 < f2)
    {
    cat('f(x1) < f(x2)', 'n')
    # If f(x1) < f(x2), then the new interval is [x1,b]:
    # a becomes the previous x1, b does not change

    lower.bound = x1
    upper.bound = upper.bound

    # x1 becomes the previous x2, n = n-1

    x1 = x2
    f1 = f2

    cat('New Upper Bound =', upper.bound, 'n')
    cat('New Lower Bound =', lower.bound, 'n')
    cat('New Upper Test Point = ', x1, 'n')

    ### Find the new x2 using the formula in Step2.
    x2 = lower.bound + (fib(n-1)/fib(n)) * (upper.bound)
    cat('New Lower Test Point = ', x1, 'n')
    f2 = f(x2)
    }
    else
    {
    cat('f(x1) > f(x2)', 'n')
    # If f(x1) > f(x2), then the new interval is [a,x2]:
    # a does not change, b becomes the previous x2

    upper.bound = x2
    lower.bound = lower.bound

    # x2 becomes the previous x1, n = n-1

    x2 = x1
    f2 = f1

    cat('New Upper Bound =', upper.bound, 'n')
    cat('New Lower Bound =', lower.bound, 'n')
    cat('New Lower Test Point = ', x1, 'n')

    # Find the new x1 using the formula in Step2.
    x1 = lower.bound + (fib(n-2)/fib(n)) * (upper.bound - lower.bound)
    cat('New Upper Test Point = ', x2, 'n')
    f1 = f(x1)
    }
    }

    ### Use the mid-point of the final interval as the estimate of the optimzer
    cat('', 'n')
    cat('Final Lower Bound =', lower.bound, 'n')
    cat('Final Upper Bound =', upper.bound, 'n')
    estimated.minimizer = (lower.bound + upper.bound)/2
    cat('Estimated Minimum Value (x) =', estimated.minimizer, 'n')
    cat('Estimated F(x)', f(estimated.minimizer),'n')

    # Step Three: Results

    ## Plot the curve of the user-defined function
    curve(f, from = Lb, to = Ub, main = expression("(x^4)-(14*(x^3))+(60*(x^2))-(70*(x))"))

    ## Plot the solution as a point
    points(estimated.minimizer, f(estimated.minimizer), col="red", pch=19)

    ## Draw a square around the optima to highlight it
    rect(estimated.minimizer-0.1, f(estimated.minimizer)-01, estimated.minimizer+0.1, f(estimated.minimizer)+01, lwd=2)
    }

    # Step Two: Loop Function
    fibonacci.search(f, Lb, Ub, Tl)









    share|cite|improve this question









    New contributor




    Josh K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      [edited] I compiled this code for an Operations Research class. I ended up submitting another format that relied on R packages; however, I feel that I am close to a functioning algorithm.



      I derived this code from another R program I complied for the 'Golden Search' algorithm. That program conducted 'bracket' searches until it found the optimal min/max. Each time I run this algorithm, it fails to conduct a 'bracket' search. After multiple hours, I have failed to find the issue. Any help finding the error in my code would be appreciated.



      My submitted (and confirmed) algorithm returned a minimum (x) value of 0.8095 with a f(x) value of -24.3445 utilizing a Fibonacci search method.



      # Step One: Initialization

      ## Input user-defined function
      f = function(x)
      {
      (x^4)-(14*(x^3))+(60*(x^2))-(70*(x))
      }

      ## Input user-defined parameters
      Lb <- 0 ### Lower Bound
      Ub <- 2 ### Upper Bound
      Tl <- .3 ### Tolerance

      n <- (Ub-Lb)/Tl
      n<- as.integer(n)

      fibonacci.search = function(f, lower.bound, upper.bound, tolerance)
      {
      iteration = 0

      Fn = fibonacci(n, sequence = FALSE)

      ### Use the fibonacci ratios to set the initial test points

      x1 = lower.bound + ((fib(n-2)/fib(n)) * (upper.bound - lower.bound))
      x2 = lower.bound + ((fib(n-1)/fib(n)) * (upper.bound))

      ### Evaluate the function at the test points
      f1 = f(x1)
      f2 = f(x2)

      while ((lower.bound - upper.bound) > tolerance)
      {
      iteration = iteration + 1
      n = n - 1
      cat('', 'n')
      cat('Iteration #', iteration, 'n')
      cat('f1 =', f1, 'n')
      cat('f2 =', f2, 'n')

      if (f1 < f2)
      {
      cat('f(x1) < f(x2)', 'n')
      # If f(x1) < f(x2), then the new interval is [x1,b]:
      # a becomes the previous x1, b does not change

      lower.bound = x1
      upper.bound = upper.bound

      # x1 becomes the previous x2, n = n-1

      x1 = x2
      f1 = f2

      cat('New Upper Bound =', upper.bound, 'n')
      cat('New Lower Bound =', lower.bound, 'n')
      cat('New Upper Test Point = ', x1, 'n')

      ### Find the new x2 using the formula in Step2.
      x2 = lower.bound + (fib(n-1)/fib(n)) * (upper.bound)
      cat('New Lower Test Point = ', x1, 'n')
      f2 = f(x2)
      }
      else
      {
      cat('f(x1) > f(x2)', 'n')
      # If f(x1) > f(x2), then the new interval is [a,x2]:
      # a does not change, b becomes the previous x2

      upper.bound = x2
      lower.bound = lower.bound

      # x2 becomes the previous x1, n = n-1

      x2 = x1
      f2 = f1

      cat('New Upper Bound =', upper.bound, 'n')
      cat('New Lower Bound =', lower.bound, 'n')
      cat('New Lower Test Point = ', x1, 'n')

      # Find the new x1 using the formula in Step2.
      x1 = lower.bound + (fib(n-2)/fib(n)) * (upper.bound - lower.bound)
      cat('New Upper Test Point = ', x2, 'n')
      f1 = f(x1)
      }
      }

      ### Use the mid-point of the final interval as the estimate of the optimzer
      cat('', 'n')
      cat('Final Lower Bound =', lower.bound, 'n')
      cat('Final Upper Bound =', upper.bound, 'n')
      estimated.minimizer = (lower.bound + upper.bound)/2
      cat('Estimated Minimum Value (x) =', estimated.minimizer, 'n')
      cat('Estimated F(x)', f(estimated.minimizer),'n')

      # Step Three: Results

      ## Plot the curve of the user-defined function
      curve(f, from = Lb, to = Ub, main = expression("(x^4)-(14*(x^3))+(60*(x^2))-(70*(x))"))

      ## Plot the solution as a point
      points(estimated.minimizer, f(estimated.minimizer), col="red", pch=19)

      ## Draw a square around the optima to highlight it
      rect(estimated.minimizer-0.1, f(estimated.minimizer)-01, estimated.minimizer+0.1, f(estimated.minimizer)+01, lwd=2)
      }

      # Step Two: Loop Function
      fibonacci.search(f, Lb, Ub, Tl)









      share|cite|improve this question









      New contributor




      Josh K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      [edited] I compiled this code for an Operations Research class. I ended up submitting another format that relied on R packages; however, I feel that I am close to a functioning algorithm.



      I derived this code from another R program I complied for the 'Golden Search' algorithm. That program conducted 'bracket' searches until it found the optimal min/max. Each time I run this algorithm, it fails to conduct a 'bracket' search. After multiple hours, I have failed to find the issue. Any help finding the error in my code would be appreciated.



      My submitted (and confirmed) algorithm returned a minimum (x) value of 0.8095 with a f(x) value of -24.3445 utilizing a Fibonacci search method.



      # Step One: Initialization

      ## Input user-defined function
      f = function(x)
      {
      (x^4)-(14*(x^3))+(60*(x^2))-(70*(x))
      }

      ## Input user-defined parameters
      Lb <- 0 ### Lower Bound
      Ub <- 2 ### Upper Bound
      Tl <- .3 ### Tolerance

      n <- (Ub-Lb)/Tl
      n<- as.integer(n)

      fibonacci.search = function(f, lower.bound, upper.bound, tolerance)
      {
      iteration = 0

      Fn = fibonacci(n, sequence = FALSE)

      ### Use the fibonacci ratios to set the initial test points

      x1 = lower.bound + ((fib(n-2)/fib(n)) * (upper.bound - lower.bound))
      x2 = lower.bound + ((fib(n-1)/fib(n)) * (upper.bound))

      ### Evaluate the function at the test points
      f1 = f(x1)
      f2 = f(x2)

      while ((lower.bound - upper.bound) > tolerance)
      {
      iteration = iteration + 1
      n = n - 1
      cat('', 'n')
      cat('Iteration #', iteration, 'n')
      cat('f1 =', f1, 'n')
      cat('f2 =', f2, 'n')

      if (f1 < f2)
      {
      cat('f(x1) < f(x2)', 'n')
      # If f(x1) < f(x2), then the new interval is [x1,b]:
      # a becomes the previous x1, b does not change

      lower.bound = x1
      upper.bound = upper.bound

      # x1 becomes the previous x2, n = n-1

      x1 = x2
      f1 = f2

      cat('New Upper Bound =', upper.bound, 'n')
      cat('New Lower Bound =', lower.bound, 'n')
      cat('New Upper Test Point = ', x1, 'n')

      ### Find the new x2 using the formula in Step2.
      x2 = lower.bound + (fib(n-1)/fib(n)) * (upper.bound)
      cat('New Lower Test Point = ', x1, 'n')
      f2 = f(x2)
      }
      else
      {
      cat('f(x1) > f(x2)', 'n')
      # If f(x1) > f(x2), then the new interval is [a,x2]:
      # a does not change, b becomes the previous x2

      upper.bound = x2
      lower.bound = lower.bound

      # x2 becomes the previous x1, n = n-1

      x2 = x1
      f2 = f1

      cat('New Upper Bound =', upper.bound, 'n')
      cat('New Lower Bound =', lower.bound, 'n')
      cat('New Lower Test Point = ', x1, 'n')

      # Find the new x1 using the formula in Step2.
      x1 = lower.bound + (fib(n-2)/fib(n)) * (upper.bound - lower.bound)
      cat('New Upper Test Point = ', x2, 'n')
      f1 = f(x1)
      }
      }

      ### Use the mid-point of the final interval as the estimate of the optimzer
      cat('', 'n')
      cat('Final Lower Bound =', lower.bound, 'n')
      cat('Final Upper Bound =', upper.bound, 'n')
      estimated.minimizer = (lower.bound + upper.bound)/2
      cat('Estimated Minimum Value (x) =', estimated.minimizer, 'n')
      cat('Estimated F(x)', f(estimated.minimizer),'n')

      # Step Three: Results

      ## Plot the curve of the user-defined function
      curve(f, from = Lb, to = Ub, main = expression("(x^4)-(14*(x^3))+(60*(x^2))-(70*(x))"))

      ## Plot the solution as a point
      points(estimated.minimizer, f(estimated.minimizer), col="red", pch=19)

      ## Draw a square around the optima to highlight it
      rect(estimated.minimizer-0.1, f(estimated.minimizer)-01, estimated.minimizer+0.1, f(estimated.minimizer)+01, lwd=2)
      }

      # Step Two: Loop Function
      fibonacci.search(f, Lb, Ub, Tl)






      optimization fibonacci-numbers programming






      share|cite|improve this question









      New contributor




      Josh K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|cite|improve this question









      New contributor




      Josh K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|cite|improve this question




      share|cite|improve this question








      edited yesterday





















      New contributor




      Josh K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 9 at 22:54









      Josh K

      11




      11




      New contributor




      Josh K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Josh K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Josh K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "69"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Josh K is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2992046%2fissues-developing-fibonacci-search-algorithm-with-r%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Josh K is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Josh K is a new contributor. Be nice, and check out our Code of Conduct.













          Josh K is a new contributor. Be nice, and check out our Code of Conduct.












          Josh K is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2992046%2fissues-developing-fibonacci-search-algorithm-with-r%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?