%input: a column vector of weights w and a bias b of a fixed line %in R^2, a numpoints by 2 matrix of data points where each row %correspons to an point in two dimensions and a scaling constant c. %output: the weights and bias of the linear separator computed using the %SVM algorithm on the data x (with labels dictated by w^Tx + b) with %scaling c. The code also plots the ground truth and the learned classifier. function r = plotsvm(w,b,x,c) [numpoints, numcols] = size(x); %find the max-margin classifier y = sign(x*w + b); A = [x(:,1).*y x(:,2).*y y]; r = quadprog(diag([1;1;0]),zeros(3,1),-A,-c*ones(numpoints,1)); %make nice plots figure; scatter(x(:, 1),x(:, 2),30*ones(numpoints,1),[(y+1)/2 zeros(numpoints,1) (y+1)/2], 'fill'); hold; plot(0:.1:1, (-w(1)*[0:.1:1; 0:.1:1]-b)/w(2), '--r','LineWidth',2); msvm = r(1)/-r(2); bsvm = r(3)/-r(2); plot(0:.1:1, msvm*(0:.1:1)+bsvm, '--b','LineWidth',2); plot(0:.1:1, msvm*(0:.1:1)+bsvm-c/r(2), 'b','LineWidth',2); plot(0:.1:1, msvm*(0:.1:1)+bsvm+c/r(2), 'b','LineWidth',2); axis manual; axis([0 1 0 1]);