Solve constrained linear least-squares problem such that A*x<=b
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everyone, could you please have a look at the following issue I am having?
I am trying to use lsqlin to calculate constrained x such that A*x<=b, where every A*x has got its own b. With some help I was able to reproduce following function, which does almost what I need:
x = lsqlin(C,d,[-C;C],[-d;0*d+bIsNumber],[],[],[],[],[],opts)
where bIsNumber is a number which is used for calculating every A*x<=b. But how would one alter the function above so that every A*x has got its own (different) b? I tried the following:
x = lsqlin(C,d,[-C;C],[-d;0*d+bIsVector],[],[],[],[],[],opts)
where bIsVector is a vector. But this function is not working.
What am I doing wrong please?
Any help is greatly appreciated
Best regards, LZ
0 commentaires
Réponse acceptée
Sargondjani
le 9 Avr 2012
i think you should put up the question in the newsgroup as well. hopefully some linear programming expert will respond...
there is hope, because i think your program is relatively easy... just linear equations! it's just that i dont know much about them
good luck!!
Plus de réponses (6)
Sargondjani
le 7 Avr 2012
i think you misunderstood the format
if A*x<=b looks like:
-x1 + 0*x2 <= b1 0*x1 + x2 <= b2
Then you have to insert it as: A=[-1,0 ; 0,1]; b=[b1;b2];
x = lsqlin([],[],A,b,[],[],[],[],[],opts)
if its not working, pls provide error message
Sargondjani
le 8 Avr 2012
you would have to rewrite all the equations as A*x<=b.
i will show you as if you only had x1 and x2:
25<=C*x1<30 => -C*x1<=-25 & C*x1<=30;
42<=C*x2<50 => -C*x2<=-42 & C*x2<=50;
so A and b are: A=[-C 0; C 0;... % for x1 0 -C; 0 C]; % for x2
b=[-25;30;-42;50];
as for the strict inequality: if that constraint is binding, and you really want strict it, then you might want to use 50-eps in stead of just 50 (eps is smallest number that matlab can handle, so 50-eps is the closest to 50 matlab goes)
I hope you understand...
2 commentaires
Sargondjani
le 8 Avr 2012
yes, like that... did you try it?? im not sure what you understand, and what you don't understand
do you get that the two inequalities are equivalent to the one which says 33<=C*x3<=40???
Sargondjani
le 8 Avr 2012
hmmm, it doesnt get me 'enters' so format is screwed:
A=[-C, 0; C, 0; 0, -C; 0, C];
3 commentaires
Sargondjani
le 8 Avr 2012
I think I understand where things go wrong!! I though C was a number, but i see now that it is a matrix... (i dont use lsqlin normally)
But then your constraints dont make sense, it think. Because your C is a matrix, so C*x is also a matrix... That can not be smaller than a number
So what really are your constraints?
Sargondjani
le 8 Avr 2012
o my god, i also didnt read carefull enough. i assumed you wanted to use lsqlin what it is supposed to do: find the best fit for a system of linear equation. but now i understand you just want to find the bounderiers in which the x-values can be, such that the system still solves... lsqlin is not well suited for that, i suppose, because your problem has infinitely many solutions...
and im so sorry, but im not familiar with this type of problem, so you should get some advice from someone who is more familiar with linear programming.
i suppose you can solve the two systems (lower and upper bounderaries) with gaussian elemination and see what the constraints are based on those results. and there is probably a way to solve this with the symbolic solver. i recommend you look in the documentation for that solver first (maybe search symbolic solver linear equations/programming)
good luck man!!
Sargondjani
le 8 Avr 2012
yes lsqlin gives you the best fit within boundaries, but you are not fitting. you are looking for all possible solutions that satisfy your inequalities (which lsqlin can not do)
anyway, i have looked around abit and matlab can do gaussian elimination for you (at least numerically). gaussian elimination reduces your system of equations to something simpler. check out the function "rref" http://www.mathworks.nl/help/techdoc/ref/rref.html
so try running this for both your systems (one with the larger than and one for the smaller than system). it is possible that this will actually give you the solution...
ps. yes i was a teacher, haha, and i want to start teaching again :-)
Sargondjani
le 8 Avr 2012
i think i understand your problem. you wrote earlier "So every result of C*x should be in the range of 2 numbers (bottom and top)."
this lsqlin can not do. or at least not 1 one go. what you could do is run lsqlin for the 'lower constraint' and then separately for the 'higher constraint'. this will give you the x values that best match those constraints. but i seriously doubt if there is only one best for either set solution. if any x_i can be in a range of values, i think lsqlin will have a problem solving it.
i would still use rref myself. the rref actually does return x values. do you know what gaussian elimination is? otherwise read about it... if your 2 systems of equations has a solution, or a range of solutions you can surely find it with gaussian elemination
in the example in the documentation on rref, the solutions are: x1=1, x2=3,x3=-3,x4=indeterminate (can take any value), thats how you have to 'read' the reduced echelon form of the matrix...
just try it, ok?
if not, can you post your C matrix, and your vectors with lower bounds and higher bounds nicely? as you put them in matlab with the '[ ]' etc. then i can try
Voir également
Catégories
En savoir plus sur Linear Least Squares dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!