Function to compute the optimal weights matematically and some figures