# PCG and MINRES question

4 views (last 30 days)
Karl on 30 Sep 2011
Edited: Josh Meyer on 23 Sep 2019
Hi, I have a problem which PCG and minres seem to be built to handle, i.e. I have
A(x) = b,
Where A is a function I've written in a .m file (linear) and b is a known column vector (measurement). I am looking for a least squares solution for x, but I have a question. First, how does MATLAB know what the size of x should be? I know what size it should be in reality, but I don't see if there's a way to set it. My function will happily take any size of x and calculate a column vector, but there is a particular size I am looking for.
Second, none of these methods seem to converge (one spits out a bunch of NaNs, the other spits out a bunch of 0s). Does anyone know wat may be causing this problem or have any suggestions? Thanks very much

Walter Roberson on 30 Sep 2011

Josh Meyer on 23 Sep 2019
Edited: Josh Meyer on 23 Sep 2019
Since A*x = b, the sizes of the variables must be:
A * x = b
(M-by-N) * (N-by-1) = (M-by-1)
But since almost all of the iterative solvers only work on square coefficient matrices it is generally the case that N = M. The only solver where this can be ambiguous is LSQR since it calculates a least squares solution for rectangular coefficient matrices. In that case MATLAB can figure out the size of x if you provide an initial guess x0 or a preconditioner matrix M. Failing that, LSQR uses the fact that x must have length equal to that of A'*b.
As for convergence, if you're having trouble getting the iterative methods to converge the answer is usually to use a better preconditioner matrix (which can also be supplied as a function handle). However, if you're looking for a least squares solution to a rectangular system then the problem could be that you're using PCG and not LSQR.