%%% 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-10;
cost1=E(w,N);
err=2.0*tol;
i=1;
while err>tol
    for j=1:N
        new_w(j)=w(j)-eta*dEdw(w(j),j/N);
    end
    w=new_w;
    fprintf('W(%d) = (%f, %f, %f, %f, %f, %f, %f, %f, %f, %f), E(W(%d))=%f \n',i+1,w,i+1,E(w,N));
    cost2=E(w,N);
    err=abs(cost2-cost1);
    cost1=cost2;
    i=i+1;
end

 

w(1)=(0.500000, 0.500000, 0.500000, 0.500000, 0.500000, 0.500000, 0.500000, 0.500000, 0.500000, 0.500000), E(W(1))=0.425000
W(2) = (0.220000, 0.290000, 0.360000, 0.430000, 0.500000, 0.570000, 0.640000, 0.710000, 0.780000, 0.850000), E(W(2))=0.038250
W(3) = (0.136000, 0.227000, 0.318000, 0.409000, 0.500000, 0.591000, 0.682000, 0.773000, 0.864000, 0.955000), E(W(3))=0.003443
W(4) = (0.110800, 0.208100, 0.305400, 0.402700, 0.500000, 0.597300, 0.694600, 0.791900, 0.889200, 0.986500), E(W(4))=0.000310
W(5) = (0.103240, 0.202430, 0.301620, 0.400810, 0.500000, 0.599190, 0.698380, 0.797570, 0.896760, 0.995950), E(W(5))=0.000028
W(6) = (0.100972, 0.200729, 0.300486, 0.400243, 0.500000, 0.599757, 0.699514, 0.799271, 0.899028, 0.998785), E(W(6))=0.000003
W(7) = (0.100292, 0.200219, 0.300146, 0.400073, 0.500000, 0.599927, 0.699854, 0.799781, 0.899708, 0.999636), E(W(7))=0.000000
W(8) = (0.100087, 0.200066, 0.300044, 0.400022, 0.500000, 0.599978, 0.699956, 0.799934, 0.899913, 0.999891), E(W(8))=0.000000
W(9) = (0.100026, 0.200020, 0.300013, 0.400007, 0.500000, 0.599993, 0.699987, 0.799980, 0.899974, 0.999967), E(W(9))=0.000000
W(10) = (0.100008, 0.200006, 0.300004, 0.400002, 0.500000, 0.599998, 0.699996, 0.799994, 0.899992, 0.999990), E(W(10))=0.000000
W(11) = (0.100002, 0.200002, 0.300001, 0.400001, 0.500000, 0.599999, 0.699999, 0.799998, 0.899998, 0.999997), E(W(11))=0.000000
W(12) = (0.100001, 0.200001, 0.300000, 0.400000, 0.500000, 0.600000, 0.700000, 0.799999, 0.899999, 0.999999), E(W(12))=0.000000