clear;
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-5;
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

 

 

결과

 

 

>> test
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
>>