Added optim based iteration to solution.

The next step was to run the cost function through optimization to find
optimal theta values. That was implemented.