function main
%mu1=10;    % mean of the first gaussian
%sigma1=5;  % standard deviation
%n1 = 100;   % number of examples from the first Gaussian
mu1 = input('mu1:');
sigma1 = input('sigma1:');
n1 = input('n1:');
x1 = normrnd(mu1, sigma1, n1, 1); 
% generate 20 random numbers from the above gaussian

%mu2=15;   % mean of the second gaussian
%sigma2=5;  % standard deviation
%n2 = 120;   % number of examples
mu2 = input('mu2:');
sigma2 = input('sigma2:');
n2 = input('n2:');
x2=normrnd(mu2, sigma2, n2, 1); 
% generate 20 random numbers from the above gaussian

%finding the decision boundary
xbar1 = mean(x1)
sx1 = std(x1)

xbar2 = mean(x2)
sx2 = std(x2)

syms x;

f = (x - xbar2)^2 / (2 * (sx2)^2) - (x - xbar1)^2 / (2 * (sx1)^2) + log(sx2/sx1) + log(n1/n2);
eval(solve(f,x))

%fprintf('decision boundary: ');
%one = 1/2/(-sx1^2+sx2^2)*(2*sx2^2*xbar1-2*sx1^2*xbar2+2*(-2*sx2^2*xbar1*sx1^2*xbar2+sx1^2*sx2^2*xbar1^2-2*sx1^4*log(n1/n2)*sx2^2-2*sx1^4*log(sx2/sx1)*sx2^2+sx2^2*sx1^2*xbar2^2+2*sx2^4*log(n1/n2)*sx1^2+2*sx2^4*log(sx2/sx1)*sx1^2)^(1/2));
%two = 1/2/(-sx1^2+sx2^2)*(2*sx2^2*xbar1-2*sx1^2*xbar2-2*(-2*sx2^2*xbar1*sx1^2*xbar2+sx1^2*sx2^2*xbar1^2-2*sx1^4*log(n1/n2)*sx2^2-2*sx1^4*log(sx2/sx1)*sx2^2+sx2^2*sx1^2*xbar2^2+2*sx2^4*log(n1/n2)*sx1^2+2*sx2^4*log(sx2/sx1)*sx1^2)^(1/2));

%graph the decision boundary
%b=0:0.0001:0.25;

%if (one > xbar1 & one < xbar2)
%    line(one,b);
%    dec = one
%else
%    line(two,b);
%    dec = two
%end
%hold on;

%graph f
z = xbar1-2*sx1:0.0001:xbar2+2*sx2;
f = (z - xbar2).^2 / (2 * (sx2)^2) - (z - xbar1).^2 / (2 * (sx1)^2) + log(sx2/sx1) + log(n1/n2);
plot(z,f, 'g-');
hold on;

%graph the two gaussians
plot(z, normpdf(z,xbar1,sx1) );
hold on;
plot(z, normpdf(z,xbar2,sx2), 'r-' );

%testing a value
user_x = input('enter a test value: ');
num = (user_x - xbar2)^2 / (2 * (sx2)^2) - (user_x - xbar1)^2 / (2 * (sx1)^2) + log(sx2/sx1) + log(n1/n2);

if (num>0)
    fprintf('choose group 1.\n');
else
    fprintf('choose group 2.\n');
end

end


function drawHist(x1, x2)
hist(x1);
hold on;
hist(x2);
end