Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Can you explain me the difference between lsq(A,b) and A\b? Why do I get a positif solution when I use lsq(A,b,1)? Where can I get the source code of lsq function?

Thank you.

share|improve this question

2 Answers 2

the difference between lsq(A,b) and A\b?

There should be no difference when $Ax=b$ has a unique solution. The differences arise when the system is underdetermined or inconsistent.

If $Ax=b$ has multiple solutions, A\b will find one, and call it good. lsq(A,b) will find the solution of smallest norm. For example, consider $x_1+x_2=1$:

  • [1 1]\[1] returns $x_1=1$, $x_2=0$
  • lsq([1,1],[1]) returns $x_1=0.5$, $x_2=0.5$

If there are no exact solutions, A\b proceeds to look for the least squares solution, and may return the same output as lsq(A,b). For example, try the inconsistent system $x=1$, $x=2$:

  • both [1;1]\[1;2] and lsq(1;1],[1;2]) return $x=1.5$.

However, the algorithm of A\b appears to be less optimized for the task of least squares solution. Consider the inconsistent system $$\begin{cases} x_1+x_2&=1 \\ x_1+x_2&=2 \end{cases}$$

  • [1 1;1 1]\[1;2] displays a warning message and returns $x_1=-\infty$, $x_2=\infty$.
  • lsq([1 1;1 1],[1;2]) returns $x_1=x_2=0.75$ with no drama.

Why do I get a positif solution when I use lsq(A,b,1)?

Depends on your $A$ and $b$. For example, lsq([1],[-1],1) returns $-1$, as it should.

Where can I get the source code of lsq function?

Scilab source code is available. Also, Scilab documentation says that the lsq function is based on the LApack functions dgelsy (for real matrices) and zgelsy (for complex matrices). LApack source is also available.

share|improve this answer

I though A\b is using the LU approach. This is my opinion, may be there are slight different in the result since A\b use direct method, while lsq is using approximation via least square

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.