Skip to content
Permalink
048fa8e03f
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
37 lines (23 sloc) 824 Bytes
function [cost,gradient]=cost_logistic(a,x,y)
%cost_logistics calculates the cost of the and the graident of that
%sets n to the lenght of y
n=length(y);
%uses equations given in problem statement
sigma=@(t) 1./(1+exp(-t));
syms a0 a1
t=a0+a1*x;
% calculates the cost with the variabels a0 and a1 not plugged in
cost = (1/n)*sum(-y.*log(sigma(t))-(1-y).*log(1-sigma(t)));
%find the two gradients using the diff function
gradient(1)=diff(cost,a0);
gradient(2)=diff(cost,a1);
%sets a0 and a2
a0=a(1);
a1=a(2);
%substites a0 and a2 into the cost and the graients and converts them
%to number arrays
cost=double(subs(vpa(cost)));
gradient=double(subs(vpa(gradient)));
%plots the data
plot(x,y, x, sigma(a(1)+a(2)*x));
end