Skip to content
Snippets Groups Projects
Commit 52c00df9 authored by asilador's avatar asilador
Browse files

Added part c. Done!

parent 442752b7
No related branches found
No related tags found
No related merge requests found
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment