ME3255-Extra_Credit
Extra Credit 3
function [J,grad]=cost_logistic(a,x,y)
% Part A
oring=dlmread('data.csv',',',1,0);
x=oring(:,2);
y=oring(:,3);
N=length(y);
J=0;
grad=0;
t=a(1)+a(2).*x;
sigma=1./(1+exp(-t));
J=sum(-y.*log(sigma)- (1-y).*log(1-sigma));
costFunction=@ (a) sum(-y.*log((1./(1+exp(-(a(1)+a(2).*x)))))- (1-y).*log(1-(1./(1+exp(-(a(1)+a(2).*x))))));
grad=(1/length(x))*sum((sigma - y).*t);
% Part B
initial_a=[0 0];
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(a)(costFunction(a,x,y)), initial_a, options);
% Part C
T=theta(1)+theta(2).*x;
sigm=1./(1+exp(-T));
setdefaults
plot(x,y,'o', x, sigm);
title('Best fit regression')
xlabel('Temp (F)')
ylabel('Pass/Fail (0,1)')
end
-
solved for a0 and a1 on part b and plotted the data in part c.
-
(a) [J, grad] = cost_logistic(a, x, y)
J = 115.5085 grad = 5.0130
-
(b) The result for a0 and a1 generated through cost_logistic