Solve an overdetermined problem with lsqlin

5 Ansichten (letzte 30 Tage)
KAE
KAE am 2 Mai 2019
Bearbeitet: KAE am 3 Mai 2019
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?

Akzeptierte Antwort

John D'Errico
John D'Errico am 2 Mai 2019
Bearbeitet: John D'Errico am 2 Mai 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 Kommentar
KAE
KAE am 3 Mai 2019
Bearbeitet: KAE am 3 Mai 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'));

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by