Convert symbolic inequality to matrix form
조회 수: 4 (최근 30일)
이전 댓글 표시
Let's say I have a symbolic inequality:
2*x + 3*y <= 5
Is there a way to elegantly extract the matrices A, b for the equivalent inequality A*[x;y] <= b?
I know the function equationsToMatrix but that only works for equalities.
Thanks!
댓글 수: 3
Nathan Hardenberg
2023년 7월 4일
My thought was the same as the one from @Torsten (I think). But I would not consider it an elegant way
syms x y
eq = 2*x + 3*y <= 5;
eq1 = lhs(eq) == rhs(eq) % convert to equality
[A, b] = equationsToMatrix(eq1, [x y])
답변 (2개)
Gandham Heamanth
2023년 7월 4일
Yes, in MATLAB, you can use the symbolic math toolbox to extract the matrices A and b from a symbolic inequality. Here's how you can do it:
syms x y;
% Define the symbolic inequality
inequality = 2*x + 3*y <= 5;
% Extract the coefficients of x and y
coefficients = coeffs(inequality, [x, y]);
% Extract the matrix A and vector b
A = [coefficients(1), coefficients(2)];
b = coefficients(3);
% Display the matrices A and b
disp('Matrix A:');
disp(A);
disp('Vector b:');
disp(b);
Note that this code assumes you have the Symbolic Math Toolbox installed in MATLAB.
댓글 수: 2
Nathan Hardenberg
2023년 7월 4일
The coeffs functions does not seem to work.
syms x y;
% Define the symbolic inequality
inequality = 2*x + 3*y <= 5;
% Extract the coefficients of x and y
coefficients = coeffs(inequality, [x, y])
% Extract the matrix A and vector b
A = [coefficients(1), coefficients(2)];
b = coefficients(3);
% Display the matrices A and b
disp('Matrix A:');
disp(A);
disp('Vector b:');
disp(b);
As an advice: you can past your code and mark it as code. Then you can run it in the browser and it is easier to read
Nathan Hardenberg
2023년 7월 4일
Moved my comment to an answer (to be accepted only if satisfied):
syms x y
eq = 2*x + 3*y <= 5; % inequality
eq1 = lhs(eq) == rhs(eq) % convert to equality
[A, b] = equationsToMatrix(eq1, [x y]) % use equationsToMatrix-function
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!