Function handle in lsqr

3 vues (au cours des 30 derniers jours)
Frank
Frank le 22 Juil 2018
Réponse apportée : Frank le 24 Juil 2018
https://www.mathworks.com/help/matlab/ref/lsqr.html
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?

Réponse acceptée

Steven Lord
Steven Lord le 22 Juil 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.

Plus de réponses (1)

Frank
Frank le 24 Juil 2018
Thank you indeed for your very very specific answer to this question!

Catégories

En savoir plus sur Oceanography and Hydrology dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by