Linear solving without fixing the input

4 views (last 30 days)
D D
D D on 7 Nov 2022
Answered: Divyam on 30 Oct 2024 at 5:06
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

Answers (1)

Divyam
Divyam on 30 Oct 2024 at 5:06
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

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!