%This function performs the golden search method as presented by Algorithm
%11.6.1.  Written by Philip Shaw, Fordham University, 2022.
function[B]=gss(A,D,fun)
p=(sqrt(5)-1)/2;
B=p*A+(1-p)*D;
C=(1-p)*A+p*D;
fB=fun(B);
fC=fun(C);
dist=2;
eps=.000001;
while dist>eps*max([1 abs(B)+abs(C)])
    if fB>fC
        D=C;
        C=B;
        fC=fB;
        B=p*C+(1-p)*A;
        fB=fun(B);
    else
        A=B;
        B=C;
        fB=fC;
        C=p*B+(1-p)*D;
        fC=fun(C);
    end
    dist=abs(D-A);
end