%%% N_variable_book.m %%%
clear;clc;
N=10; w = 0.5*ones(1,N); eta = 0.7;
E = inline('0.5*sum((w-[1:N]/N).^2)','w','N');
dEdw = inline('xx-tt','xx','tt');
fprintf('W(1) = (%f, %f, %f, %f, %f, %f, %f, %f, %f, %f),E(W(1))=%f \n',w,E(w,N));
tol = 1.0e-4;
cost1 = E(w,N);
err = 2.0*tol;
k=1;
while err>tol
    for i=1:N
        new_w(i) = w(i) - eta*dEdw(w(i),i/N);
    end
    w=new_w;
fprintf('W(%d) = (%f, %f, %f, %f, %f, %f, %f, %f, %f, %f),E(W(%d))=%f \n',k+1,w,k+1,E(w,N));
cost2 = E(w,N);
err = abs(cost2 - cost1);
cost1 = cost2;
k=k+1;
end