Linear solving without fixing the input

조회 수: 1 (최근 30일)
D D
D D 2022년 11월 7일
답변: Divyam 2024년 10월 30일
Inputs - a, b
Output - d,c (where d equals c) AND x,y where dx+cy = 0
In general, I have 2 random inputs. I need the outputs, x and y, where my inputs becomes equal.
Is it possible to add constraints to inuts also, when using solvers like lsqlin

답변 (1개)

Divyam
Divyam 2024년 10월 30일
Hi @D D,
In essence, I believe you are trying to solve the equation, where . The use of "lsqlin" does not seem appropriate for this problem.
It's trivial to see that the equation simplifies to if . As a result of this decomposition, you can choose any value of x and then get the corresponding value for y.
The simplest way to add input constraints to your code is by introducing an error statement that gets triggered whenever . That being said, there is no specific setting to add input constraints to "lsqlin".
if a ~= b
error('a and b must be equal');
end
If you wish to solve this problem using the "lsqlin" function, the following setup can be used to experiment:
% Define the inputs
a = input('Enter the value for a: ');
b = input('Enter the value for b: ');
% Ensure a = b
if a ~= b
error('a and b must be equal');
end
% Coefficient matrix C and target vector d
C = [a, a];
d = 0;
% Initial guess (optional)
x0 = [];
% Lower and upper bounds for x and y
lb = [-10; -10];
ub = [10; 10];
% Linear inequality and equality constraints (optional)
% Ax <= b
A = [];
b_ineq = [];
Aeq = [];
beq = [];
% Solve using lsqlin
x = lsqlin(C, d, A, b_ineq, Aeq, beq, lb, ub, x0);
% Display the solution
disp('Solution for [x; y]:');
disp(x);
As an alternative, you can also use "linsolve" to solve this system of two linear equations: https://www.mathworks.com/help/symbolic/solve-a-system-of-linear-equations.html

카테고리

Help CenterFile Exchange에서 Linear Least Squares에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by