# Convert symbolic inequality to matrix form

6 visualizzazioni (ultimi 30 giorni)
Cedric Kotitschke il 4 Lug 2023
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 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Nathan Hardenberg il 4 Lug 2023
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
eq1 =
[A, b] = equationsToMatrix(eq1, [x y])
A =
b =
Cedric Kotitschke il 4 Lug 2023
I would surely call that elegant, thanks!

Accedi per commentare.

### Risposte (2)

Gandham Heamanth il 4 Lug 2023
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 CommentiMostra NessunoNascondi Nessuno
Nathan Hardenberg il 4 Lug 2023
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])
coefficients =
% Extract the matrix A and vector b
A = [coefficients(1), coefficients(2)];
Index exceeds the number of array elements. Index must not exceed 1.

Error in indexing (line 936)
R_tilde = builtin('subsref',L_tilde,Idx);
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
Cedric Kotitschke il 4 Lug 2023
But this assumes a very specific structure of the inequality. I also want to be able to deal with those structurs:
syms x y
ineq1 = 2*x <= 2-y;
ineq2 = 0 >= 4*x + 6*y;
ineq3 = 3 >= 5*y;
etc.

Accedi per commentare.

Nathan Hardenberg il 4 Lug 2023
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
eq1 =
[A, b] = equationsToMatrix(eq1, [x y]) % use equationsToMatrix-function
A =
b =
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Code Performance in Help Center e File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by