# Closed form Solution
# See Tutorial #4 on optimization
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import root_scalar
np.set_printoptions(precision=2, suppress=True)
# Define the function sum(max(b-gamma,0))-1
def my_func(gamma,x0):
return np.sum(np.maximum(x0-gamma, 0))-1
# Solve for gamma in the equation sum(max(b-gamma,0))=1
x0 = np.random.rand(10)
gamma = root_scalar(my_func,args=(x0),bracket=[0, 3]).root
# Closed form solution
x = np.maximum(x0-gamma,0)
x
# Solve using CVX
import cvxpy as cvx
np.set_printoptions(precision=2, suppress=True)
c = np.ones(10)
# ===== Call CVX ==========
x = cvx.Variable(10)
objective = cvx.Minimize( cvx.sum_squares(x-x0) )
constraints = [ c @ x == 1,
x >= 0]
prob = cvx.Problem(objective, constraints)
prob.solve()
sol = x.value
# =========================
sol
%%shell
jupyter nbconvert --to html /content/ECE595_lecture06_constrained.ipynb