Getting error with my codes

Hello,

I am very new on R Programming Language . I try to solve golden section search method with using R language. I solved with excel the answer should be maximum deflection is 0.5152 at x =267.40 . this problem but also I want to solve with R. I am using the script that implemented this method In the codes I indicated proper function and boundary values but the result is not I exptected. I write my codes below, I cant find which part is wrong. thank you for interest.

My problem:

image

> w0 = 2.5; E = 50000; I = 30000; L=600;
> f <- function(x)  (wo/(120*E*I*L)*(-x.^5+2*L^2*x.^3-L^4*x);
> lower.bound = 0
> upper.bound = 600
> golden.section.search = function(f, lower.bound, upper.bound, tolerance)
> {
>    golden.ratio = 2/(sqrt(5) + 1)
> 
>    ### Use the golden ratio to set the initial test points
>    x1 = upper.bound - golden.ratio*(upper.bound - lower.bound)
>    x2 = lower.bound + golden.ratio*(upper.bound - lower.bound)
> 
>    ### Evaluate the function at the test points
>    f1 = f(x1)
>    f2 = f(x2)
> 
>    iteration = 0
> 
>    while (abs(upper.bound - lower.bound) > tolerance)
>    {
>       iteration = iteration + 1
>       cat('', '\n')
>       cat('Iteration #', iteration, '\n')
>       cat('f1 =', f1, '\n')
>       cat('f2 =', f2, '\n')
> 
>       if (f2 > f1)
>       # then the minimum is to the left of x2
>       # let x2 be the new upper bound
>       # let x1 be the new upper test point
>       {
>          cat('f2 > f1', '\n')
>          ### Set the new upper bound
>          upper.bound = x2
>          cat('New Upper Bound =', upper.bound, '\n')
>          cat('New Lower Bound =', lower.bound, '\n')
>          ### Set the new upper test point
>          ### Use the special result of the golden ratio
>          x2 = x1
>          cat('New Upper Test Point = ', x2, '\n')
>          f2 = f1
> 
>          ### Set the new lower test point
>          x1 = upper.bound - golden.ratio*(upper.bound - lower.bound)
>          cat('New Lower Test Point = ', x1, '\n')
>          f1 = f(x1)
>       } 
>       else 
>       {
>          cat('f2 < f1', '\n')
>          # the minimum is to the right of x1
>          # let x1 be the new lower bound
>          # let x2 be the new lower test point
> 
>          ### Set the new lower bound
>          lower.bound = x1
>          cat('New Upper Bound =', upper.bound, '\n')
>          cat('New Lower Bound =', lower.bound, '\n')
> 
>          ### Set the new lower test point
>          x1 = x2
>          cat('New Lower Test Point = ', x1, '\n')
> 
>          f1 = f2
> 
>          ### Set the new upper test point
>          x2 = lower.bound + golden.ratio*(upper.bound - lower.bound)
>          cat('New Upper Test Point = ', x2, '\n')
>          f2 = f(x2)
>       }
>    }
> 
>    ### 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 Minimizer =', estimated.minimizer, '\n')
> }

Since you're new here, it might be helpful to know how to properly format code and console output that you post here. Using proper code formatting makes the site easier to read, prevents confusion (unformatted code can get garbled by the forum software :anguished:), and is generally considered the polite thing to do. Check out this FAQ to find out how — it's as easy as the click of a button! :grinning::

It is ok now I guess :wink:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.