PHYCS 6730 Lab Exercise: Introduction to the Conjugate Gradient Algorithm

These exercises use Maple to illustrate how the conjugate gradient method works. The Maple setup files are in ~p6730/exercises/cg. Start Maple and use the command read `maplesd`; or read `maplecg`;.

We are solving the problem

      A x = b

      [ 5     4.5]
 A := [          ]
      [4.5    4.5]
       
      [ 1  ]        [ u ]
 b := [    ]   x := [   ]
      [ -1 ]        [ v ]
Note that the matrix is symmetric, so we can use simplified methods.

The contour plot shows the elevation of the function

  f(x) = x * A x/2 - b*x
which is minimum when x solves the linear system.

Exercise 1. Steepest descent

Start Maple and load the file maplesd. This file gets you started doing the steepest descent algorithm. Continue the iteration until the residual vector components are both less than 2x10-4. Try plotting the series of trial solution points xk.

Hand in your Maple session. Also give your best answer (within the error of the method.)


    Steepest descent algorithm

    x0 = 0 (or any guess)
    For k = 1 to MAX (until converged)
      rk-1 = b - Axk-1
      Stop when rk-1 = 0 (converged)
      alphak = rk-1*rk-1/rk-1*A rk-1
      xk = xk-1 + alphak rk-1
    Loop

Exercise 2. Conjugate gradient

The file maplecg gets you started with the conjugate gradient algorithm. Carry out further iterations until the residual vector components are both less than 10-7. Try plotting the series of trial solution points xk.

Hand in your Maple session. Also give your best answer (within the error of the method.) Comment on the difference between this method and steepest descent.


    Conjugate gradient algorithm

    x0 = 0 (or any guess)
    r0 = b - A x0
    p0 = r0
    For k = 1 to MAX (until converged)
      alphak = rk-1*rk-1/pk-1*A pk-1
      xk = xk-1 + alphak pk-1
      rk = rk-1 - alphak A pk-1
      Stop when rk = 0 (converged)
      betak = rk*rk/rk-1*rk-1
      pk = rk + betak pk-1
    Loop