function [J grad h th] = cost(thetas, xtrain,ytrain,alpha,iter)

th=thetas
m=size(xtrain,1); % length of training input data
for j=1:iter
    h=logsig(xtrain*th); % sigmoid function
    J=-(1/m)*sum(ytrain.*log(h)+(1-ytrain).*log(1-h)); % cost function
    th=th+(alpha/m)*xtrain'*(ytrain-h) % update the weights
end
clear i k;
k=length(theta);
grad=zeros(k,1);

for i=1:k
    grad(i)=(1/m)*sum((h-ytrain)'*xtrain(:,i)); % gradient calculation
end
end

