From 52c00df9019a6460e4dfea9b3e65953cb81a6882 Mon Sep 17 00:00:00 2001 From: asilador <asilador@illinois.edu> Date: Sat, 18 Feb 2017 15:39:49 -0600 Subject: [PATCH] Added part c. Done! --- Assignment 1/assignment1.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Assignment 1/assignment1.py b/Assignment 1/assignment1.py index 8e284d8..de0d8f4 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 -- GitLab