Conjugate Gradient Method in MATLAB: Project 2 for Math 526, Study Guides, Projects, Research of Mathematics

A matlab lab project for math 526 students, focusing on the conjugate gradient method for solving the linear system ax = b. It includes an overview of iterative methods, pseudocode for the cg algorithm, and assignments to implement and analyze the method using given matrices and vectors.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 10/01/2009

koofers-user-qzu
koofers-user-qzu 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATLAB Lab for Math 526 Project 2
Iterative Methods and Conjugate Gradient
Kamala Diefenthaler ([email protected])
Department of Mathematics
Overview
Discuss iterative methods to solve Ax=b, when Ais n×nand invertible.
Write a Matlab script that implements the Conjugate Gradient method (CG).
Find the condition number of a matrix.
Activities
Read the Additional Notes page to understand the CG method.
xkthe approximate solution to Ax=b. It is the projection of the true solution
x=A1bonto Kk.
rkthe residual vector. It is equal to bAxk, the error in our approximation;
however in the algorithm, we compute it with a different formula. The key step
in CG is that rkis made orthogonal to Kk.
pkthe conjugate gradient vector. It’s described on the Additional Notes page.
Pseudocode for the conjugate gradient:
% Initialize values
x=0;
r0=b;
p0=b;
for k >= 1
αk= (rT
k1rk1)/(pT
k1Apk1)% step length
xk=xk1+αkpk1% approximate solution
rk=rk1αkApk1% error
βk= (rT
krk)/(rT
k1rk1)% improvement
pk=rk+βkpk1% search direction
% Terminate if ||rk|| < or if k=n.
Assignments
1. Let = 108be your termination criterion.
Write and submit a Matlab script to implement the conjugate gradient algorithm
to solve Ax=bwith the following modifications:
(i) Do one multiplication by Aper iteration (pseudocode does two).
(ii) Compute only three dot products per iteration (pseudocode does five count-
ing the norm of rto check to see if you have converged).
(iii) Store only the three vectors xk,rk, and pkto pass to the next iteration (you
will also need to store Apkfor use within each iteration).
1 April 15, 2009
pf2

Partial preview of the text

Download Conjugate Gradient Method in MATLAB: Project 2 for Math 526 and more Study Guides, Projects, Research Mathematics in PDF only on Docsity!

MATLAB Lab for Math 526 Project 2

Iterative Methods and Conjugate Gradient

Kamala Diefenthaler ([email protected]) Department of Mathematics

Overview

Discuss iterative methods to solve Ax = b, when A is n × n and invertible. Write a Matlab script that implements the Conjugate Gradient method (CG). Find the condition number of a matrix.

Activities

Read the Additional Notes page to understand the CG method.

  • xk the approximate solution to Ax = b. It is the projection of the true solution x = A−^1 b onto Kk.
  • rk the residual vector. It is equal to b − Axk, the error in our approximation; however in the algorithm, we compute it with a different formula. The key step in CG is that rk is made orthogonal to Kk.
  • pk the conjugate gradient vector. It’s described on the Additional Notes page.

Pseudocode for the conjugate gradient: % Initialize values x=0; r0=b; p0=b; for k >= 1 αk = (rTk− 1 rk− 1 )/(pTk− 1 Apk− 1 ) % step length xk = xk− 1 + αkpk− 1 % approximate solution rk = rk− 1 − αkApk− 1 % error βk = (rTk rk)/(rTk− 1 rk− 1 ) % improvement pk = rk + βkpk− 1 % search direction % Terminate if ||rk|| <  or if k = n.

Assignments

  1. Let  = 10−^8 be your termination criterion. Write and submit a Matlab script to implement the conjugate gradient algorithm to solve Ax = b with the following modifications:

(i) Do one multiplication by A per iteration (pseudocode does two). (ii) Compute only three dot products per iteration (pseudocode does five count- ing the norm of r to check to see if you have converged). (iii) Store only the three vectors xk, rk, and pk to pass to the next iteration (you will also need to store Apk for use within each iteration).

1 April 15, 2009

MATLAB Lab for Math 526 Project 2

(iv) Print or display the value of k for which the program terminated. (v) Store ||rk|| for each k so that after the code has run, you can analyze how xk converged to the solution.

  1. Load the file Proj-2.mat into Matlab. The file contains the matrices A, B, C, and the vector b.
  2. Use your CG algorithm to solve Ax = b, Bx = b, and Cx = b.
  3. For each equation, how many steps did CG need to converge?
  4. Make a plot of ||rk|| as a function of k to see how the error goes down. (Use semilogy() to plot make the plot.)
  5. The norm of rk measures the overall error. What is the largest error in a single entry of your final approximation?
  6. Compute the condition numbers of A, B, and C.
  7. You should have found that the solution for A converges very quickly, B converges more slowly, and if you keep running for k larger than n, you may wonder if C will converge at all. Describe the correlation between the condition numbers and how difficult the systems are to solve.

2 April 15, 2009