diff --git a/Assignment 1/assignment1.py b/Assignment 1/assignment1.py index 8e284d8b5a76c3e6c79eab8d7113c8aede09839d..de0d8f43d5a9671a3ef7a172891ff4ca0205d7ba 100644 --- a/Assignment 1/assignment1.py +++ b/Assignment 1/assignment1.py @@ -1,4 +1,6 @@ import numpy as np +import numpy +from scipy.optimize import minimize # Import text files Q = np.asmatrix(np.loadtxt('Q.txt')) @@ -6,10 +8,17 @@ b = np.asmatrix(np.loadtxt('b.txt')) c = np.asmatrix(np.loadtxt('c.txt')) b = np.transpose(b) #make b a column vector m = 1 +x_c = np.transpose(np.asmatrix(np.zeros(np.size(b)))*1) # Define f(x) -def f(Q,b,c,x): - return np.transpose(x)*Q*x + np.transpose(b)*x + c +def f(Q,b,c,x): + if np.shape(x) == (5L,1L): + ans = np.transpose(x)*Q*x + np.transpose(b)*x + c + else: + + x = np.transpose(np.asmatrix(x)) + ans = np.transpose(x)*Q*x + np.transpose(b)*x + c + return ans # Define gradient f(x) def gradf(Q,b,x): @@ -63,6 +72,17 @@ def partb(Q,b,x): print f(Q,b,c,x) print 'f(x*) is', f(Q,b,c,ans) return 0 + +class partcfun: + def __init__(self,arg1): + self.arg1 = arg1 + def __call__(self,arg1): + return f(Q,b,c,arg1) + +def partc(x_0c): + cans = minimize(partcfun(x_c),x_0c) + print 'x is', cans + return 0 def run(epsilon): # Make a guess for x vector @@ -76,6 +96,9 @@ def run(epsilon): grad_opt(epsilon,x,count,alpha0) print '******** Part B ********' partb(Q,b,x) + print '******** Part C ********' + x_0c = np.transpose(np.asmatrix(np.ones(np.size(b)))*1) + partc(x_0c) return 0 run(0.00001) \ No newline at end of file