ME3255-Extra_Credit
Extra Credit Assignments
Extra Credit #3
data=[1.0000 , 53.0000 , 1.0000
2.0000 , 57.0000 , 1.0000
3.0000 , 58.0000 , 1.0000
4.0000, 63.0000 , 1.0000
5.0000 , 66.0000 , 0
6.0000 , 66.8000 , 0
7.0000 , 67.0000 , 0
8.0000 , 67.2000 , 0
9.0000 , 68.0000 , 0
10.0000 , 69.0000 , 0
11.0000 , 69.8000 , 1.0000
12.0000 , 69.8000 , 0
13.0000, 70.2000 , 1.0000
14.0000 , 70.2000 , 0
15.0000 , 72.0000 , 0
16.0000 , 73.0000 , 0
17.0000 , 75.0000 , 0
18.0000 , 75.0000 , 1.0000
19.0000 , 75.8000 , 0
20.0000 , 76.2000 , 0
21.0000 , 78.0000 , 0
22.0000 , 79.0000, 0
23.0000 , 81.0000 , 0];
n=23;
x=data(:,3);
y=data(:,2);
initial_a=[0,0];
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[a] = ...
fminunc(@(a) sum(-y.*log(1./(1+exp(-a(1)-a(2).*x)))-(1-y).*log(1./(1+exp(-a(1)-a(2).*x)))), initial_a, options);
function [cost,grad]=cost_logistic(a,x,y)
n=23;
x=data(:,3);
y=data(:,2);
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)(sum(-y.*log(1./(1+exp(-a.*x)))-(1-y).*log(1./(1+exp(-a.*x))))), initial_a, options);
t=a.*x;
sigma=1./(1+exp(-t));
cost=zeros(1,n);
grad=zeros(1,n);
for i=1:n
cost(i)=sum(-y(i).*log(sigma(i))-(1-y(i)).*log(1-sigma(i)))
grad(i)=sum(sigma(i)-y(i)).*x(i)
end
end