% 7.2 steepest descent example

clear
format long

f=@(x,y,z) 2*x.^2-2*x+y.^2-4*y+z.^2-6*z+13;
gradf=@(x,y,z) [4*x-2 2*y-4 2*z-6];

x=0;y=0;z=0; % initial point
df=gradf(x,y,z);

% first step - subtract alpha times gradient
falpha=@(x,y,z,dfx,dfy,dfz,alpha) f(x-alpha*dfx,y-alpha*dfy,z-alpha*dfz);

alpha=(1:15)'/15;
figure;plot(alpha,falpha(x,y,z,df(1),df(2),df(3),alpha))

[bestalpha,minval]=GoldenSectionFn(@(alpha) falpha(x,y,z,df(1),df(2),df(3),alpha),0,1);

% run multiple iterations until residual error sufficiently small
x=0;y=0;z=0; % initial point
count=0;
while norm(gradf(x,y,z))>1e-6 && count<100
    count=count+1; 
    df=gradf(x,y,z);
    % first find optimal alpha
    [bestalpha,minval]=GoldenSectionFn(@(alpha) falpha(x,y,z,df(1),df(2),df(3),alpha),0,1);
    x=x-bestalpha*df(1);
    y=y-bestalpha*df(2);
    z=z-bestalpha*df(3);
end
[x y z]
f(x,y,z)