Open mollyzyy787 opened 8 years ago
Yes you are correct, this is the initialization in the assignment: xkp1 = x xk = x + 1
where I am not using a Python array/list but simply two variables xk and xkp1 for the current and the next guess respectively. You need not store all the values of the gradient algorithm iterates, so you can just use two variables like I did above.
If you still want to use a Python list/array (after declaring x as an array or a list), for the index count k you can initialize k=0 before the while loop, initialize x[k] and x[k+1], and increment k by 1 inside the while loop. Does that help?
NOTE ALSO: The test suit will expect back the "final" iterate value, the global minimum. So your gradient_descent algorithm should return only the final iterate value and not a list/array. Refer to test_homework1.py provided.
Thank you mvelegar. I guess since I don't entirely understand how python works yet, what I still don't understand is that I thought xk is one of the argument of the function, and it should be given before xkp1 I assume. and since x itself is not defined, I don't understant how this will work inside the function?
Also, I understand that we don't need to provide an array, and we only need the final value, but I don't know how to iterate the process without writing them in a loop with index count? I also don't know how to incorporate the loop stop condition without using a "while" command.
x is x0, note that the provided signature of gradient_descent is:
def gradient_descent(f, df, x, sigma=0.5, epsilon=1e-8)
the third input argument defined as x is the initial guess x0 we will be passing to this function. So x will be defined/known in the function.
You do have to use a while command, I am not sure why you would need an index count unless you want to create a Python list with all of your iterates.
Also you asked: how can we incorporate the condition of abs(x[k+1]-x[k]) < epsilon in the while condition? Simply as: while (abs(xkp1 - xk) > epsilon): do something
Please review the lectures on Python if you have further questions regarding writing loops in Python.
Thanks mvelegar!!! I think I got gradient_step and gradient_descent all mixed up!! Now I understand :+1: :)
Good luck!
Hi, I was having a hard time understanding the backward point arrow in the problem statement of exercise 2. Should I understand x[k+1] <-- x0; x[k] <-- x0 +1 as x[k+1]=x0 and x[k]=x0+1? Also, in the function argument there is an xk, so when we initialize x[k+1], should it become xk-1 then?
Another question, I would assume we should use a while loop for this problem, but I don't know how to put index count in python, and if we define the value of x[k+1] and x[k] in the loop, how can we incorporate the condition of abs(x[k+1]-x[k])<epsilon in the "while" condition?
Thank you in advance!!