Question: function x = ConjugateGradientMethod ( A , b , x 0 , tol, max _ iter ) % Conjugate Gradient Method to solve Ax =
function x ConjugateGradientMethodA b x tol, maxiter
Conjugate Gradient Method to solve Ax b
if nargin
tol e;
end
if nargin
maxiter lengthb;
end
Initial Setup
r b A x;
p r;
rsold r r;
x x;
Iterate
for k :maxiter
Ap A p;
alpha rsold p Ap;
x x alpha p;
r r alpha Ap;
rsnew r r;
if sqrtrsnew tol
break;
end
p r rsnew rsold p;
rsold rsnew;
end
end
My student ID is
studentid ;
lasttwodigits modstudentid;
Size of the problem
n lasttwodigits; For the first problem
n lasttwodigits; For the second problem
Create matrix A and vector b for the first problem
A diag onesn diagonesn diagonesn;
b zerosn;
b;
bend;
Create matrix A and vector b for the second problem
A diag onesn diagonesn diagonesn;
b zerosn;
b;
bend;
Initial guess
x zerosn;
x zerosn;
Solve using conjugate gradient method
x ConjugateGradientMethodA b x;
x ConjugateGradientMethodA b x;
Display solutions
dispSolution for the first problem n :;
dispx;
dispSolution for the second problem n :;
dispx;
correct the code to remove parse errors
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
