Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
lsqlin confusion

Subject: lsqlin confusion

From: John Smith

Date: 28 Jul, 2011 22:44:29

Message: 1 of 9

I am not understanding lsqlin's output. Here's what I have (omitting some):

  A = [A1c A2c A3c A4c A5c A6c
               -A1s -A2s -A3s -A4s -A5s -A6s
               A1moment A2moment A3moment A4moment A5moment A6moment]
           b = [-Fx; -Fy; -M]
           l = [0 0 0 0 0 0];
           u = [940 940 940 940 940 940];
          soltn = lsqlin(A,b,[],[],[],[],l,u)


And I get this output:

A =

     1 1 1 1 1 1
     0 0 0 0 0 0
    12 -12 -14 14 0 0


b =

  1.0e+025 *

    6.0548
         0
         0

Warning: Large-scale algorithm requires at least as many equations as variables
    in C matrix; using medium-scale algorithm instead.
> In lsqlin at 282
  In least_squares_2 at 129
Optimization terminated.

soltn =

   940
   940
   940
   940
   940
   940


Now, I purposely made the first element of "b" an outrageous number in hopes of getting lsqlin to fail-- I want to make sure I understand what's going on. Why is it claiming to find a solution? Now if try to confirm the results thus...

EDU>> A = [1 1 1 1 1 1; 0 0 0 0 0 0; 12 -12 -14 14 0 0]

A =

     1 1 1 1 1 1
     0 0 0 0 0 0
    12 -12 -14 14 0 0

EDU>> x = [940; 940; 940; 940; 940; 940]

x =

   940
   940
   940
   940
   940
   940


 
EDU>> A*x

ans =

        5640
           0
           0

EDU>>

What am I not getting?

Subject: lsqlin confusion

From: Bruno Luong

Date: 29 Jul, 2011 05:36:10

Message: 2 of 9

"John Smith" <johnps@gmail.com> wrote in message <j0sood$a5j$1@newscl01ah.mathworks.com>...
>
>
> What am I not getting?

Take a look at the first equation in this page:
http://www.mathworks.com/help/toolbox/optim/ug/lsqlin.html

This equation is what's lsqlin solves.

Bruno

Subject: lsqlin confusion

From: John Smith

Date: 1 Aug, 2011 17:04:11

Message: 3 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j0tgsa$1jt$1@newscl01ah.mathworks.com>...
> "John Smith" <johnps@gmail.com> wrote in message <j0sood$a5j$1@newscl01ah.mathworks.com>...
> >
> >
> > What am I not getting?
>
> Take a look at the first equation in this page:
> http://www.mathworks.com/help/toolbox/optim/ug/lsqlin.html
>
> This equation is what's lsqlin solves.
>
> Bruno

I am not a mathematician, so I have never seen least squares problems in that form. However, supposing that it does what it says it does, what is different about the way I formulate my problem using lsqlin as opposed to using \ ? Do I not present my vectors/matrices in exactly the same form? The only reason I'm trying to use lsqlin is that it allows constraints. Using simple sets of equations, I get exactly the same results whether I use lsqlin or \.

Subject: lsqlin confusion

From: John Smith

Date: 1 Aug, 2011 19:52:27

Message: 4 of 9

Again, the following example (with random numbers) illustrates that lsqlin and \ can be used similarly.

EDU>> A = [4 5 6 7; 3 8 7 4; 23 34 45 65]

A =

     4 5 6 7
     3 8 7 4
    23 34 45 65

EDU>> b = [32; 32; 32]

b =

    32
    32
    32

EDU>> A\b

ans =

   18.6846
    0.0716
         0
   -6.1566

EDU>> lsqlin(A,b,[],[])

ans =

   18.6846
    0.0716
         0
   -6.1566


EDU>> A*ans

ans =

   32.0000
   32.0000
   32.0000

EDU>>

So what could I be doing wrong such that lsqlin supposedly produces a solution, but when I test the solution (as I did with A*ans) it does not produce the correct result (in this case "b").

This is rather mystifying so any help would be appreciated. I can post another example of how it seems to fail if requested.

Subject: lsqlin confusion

From: Alan Weiss

Date: 2 Aug, 2011 18:21:42

Message: 5 of 9

On 8/1/2011 3:52 PM, John Smith wrote:
*SNIP*
> So what could I be doing wrong such that lsqlin supposedly produces a
> solution, but when I test the solution (as I did with A*ans) it does not
> produce the correct result (in this case "b").
>
> This is rather mystifying so any help would be appreciated. I can post
> another example of how it seems to fail if requested.

lsqlin produces a least-squares solution within the bounds you specify
(or other constraints such as linear equalities or inequalities).
Earlier in the thread you posted an example where you restricted the
range of possible x values by bounds. lsqlin produced the value (a
vector of 940s, if I recall correctly) that was the solution, meaning
the vector that gives the lowest value of your objective function among
all vectors within your bounds.

