Solve linear equation in matrix form with least square method
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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?
채택된 답변
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.
Argument must be a floating point matrix or a function handle.
Error in lsqr (line 93)
[atype,afun] = iterchk(A);
댓글 수: 12
Matrix A item is a very small decimal, so i round them like this before i use any method to find x
A=round(A,10)
I just search how to find the type of A
isa(A,'double')
turns out
logical
0
that means A is not double.
Does round command effect A?
Long before i use round command, i already use vpa to cut off some decimal.
that means A is not double.
And that means your problem has been identified and solved. Please Accept-click the answer if you agree.
Does round command effect A?
round() can affect the result because it changes the input data. However, it does not cause an error, as you can see yourself from a simple test,
A=rand(3); b=A(:,1);
lsqr(round(A,10),round(b,10))
lsqr converged at iteration 3 to a solution with relative residual 2.8e-12.
ans = 3×1
1.0000
0.0000
0.0000
What do the following commands show before and after you call round on A?
whos A
which -all round
I dont know what is wrong, but if I use
A\b
the result is
"Warning: Solution does not exist because the system is inconsistent.
> In symengine
In sym/privBinaryOp (line 1050)
In \ (line 399) "
it shows
whos A
which -all round
Name Size Bytes Class Attributes
A 1009x1008 8 sym
lsqr is not defined on symbolic variables.
What shows up for
rank(A)
Is there a particular reason you are using a symbolic matrix? Does your matrix contain symbolic expressions such as 3*x+5*y as terms? Or do you need precision greater than is available in double precision? Or are you expecting double precision values there but you calculated them by symbolic means, such as if you add a calculation with int() and subs() to get what you expected to be numeric values?
First I have row vector 1*1008, which is all symbol (q1 q2 ...) and matrix F (1008*1008). Then i need to multiply them, i got 1008 linear equation that all equals to zero. After that i have 1 more equation add to them so there are 1009 equation with 1008 variable
there the symbol came from
After that, i use equationtoMatrix to get matrix A and b. And then need to find 1008 value of x with Ax=b. My steps are wrong?
rank(A) is 1008 Sir.
It doesn't really make sense to be using symbolic math when you have 1000's of variables. However, you can try converting your matrices to doubles,
A=double(A);
b=double(b);
assuming the coefficients of A are all literal numbers.
I agree, double() the results of equationToMatrix() .
... or simply build it all up numerically in the first place.
May i know is there any method not to use row vector 1*1008, which is all symbol (q1 q2 ...)?
Like using command that have row vector and can be multiply with matrix.
The least squares solvers like A\b and lsqrt work with numbers, not symbolic variables. You should follow the advice that has been given to you. Plug numbers into the symbols and convert to double.
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.
Finally I understood all and was able to solve my matrix.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
