I am not able to get the answer for the following code, I am using golden search section method.

3 views (last 30 days)
%defining interval and function
function y = gss(func,x)
y = 6*x^2+11*x-35
xL = input('Enter the lower bound value = ');
xU = input('Enter the upper bound value = ');
%evaluating the function at end points
fL = func(xL);
fU = func(xU);
%calculating intermidiate point
R = 0.5*(sqrt(5)-1);
d = R*(xU-xL);
x1 = xU-d;
x2 = xL+d;
%evaluate function at x1 and x2
f1 = func(x1);
f2 = func(x2);
%main loop
tol = 1e-4;
err = inf;
while err > tol
if f1 > f2
xU = x2; %moving upper bound to x2
fU = f2;
x2 = x1; %new x2
f2 = f1;
d = R*(xU-xL); %new x1
x1 = xU-d;
f1 = func(x1); %evaluating f1
elseif f1 < f2
xL = x1; %moving the lower bound to x1
fL = f2;
x1 = x2; %new x1
f2 = f1;
d = R*(xU-xL); %new x2
x2 = xL+d;
f2 = func(x2); %evaluating f2
else
xL = (x1+x2)/2;
xU = xL;
end
err = 2*abs(xU-xL)/(xU+xL); %to determine if converged
end
%calculating final answer
xe = (x1+x2)/2
end
[SL: formatted the code as code not text]

Answers (1)

Steven Lord
Steven Lord on 27 Sep 2022
You define a function named gss but you never call that function.
Also, I would remove the first three lines of your code.
clc
close all
clear all

Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!