If you don't give any bounds or other constraints, lsqlin and backslash
are identical, as you discovered.

If you give constraints, the residual might not be zero, but lsqlin
gives the least-squares solution.

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: lsqlin confusion

From: John Smith

Date: 2 Aug, 2011 18:40:25

Message: 6 of 9

Alan Weiss <aweiss@mathworks.com> wrote in message <j19f7m$js4$1@newscl01ah.mathworks.com>...
> On 8/1/2011 3:52 PM, John Smith wrote:
> *SNIP*
> > So what could I be doing wrong such that lsqlin supposedly produces a
> > solution, but when I test the solution (as I did with A*ans) it does not
> > produce the correct result (in this case "b").
> >
> > This is rather mystifying so any help would be appreciated. I can post
> > another example of how it seems to fail if requested.
>
> lsqlin produces a least-squares solution within the bounds you specify
> (or other constraints such as linear equalities or inequalities).
> Earlier in the thread you posted an example where you restricted the
> range of possible x values by bounds. lsqlin produced the value (a
> vector of 940s, if I recall correctly) that was the solution, meaning
> the vector that gives the lowest value of your objective function among
> all vectors within your bounds.
>
> If you don't give any bounds or other constraints, lsqlin and backslash
> are identical, as you discovered.
>
> If you give constraints, the residual might not be zero, but lsqlin
> gives the least-squares solution.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

Aha, that's beginning to make sense. Is there a way to specify the max value of the residual so that if it goes beyond that number it returns the warning that a solution is unfeasible?

Subject: lsqlin confusion

From: Alan Weiss

Date: 2 Aug, 2011 19:06:48

Message: 7 of 9

On 8/2/2011 2:40 PM, John Smith wrote:
> Alan Weiss <aweiss@mathworks.com> wrote in message
> <j19f7m$js4$1@newscl01ah.mathworks.com>...
>> On 8/1/2011 3:52 PM, John Smith wrote:
>> *SNIP*
>> > So what could I be doing wrong such that lsqlin supposedly produces a
>> > solution, but when I test the solution (as I did with A*ans) it does
>> not
>> > produce the correct result (in this case "b").
>> >
>> > This is rather mystifying so any help would be appreciated. I can post
>> > another example of how it seems to fail if requested.
>>
>> lsqlin produces a least-squares solution within the bounds you specify
>> (or other constraints such as linear equalities or inequalities).
>> Earlier in the thread you posted an example where you restricted the
>> range of possible x values by bounds. lsqlin produced the value (a
>> vector of 940s, if I recall correctly) that was the solution, meaning
>> the vector that gives the lowest value of your objective function
>> among all vectors within your bounds.
>>
>> If you don't give any bounds or other constraints, lsqlin and
>> backslash are identical, as you discovered.
>>
>> If you give constraints, the residual might not be zero, but lsqlin
>> gives the least-squares solution.
>>
>> Alan Weiss
>> MATLAB mathematical toolbox documentation
>
> Aha, that's beginning to make sense. Is there a way to specify the max
> value of the residual so that if it goes beyond that number it returns
> the warning that a solution is unfeasible?

Take a look at the documentation:
http://www.mathworks.com/help/toolbox/optim/ug/lsqlin.html

Look at the second output of lsqlin: resnorm. I mean, when you call
lsqlin, call it this way:
[x,resnorm] = lsqlin(...)
resnorm is the squared 2-norm of the residual, meaning norm(C*x-d)^2.
I believe this gives the information you want.

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: lsqlin confusion

From: Matt J

Date: 2 Aug, 2011 19:08:08

Message: 8 of 9

"John Smith" <johnps@gmail.com> wrote in message <j19gap$ner$1@newscl01ah.mathworks.com>...
>
>
> Aha, that's beginning to make sense. Is there a way to specify the max value of the residual so that if it goes beyond that number it returns the warning that a solution is unfeasible?
==================

Only after the "unfeasible" solution has been found:

[x,resnorm]=lsqlin(...);

if resnorm>resnormMAX
 warning 'The solution found has an unacceptably large residual'
end

Subject: lsqlin confusion

From: John Smith

Date: 2 Aug, 2011 22:15:12

Message: 9 of 9

"Matt J" wrote in message <j19huo$sv0$1@newscl01ah.mathworks.com>...
> "John Smith" <johnps@gmail.com> wrote in message <j19gap$ner$1@newscl01ah.mathworks.com>...
> >
> >
> > Aha, that's beginning to make sense. Is there a way to specify the max value of the residual so that if it goes beyond that number it returns the warning that a solution is unfeasible?
> ==================
>
> Only after the "unfeasible" solution has been found:
>
> [x,resnorm]=lsqlin(...);
>
> if resnorm>resnormMAX
> warning 'The solution found has an unacceptably large residual'
> end

Great, thanks for the help!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us