I am trying gradient descent, I wrote following however not getting any answer,
n=0; %initialize iteration counter
eps=1; %initialize error
a=0.8; %set iteration parameter
x=[1;1]; %set starting value
f=6*x(1)^2+8*x(2)^2-3*x(1)*x(2);
%Computation loop
while eps>1e-12||n<100
gradf=[12*x(1)-3*x(2); 16*x(2)-3*x(1)]; %gradf(x)
eps=(norm(gradf)/(1+abs(f))); %error
y=x-a*gradf; %iterate
x=y; %update x
n=n+1; %counter+1
end
n;x;eps; %display end values
When I add this file to path and type x it shows NaN, NaN. what is wrong?
There are several errors in your code. Consider this (I put comments where corrections were needed)
for instance with the current value
a=.1I getThat is I had to perform 100 iteration because my epsilon is still above the threshold. If I allow 200 iterations I get
I.e. 110 iterations are sufficient.
Case of a general
f(i.e. not a quadratic form).You can use, for instance, function handles, i.e. you define (before the
while)then, in the
whileyou substituteP.S. for what concerns the
whilecycle, please keep in mind that you keep on iterating while you are not satisfied with your precision (eps>1e-12) AND your total number of iteration is below a given threshold (n<100).Consider also that you are working in finite precision: a numerical algorithm can never reach the analytic solution (i.e. what you have with infinite precision and infinite iterations), therefore, you always have to set a threshold (
eps, which should be above the machine precision \approx1e-16) and that is your0.