Function handle in lsqr

9 views (last 30 days)
Frank on 22 Jul 2018
Answered: Frank on 24 Jul 2018
In the above page, there is code: function y = afun(x,transp_flag) if strcmp(transp_flag,'transp') % y = A'*x y = 4 * x; y(1:n-1) = y(1:n-1) - 2 * x(2:n); y(2:n) = y(2:n) - x(1:n-1); elseif strcmp(transp_flag,'notransp') % y = A*x y = 4 * x; y(2:n) = y(2:n) - 2 * x(1:n-1); y(1:n-1) = y(1:n-1) - x(2:n); end end
Could anybody please kindly tell me what is the purpose of strcmp(transp_flag,'transp')? Why there have to be two kinds of transp_flag?

Answers (2)

Steven Lord
Steven Lord on 22 Jul 2018
The first paragraph in the Description section on that documentation page states:
"You can specify A as a function handle, afun, such that afun(x,'notransp') returns A*x and afun(x,'transp') returns A'*x."
By writing a function you may be able to solve systems where explicitly creating and storing A would require a lot of time and/or memory, but A has a structure that allows you to compute its product with a vector more efficiently without needing to build A. As an extreme case, if A was eye(1e6) that would require a lot of memory to store, but A*x and A'*x are each just x.
Why does lsqr require both those products? See page 44 as well as step 3 of the algorithm on page 50 of this paper which is the second reference on the lsqr documentation page.

Frank on 24 Jul 2018
Thank you indeed for your very very specific answer to this question!


Find more on Desktop in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by