Solve linear equation in matrix form with least square method

조회 수: 9 (최근 30일)
DoinK
DoinK 2023년 5월 26일
편집: DoinK 2023년 5월 28일
I have linear equation Ax=b, the size of every matrix are : matrix A is 1009*1008, vector b is 1009*1, and vector x is 1008*1.
If I use lsqr command which is lsqr(A,b), this come out
"Error using iterchk (line 38)
Argument must be a floating point matrix or a function handle.
Error in lsqr (line 93)
[atype,afun,afcnstr] = iterchk(A);".
If I try simple matrix, it can calculate easily.
If I use (A.'*A)\(A.'*b), i can get the result, but why this code lsqr(A,b) cannot
I want to know what is wrong and how to solve it.
Actually i have 1009 linear equation and 1008 variable, so i use this method, or is there any method to use to solve this problem?

채택된 답변

Matt J
Matt J 2023년 5월 26일
편집: Matt J 2023년 5월 26일
If I use (A.'*A)\(A.'*b), i can get the result
An even easier and more accurate way to get it is,
x=A\b
and that is the method you should be using for matrix data of this size. As to why you get the error, perhaps your A matrix is not type double, but some other type? E.g.,
A=randi(3,3,'int8');
b=A(:,1);
lsqr(double(A),double(b))
lsqr converged at iteration 3 to a solution with relative residual 2.9e-13.
ans = 3×1
1.0000 -0.0000 -0.0000
lsqr(A,b)
Error using iterchk
Argument must be a floating point matrix or a function handle.

Error in lsqr (line 93)
[atype,afun] = iterchk(A);
  댓글 수: 12
Walter Roberson
Walter Roberson 2023년 5월 27일
The least squares solvers like A\b and lsqrt work with numbers, not symbolic variables.
syms A [3 3]
syms b [3 1]
A\b
ans = 
However, with a system of rank 1008, unless it is pretty much a diagonal matrix, the time required to solve might be enormous.
DoinK
DoinK 2023년 5월 28일
편집: DoinK 2023년 5월 28일
Finally I understood all and was able to solve my matrix.
Thank you so much Sir @Matt J @Steven Lord @Walter Roberson

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by