O restart; # conjugate direction algorithm: x(k+1) = x(k) + alpha(k) * d(k) with(plots): O # Here's our function -- note that I am not starting with Q and b, so I will not # be able to use our closed form expression for alpha(k). f := (x,y) -> (1/2)*(x-1)^2+3*(y+1/2)^2-4; 1 1 2 f := x, y / xK1 2 C3 yC K4 2 2 O # Here's the plot...in the Maple worksheet, you can spin it around. (1) plot3d(f(x,y),x=-2..2,y=-2..2, axes=boxed); O # The minimizer is clearly at (1, -1/2). x[0] := [3,2]; Let's start at (3,2). x0 := 3, 2 O # Here's our first search direction: d(0) = -g(0). can use subs instead of unapply if you want.... (2) (NOTE: You O O d[0][1] := -unapply(diff(f(x,y),x),(x,y))(3,3); d[0][2] := -unapply(diff(f(x,y),y),(x,y))(3,3); d0 := K2 1 d0 := K21 2 (3) O # Let's just pick alpha(0) out of the air (refining it as necessary): (You could do a line search to find it, though there's a formula for it in the case of f being quadratic. alpha[0] := 0.12; a 0 := 0.12 (4) O # Now we can update x to get our next iterate: # (This block also gives the function value at our new x.) x[1][1] := x[0][1] + alpha[0]*d[0][1]; x[1][2] := x[0][2] + alpha[0]*d[0][2]; f(x[1][1], x[1][2]); x1 := 2.76 1 x1 := K0.52 2 K2.450000000 (5) O # Here's our next search direction: d(1) = -g(1) + beta(0)*g(0). # To start, here's the gradient: g[1][1] := unapply(diff(f(x,y),x),(x,y))(x[1][1],x[1][2]); g[1][2] := unapply(diff(f(x,y),y),(x,y))(x[1][1],x[1][2]); g1 := 1.76 1 g1 := K0.12 2 (6) O # Using Fletcher-Reeves (since it's simple and I don't have Q handy): (d[0]=-g[0]) beta[0] := (g[1][1]^2 + g[1][2]^2)/(d[0][1]^2 + d[0][2]^2); b 0 := 0.006993258427 (7) O # So here's our direction: d[1][1] := -g[1][1] + beta[0]*d[0][1]; d[1][2] := -g[1][2] + beta[0]*d[0][2]; d1 := K1.773986517 1 d1 := K0.0268584270 2 (8) O #Again, we pick alpha out of the air (or with a formula for quadratics or with a line search more generally): alpha[1] := 1; a 1 := 1 O #Finally, here's x[2] and f(x[2]): x[2][1] := x[1][1] + alpha[1]*d[1][1]; (9) O x[2][2] := x[1][2] + alpha[1]*d[1][2]; f(x[2][1],x[2][2]); x2 := 0.986013483 1 x2 := K0.5468584270 2 K3.993315052 O # Recall that the minimizer was at (1, -1/2) and the value of f there is -4. As expected, in 2 variables, with a quadratic, conjugate direction required 2 steps. (10)