Output and plot the cost function with iteration.