필터 지우기
필터 지우기

Solving a least squares problem using QR decomposition

조회 수: 8 (최근 30일)
Pascale Bou Chahine
Pascale Bou Chahine 2020년 11월 15일
편집: Image Analyst 2020년 11월 15일
"Given the least squares problem ||b - Ax||_2 = min_{y in R^n} ||b - Ax||_2 where A in R^{mxn} and b in R^m are given, and A is full-rank.
Write a MATLAB algorithm that solves the problem using the Matlab’s built-in QR Decomposition (qr) assuming m => n. "
Here's my code, but whenever I run it, I get an error in line 3: "Too many output arguments". What is the problem?
function[x] = ded(A,b)
[m,n] = size(A);
[Q, R, P] = qr(A);
c = Q'*b;
tol = max(size(A))*eps*abs(R(1,1));
r = 1;
while ( abs(R(r+1,r+1)) >= tol && r < n ); r = r+1; end
y1 = R(1:r,1:r) \ c(1:r);
y2 = zeros(n-r,1);
x = P*[y1;y2]

답변 (1개)

Image Analyst
Image Analyst 2020년 11월 15일
How are you calling ded()? What are you passing in for A and b? And according to the error you're expecting more than one output, so you're calling it like
[x, x2, x3] = ded(A,b)
with 2 or more outputs when ded returns only one output. Try with only one output:
x = ded(A,b)
Also, in
function[x] = ded(A,b)
you must have a space after the function key word and before the left bracket.
  댓글 수: 2
Pascale Bou Chahine
Pascale Bou Chahine 2020년 11월 15일
I'm taking A - rand(3,2), and b = rand(3,1)
I still don't know why I'm getting this error, can you edit my code to see where I went wrong please?
Image Analyst
Image Analyst 2020년 11월 15일
편집: Image Analyst 2020년 11월 15일
OK, so that's how you're defining A and b, but what about my first, and most important question? Again:
"How are you calling ded()? "
You have not supplied us with your code, so we can't edit anything yet. I don't see anywhere in your posted code where you said you're calling it something like
[x, x2, x3] = ded(A,b)
Why not? Why won't you tell us how you're calling this function? Just to be super explicit, that will be on line 3 of your main script (not this function). Please attach the main script m-file, and the ded function m-file with the paper clip icon.

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

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by