Skip to content
Snippets Groups Projects
Commit 442752b7 authored by asilador's avatar asilador
Browse files

Added part b and cleaned up code

parent 3694663a
No related branches found
No related tags found
No related merge requests found
......@@ -7,10 +7,6 @@ c = np.asmatrix(np.loadtxt('c.txt'))
b = np.transpose(b) #make b a column vector
m = 1
# Initialize iteration counter
global x
global count
# Define f(x)
def f(Q,b,c,x):
return np.transpose(x)*Q*x + np.transpose(b)*x + c
......@@ -33,9 +29,7 @@ def armijo(alpha0,Q,b,c,D,m,x):
alpha = beta**m*s
#print('alpha is ', alpha)
return alpha
# Begin Gradient Descent Algorithm
def grad_opt(epsilon,x,count,alpha):
#print('alpha is ', alpha)
......@@ -58,6 +52,18 @@ def grad_opt(epsilon,x,count,alpha):
print 'epsilon is ', epsilon
return 0
#part b
#gradient of f * x = zero vector. solve for x by getting inverse of grad_f
# grad f = 2*Q*x + b
#setting to zero gives us 2*Q*x = -b
def partb(Q,b,x):
Qinv = np.linalg.inv(2*Q)
ans = Qinv*(-b)
print '\n Solving for x* using the inverse of 2*Q*x=-b is', ans
print f(Q,b,c,x)
print 'f(x*) is', f(Q,b,c,ans)
return 0
def run(epsilon):
# Make a guess for x vector
#x = np.asmatrix(np.zeros(np.size(b)))
......@@ -66,7 +72,10 @@ def run(epsilon):
x = np.transpose(np.asmatrix(np.ones(np.size(b)))*1)
count = 0
alpha0 = 1.0
print '******** Part A ********'
grad_opt(epsilon,x,count,alpha0)
print '******** Part B ********'
partb(Q,b,x)
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