function h = plotlogreg(w,col,xrange,yrange)
% h = plotlogreg(w,col,xrange,yrange)
% Plots the decision boundary induced by a 2D logistic regression model
% with parameters w. 
% Uses current active figure to plot, returns handle to the current figure.

%
%    Greg Shakhnarovich 2002

if (nargin < 2)
  col = 'r--';
end
if nargin<3
  xrange = [-5 5];
end
if nargin<4
  yrange = xrange;
end

figure = gcf;
[x,y] = meshgrid(fillrange(xrange),fillrange(yrange));
switch length(w),
 case 3,
  p = g(w(1) + x*w(2)+y*w(3))-0.5;
 case 5,
  p = g(w(1) + x*w(2)+y*w(3)+x.^2*w(4)+y.^2*w(5))-0.5;
 case 6,
  p = g(w(1) + x*w(2)+y*w(3)+(x.^2)*w(4)+(y.^2)*w(5)+(x.*y)*w(6))-0.5;
end
contour(x,y,p,[0,0],col);

h = gcf;

function p = g(z)
p = 1./(1+exp(-z));

function filledrange = fillrange(range)
filledrange = range(1):((range(2)-range(1))/100):range(2);
