lsqnonneg

Can I solve [x] in the relation [C]*[x]=[d] using x= lsqnonneg(C,d);? Vector dimensions are as follows C(5x2), d(5x1). If not please suggest me better way.
Thank you,

 Accepted Answer

Andrew Newell
Andrew Newell on 10 Jan 2012
x = C\d;
EDIT: The least squares estimate for x is
xhat = (C'*C)\(C'*d);
You can estimate the error in the fit as follows:
n = length(d);
s2 = norm(x-xhat)^2/(n-2); %estimated variance of the data
covar = s2*inv(C'*C); %covariance matrix for the components of xhat
xerr = sqrt(diag(covar)) %standard errors for xhat components

8 Comments

Abex
Abex on 10 Jan 2012
Dear Andriw,
Thank you for your suggestion.
How can i use matrix division as the C and d posses different size? I tried it using x=inv(C)*d but C is not a square matrix and has no inverse.
Would you please have a say on this?
Titus Edelhofer
Titus Edelhofer on 10 Jan 2012
The backslash is solving the linear system of equations you wrote, even for overdetermined system (e.g. C has more rows then columns). It does this in the least squares sense (because there (usually) is no exact solution).
Andrew Newell
Andrew Newell on 10 Jan 2012
MATLAB does not take the inverse of C to solve this equation. In effect, you have two equations for the components of x, and it solves them using Gaussian elimination.
Titus Edelhofer
Titus Edelhofer on 10 Jan 2012
If the matrix C is 5x2 you have 5 equations for 2 unknowns, that is an overdetermined system ...
Abex
Abex on 10 Jan 2012
Dear Andrew,
Thank you for elaborating, it is very helpful.I am interested in posetive value of x and that is why I used lsqnonneg(C,d). Why is posetive value of C\d and lsqnonneg(C,d) show different result for same C and d?
thank you
Andrew Newell
Andrew Newell on 10 Jan 2012
Can you give an example?
Abex
Abex on 11 Jan 2012
Dear Andrew,thank you
Here is my full data
C =
0.6065 0.6065
0.6065 0.6065
0.6065 0.6065
0.6065 0.6065
0.6065 0.6065
0.6065 0.6065
0.6065 0.6065
0.6065 0.6065
0.6065 0.6065
0.6064 0.6064
0.6002 0.6003
0.5648 0.5649
0.5705 0.5705
0.5783 0.5783
0.5786 0.5786
0.5860 0.5859
0.5925 0.5925
0.5962 0.5961
0.5962 0.5961
0.5967 0.5965
0.6015 0.6014
0.6000 0.5998
0.5994 0.5993
0.6016 0.6015
0.6034 0.6034
0.6046 0.6045
0.6031 0.6030
0.6049 0.6049
0.6036 0.6035
0.6050 0.6049
0.6047 0.6046
0.6044 0.6044
0.6041 0.6040
0.6043 0.6042
0.6043 0.6043
0.6044 0.6043
0.6047 0.6047
0.6046 0.6045
0.6047 0.6046
0.6041 0.6040
0.6029 0.6028
0.6027 0.6026
0.6032 0.6031
0.6040 0.6039
0.6024 0.6022
0.6015 0.6013
0.6024 0.6022
0.6038 0.6037
0.6040 0.6039
0.6049 0.6048
0.6044 0.6043
0.6043 0.6042
0.6028 0.6026
0.6026 0.6024
0.6047 0.6046
0.6043 0.6042
0.6053 0.6052
0.6042 0.6041
0.6046 0.6045
0.6034 0.6033
0.6054 0.6053
0.6055 0.6054
0.6038 0.6037
0.6045 0.6043
0.6052 0.6051
0.6059 0.6058
0.6056 0.6056
0.6053 0.6052
0.6054 0.6053
0.6058 0.6057
0.6062 0.6062
0.6063 0.6062
0.6064 0.6064
0.6064 0.6064
0.6065 0.6065
d =
0.2500
0.2600
0.2700
0.2800
0.2900
0.3000
0.3100
0.3200
0.3300
0.3400
0.3500
0.3600
0.3700
0.3800
0.3900
0.4000
0.4100
0.4200
0.4300
0.4400
0.4500
0.4600
0.4700
0.4800
0.4900
0.5000
0.5100
0.5200
0.5300
0.5400
0.5500
0.5600
0.5700
0.5800
0.5900
0.6000
0.6100
0.6200
0.6300
0.6400
0.6500
0.6600
0.6700
0.6800
0.6900
0.7000
0.7100
0.7200
0.7300
0.7400
0.7500
0.7600
0.7700
0.7800
0.7900
0.8000
0.8100
0.8200
0.8300
0.8400
0.8500
0.8600
0.8700
0.8800
0.8900
0.9000
0.9100
0.9200
0.9300
0.9400
0.9500
0.9600
0.9700
0.9800
0.9900
>> C\d
ans =
1.0e+003 *
1.7095
-1.7087
>> lsqnonneg(C,d)
ans =
1.0313
0
My expectation for the value of x was 0 to 1.
Andrew Newell
Andrew Newell on 11 Jan 2012
Your matrix C is very ill-conditioned because the two rows are so nearly equal. The estimated errors for xhat, using the equations I have added above, are
1.0e+05 *
3.4387
3.4391
i.e., much larger than the components themselves. In other words, it is futile to try to fit such a data set.

Sign in to comment.

More Answers (1)

Titus Edelhofer
Titus Edelhofer on 11 Jan 2012
Hi,
if you take a closer look at your data you observe:
1. the two columns of C are very similar, therefore it's no surprise that using lsqnonneg you get one positive value and a zero.
2. If you do the following:
x = C(:,1)./d
you get values 2.4, 2.3, 2.2, 2.1, ..., 0.62, 0.61. The quadratic mean will be about the value you get from lsqnonneg and (from the data) it's not clear why the value should be less then one.
Titus

1 Comment

Abex
Abex on 12 Jan 2012
the value of d is normalised mass which varies from 0 to 1 and C is obtained from other algorithm

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!