Problems using pcg

I am trying to use a predefined conjugate gradient in matlab to speed up the process. I use this iterative method because the backslash operator is too long. However, I am having some problems. E.g. I want to solve this system of linear equations given

Ax=B 

where A is a sparse positive definite matrix, and B is also a matrix. In matlab, I can do it simply

x= A\B

However, if I use the pcg function, then I will have to iterate over all columns of B and solve individual

x(:,i)=pcg(A,B(:,i))

This cycle will take longer than x = A \ B. If I consider only one column as b instead of matrix B, then pcg is faster than the backslash operator. However, if I look at the whole matrix B, then pcg is slower than the backslash operator. Thus, it makes no sense to use pcg.

Any suggestions guys?

When using the method suggested by Mattj, it shows the following error:

Error using iterapp (line 60)
user supplied function ==>
@(x)reshape(repmat(A*x,1,nb),[],1)
 failed with the following error:

 Inner matrix dimensions must agree.

Error in pcg (line 161)
r = b -
iterapp('mtimes',afun,atype,afcnstr,x,varargin{:});
+1
source share
2 answers

I think we need to see more data about your time tests and sizes / sections A and B and better understand why pcg is faster than mldivide. However, you can realize what you are after that,

 [ma,na]=size(A);
 [mb,nb]=size(B);
 afun=@(x)  reshape(A*reshape(x,na,[]),[],1);

 X=pcg(afun,B(:));

  X=reshape(X,na,nb);
0
source

, B, pcg , . , pcg.

. A * x = B (:, 1), B (:, i), , LU- A.

, , PCG B (:, i), . , , , PCG. - B (:, + 1) B (:, i). , B , . , PCG , , i- x (:, i) PCG . , PCG.

0

All Articles