I have to implement the steepest descent method and test it on functions of two variables, using Matlab. Here's what I did so far:
x_0 = [0;1.5]; %Initial guess
alpha = 1.5; %Step size
iteration_max = 10000;
tolerance = 10e-10;
% Two anonymous function to compute 1st and 2nd entry of gradient
f = @(x,y) (cos(y) * exp(-(x-pi)^2 - (y-pi)^2) * (sin(x) - 2*cos(x)*(pi-x)));
g = @(x,y) (cos(x) * exp(-(x-pi)^2 - (y-pi)^2) * (sin(y) - 2*cos(y)*(pi-y)));
%Initiliazation
iter = 0;
grad = [1; 1]; %Gradient
while (norm(grad,2) >= tolerance)
grad(1,1) = f(x_0(1), x_0(2));
grad(2,1) = g(x_0(1), x_0(2));
x_new = x_0 - alpha * grad; %New solution
x_0 = x_new %Update old solution
iter = iter + 1;
if iter > iter_max
break
end
end
The problem is that, compared to for example the results from WolframAlpha, I do not obtain the same values. For this particular function, I should obtain either (3.14,3.14) or (1.3,1.3) but I obtain (0.03, 1.4).