Permalink
Cannot retrieve contributors at this time
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?
curve_fitting/cost_logistic.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
37 lines (23 sloc)
824 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |