[37] | 1 | function assign(X,value,ls) |
---|
| 2 | %ASSIGN Assigns a numerical value to an sdpvar |
---|
| 3 | % |
---|
| 4 | % ASSIGN(X,value) Tries to set the free variables in X so that |
---|
| 5 | % double(X)=value. Notice that other variables |
---|
| 6 | % sharing the same free variables will be affected. |
---|
| 7 | % If the assignment is infeasible, an error message |
---|
| 8 | % will be issued. |
---|
| 9 | % |
---|
| 10 | % ASSIGN(X,value,1) Least square assignment. |
---|
| 11 | |
---|
| 12 | % Author Johan Löfberg |
---|
| 13 | % $Id: assign.m,v 1.4 2006/08/09 13:47:43 joloef Exp $ |
---|
| 14 | |
---|
| 15 | if nargin<3 |
---|
| 16 | ls = 0; |
---|
| 17 | end |
---|
| 18 | |
---|
| 19 | if ~isa(X,'sdpvar') |
---|
| 20 | error('First argument should be an SDPVAR object.'); |
---|
| 21 | end |
---|
| 22 | |
---|
| 23 | if ~isa(value,'double') |
---|
| 24 | error('Second argument should be a DOUBLE.'); |
---|
| 25 | end |
---|
| 26 | |
---|
| 27 | if prod(size(value)) == 1 |
---|
| 28 | value = repmat(value,size(X)); |
---|
| 29 | end |
---|
| 30 | |
---|
| 31 | if ~isequal(size(X),size(value)) |
---|
| 32 | error('Both arguments must have same size') |
---|
| 33 | end |
---|
| 34 | if ~isa(X,'sdpvar') |
---|
| 35 | error('First arguments must be an sdpvar object') |
---|
| 36 | end |
---|
| 37 | |
---|
| 38 | if is(X,'complex') |
---|
| 39 | assign(real(X),real(value)); |
---|
| 40 | assign(imag(X),imag(value)); |
---|
| 41 | return |
---|
| 42 | end |
---|
| 43 | |
---|
| 44 | x_lmi_variables = X.lmi_variables; |
---|
| 45 | b = value(:)-X.basis(:,1); |
---|
| 46 | A = X.basis(:,2:end); |
---|
| 47 | feas_var = A\b; |
---|
| 48 | % Improve |
---|
| 49 | e = A*feas_var-b; |
---|
| 50 | de = A\e; |
---|
| 51 | feas_var = feas_var-de; |
---|
| 52 | |
---|
| 53 | if ~ls |
---|
| 54 | if norm(A*feas_var-b)>sqrt(eps) |
---|
| 55 | error('Inconsistent assignment') |
---|
| 56 | end |
---|
| 57 | end |
---|
| 58 | |
---|
| 59 | sol = yalmip('getsolution'); |
---|
| 60 | keep_these = find(~ismember(sol.variables,x_lmi_variables)); |
---|
| 61 | sol.optvar = [sol.optvar(keep_these);feas_var(:)]; |
---|
| 62 | sol.variables = [sol.variables(keep_these);x_lmi_variables(:)]; |
---|
| 63 | yalmip('setallsolution',sol); |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | |
---|