function [ w_i, data ] = prob_4_7(Niter) % PROB_4_7 - % % Written by: % -- % John L. Weatherwax 2005-08-04 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- data = [ 1 1 -1 0 2; 0 0 1 2 0; -1 -1 1 1 0; 4 0 1 2 1; -1 1 1 1 0; -1 -1 -1 1 0; -1 1 1 2 1 ]; class = [ 2 1 2 1 1 1 2 ]; d = 0.1; w_i = [ 1 1 1 -1 -1 1 ]; % % Code begins: %--- % Append +1 to all data: data = [ data, ones(size(data,1),1) ]; % Negate the feature vector from class 2: ind = find( class==2 ); data(ind,:) = -data(ind,:); clear ind; w_history = zeros( Niter, length(w_i) ); for ni=0:Niter+1, si = mod( ni, size(data,1) )+1; y_i = data( si, : ).'; v_i = y_i; e_i = 1; if( w_i * y_i < d ) % This element is INCORRECTLY classified f2Prime = 2 * ( d - w_i * y_i ); w_ip1 = w_i + e_i * f2Prime * y_i.'; else w_ip1 = w_i; % This element is CORRECTLY classified end % Save w iterates: w_history( ni+1, : ) = w_i; % Prepare for next iteration: w_i = w_ip1; end w_history(end-4:end,:) % $$$ % Plot results: % $$$ w_plot = w_history( 1:10:end, : ); % $$$ figure; plot( w_plot, '-o' );