Solve an overdetermined problem with lsqlin

4 views (last 30 days)
I am having trouble formatting the inputs to lsqlin. I would like to solve for x in Cx=d, where C is 100 by 5 and d is 100 by 10 so x has to be 5 by 10. I would like to impose the constraint that d>0 and d<15. Here is my example.
C = rand(10,5); d = rand(10,15); % Create example inputs
% Skip inputs that don't seem to be required for the problem
A = []; b = [];
Aeq = []; beq = [];
% Set up the lower and upper bound constraints
[rowC,colC] = size(C);
[rowD,colD] = size(d);
dum = zeros(colC,colD); % Placeholder matrix which is the same size as x
lowerBound = 0*dum; % Lower bound on all values of x is zero
upperBound = 100 + dum; % Upper bound
x = lsqlin(C, d, A, b, Aeq, beq, lowerBound, upperBound);
I get the following error,
Error using lsqlin (line 226)
The number of rows in C must be equal to the length of d.
Matlab defines the length of a matrix to be the larger of its dimensions, so here the length of b is 15. This error occurs even for the simpler problem
x = lsqlin(C, d);
I am missing something basic in the setup here. What is it? If I should use a nonlinear solver instead like lsqnonlin, what is the input function?

Accepted Answer

John D'Errico
John D'Errico on 2 May 2019
Edited: John D'Errico on 2 May 2019
lsqlin is not written to allow multiple right hand side vectors. But each column of d is just a separate, distinct, independent problem. So just use a loop. WTP?
  1 Comment
KAE
KAE on 3 May 2019
Edited: KAE on 3 May 2019
Just to show John's suggested implementation, for others learning like me. I thought it was interesting that so many of the solutions ended up being the max or min permitted values.
C = rand(1000,100); d = rand(1000,75); % Create example inputs
% Skip inputs that don't seem to be required for the problem
A = []; b = [];
Aeq = []; beq = [];
% Set up the lower and upper bound constraints
[rowC,colC] = size(C);
[rowD,colD] = size(d);
dum = zeros(colC,1); % Placeholder matrix which is the same size as x
lowerBound = 0 + dum; % Lower bound on all values of x
upperBound = 0.05 + dum; % Upper bound (choose one which will affect results)
for iCol = 1:colD % Loop through each column of d, which is independent
x(:,iCol) = lsqlin(C, d(:,iCol), A, b, Aeq, beq, lowerBound, upperBound);
end
figure;
ax1 = subplot(211)
hist(x(:), 100);
hold on;
plot([1 1]*lowerBound(1), get(gca,'ylim')); % Lower bound
plot([1 1]*upperBound(1), get(gca,'ylim')); % Upper bound
box off;
title('Histogram of x Solutions with Constraint')
% How does this differ from an unconstrained solution?
lowerBound = -Inf + dum; % No lower bound on x
upperBound = Inf + dum; % No upper bound
for iCol = 1:colD % Loop through each column of d, which is independent
x(:,iCol) = lsqlin(C, d(:,iCol), A, b, Aeq, beq, lowerBound, upperBound);
end
ax2 = subplot(212)
hist(x(:), 100);
box off;
title('Histogram of x Solutions without Constraint')
set(ax1, 'xlim', get(ax2, 'xlim'));

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!