Graphics Programs Reference
In-Depth Information
n = length(v);
Av = zeros(n,1);
Av(1) = 2*v(1) - v(2) + v(n);
Av(2:n-1) = -v(1:n-2) + 2*v(2:n-1) - v(3:n);
Av(n) = -v(n-1) + 2*v(n) + v(1);
The program shown belowutilizes the function conjGrad . The solutionvector x
is initialized to zero in the program, which also sets up the constant vector b .
% Example 2.18 (Conjugate gradient method)
n=20;
x = zeros(n,1);
b = zeros(n,1); b(n) = 1;
[x,numIter] = conjGrad(@fex2
_
18,x,b)
Running the program results in
x=
-4.5000
-4.0000
-3.5000
-3.0000
-2.5000
-2.0000
-1.5000
-1.0000
-0.5000
0
0.5000
1.0000
1.5000
2.0000
2.5000
3.0000
3.5000
4.0000
4.5000
5.0000
numIter =
10
Search WWH ::




Custom Search