### Sage code for 4(b) in challenge set 1. Use Sage online at http://www.sagemath.org.
### All statements preceded by hash marks (like this one) are comments.
### This command defines the class of polynomials in 2 variables, x and y, with real coefficients. It has to come before everything else we do here:
R. = RR['x,y']
### Define the function f(x, y):
f = exp(-x^2 - y^2)
### Pick a random starting point [a, b] in the region [-1, 1]x[-1, 1]. (random() gives a number between 1 and 0, so we scale and shift its output.):
[a, b] = [random()*2 - 1, random()*2 - 1]
### Print the coordinates of the starting point together with the z-value f(a, b) at the starting point. Also print the gradient of f at [a, b].
print "start"
print [a, b, f(x = a, y = b)]
print f.gradient()(x = a, y = b)
### This is just a line break to make the output easier to read:
print "\n"
### Now take 10 steps with step size 0.2. (Note that the loop stops just before the end of the range, so i runs from 1 to 10 inclusive. Also note that the values represented by "a" and "b" are changed at every step---we update the values to the coordinates of the new point each time.)
for i in range(1, 11):
### take the i-th step in the direction of the gradient at the current point, updating [a, b] to the (x, y)-coordinates of the new point:
[a, b] = [a + f.diff(x)(x = a, y = b)*0.2, b + f.diff(y)(x = a, y = b)*0.2]
### print out the step number for easy reading...
print i
### the (x, y, z)-coordinates of the new point...
print [a, b, f(x = a, y = b)]
### ...and the gradient at the new point:
print f.gradient()(x = a, y = b)
### ...plus a line break:
print "\n"
### Finally, print out the (x, y, z)-coordinates of the stopping point (this will repeat the last item of the loop).
print "end"
print [a, b, f(x = a, y = b)]
### This command defines the class of polynomials in 2 variables, x and y, with real coefficients. It has to come before everything else we do here.
R. = RR['x,y']
### Define the function f(x, y):
f = exp(-x^2 - y^2)
### Pick a random starting point [a, b] in the region [-1, 1]x[-1, 1]. (random() gives a number between 1 and 0, so we scale and shift its output.)
[a, b] = [random()*2 - 1, random()*2 - 1]
### Print the coordinates of the starting point together with the z-value f(a, b) at the starting point. Also print the gradient of f at [a, b].
print "start"
print [a, b, f(x = a, y = b)]
print f.gradient()(x = a, y = b)
### This is just a line break to make the output easier to read:
print "\n"
### Now take 10 steps with step size 0.2. (Note that the loop stops just before the end of the range, so i runs from 1 to 10 inclusive. Also note that the values represented by "a" and "b" are changed at every step---we update the values to the coordinates of the new point each time.)
for i in range(1, 11):
### take the i-th step in the direction of the gradient at the current point, updating [a, b] to the (x, y)-coordinates of the new point:
[a, b] = [a + f.diff(x)(x = a, y = b)*0.2, b + f.diff(y)(x = a, y = b)*0.2]
### print out the step number for easy reading...
print i
### the (x, y, z)-coordinates of the new point...
print [a, b, f(x = a, y = b)]
### ...and the gradient at the new point:
print f.gradient()(x = a, y = b)
### ...plus a line break:
print "\n"
### Finally, print out the (x, y, z)-coordinates of the stopping point (this will repeat the last item of the loop).
print "end"
print [a, b, f(x = a, y = b)]