grouping an equation in quadratic/squared terms

11 visualizaciones (últimos 30 días)
Berkin Birol el 27 de Feb. de 2022
Comentada: Berkin Birol el 25 de Oct. de 2023
Hi all,
I would like MATLAB to group my equation
(-a-b)*x1^2 + 0.2*b*x1*x2 - 0.2*c*x2^2
as all quadratic items like below:
-(b*x1-0.1*x2)^2 - (a+b-b^2)*x1^2 - (0.2*c-0.01)*x2^2
I had found a method once from Matlab answers but I can not remember it. This equation is OK but I would like to use MATLAB to make this arrangement (if it's algebraically possible) if my equation is updated in the future.
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Respuestas (1)

T.Nikhil kumar el 20 de Oct. de 2023
Hello Berkin,
As per my understanding, you want to group all the items in your equation as quadratic or squared terms.
I recommend you leverage the symbolic variables and the symbolic algebra capabilities of MATLAB for this purpose. You can first create ‘x1’, ‘x2’,’a’ and ‘b’ as symbolic scalar variables of type sym’. Then, you can define your equation in a separate variable after which you can use the collect function to group the quadratic terms (x1^2 and x2^2) and squared terms. Refer to the following code snippet for an overview of the method.
%Creating the symbolic variables
syms x1 x2 a b c
%Defining the equation
equation = (-a-b)*x1^2 + 0.2*b*x1*x2 - 0.2*c*x2^2;
% Grouping the quadratic terms (This grouped_equation variable contains the
% new equation)
grouped_equation = collect(equation, [x1^2, x2^2]);
By using symbolic variables, you can easily update your equation in the future and MATLAB will automatically group the quadratic terms for you.
Refer to the following documentation to understand more about ‘syms’ and ‘collect’ functions.
Hope this helps!
1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Berkin Birol el 25 de Oct. de 2023
Hello Nikhil,
Thank you for your answer. I just tried your suggestion, unfortunately, this is not what I was looking for. collect(equation, [x1^2, x2^2]) returns "(- a - b)*x1^2 + (b*x2*x1)/5 - (c*x2^2)/5", which is different what I asked: "-(b*x1-0.1*x2)^2 - (a+b-b^2)*x1^2 - (0.2*c-0.01)*x2^2"
I don't want to just group squared x1 or x2's. I want to group the equation with just squared terms, like I highlighted in bold in the previous paragraph; square of (b*x1-0.1*x2), square of x1 with some coefficient (a+b-b^2), and similar to x1, square of x2 with some coefficient (0.2*c-0.01). These should be returns of the function, separated by all squared/quadratic terms (with their coefficients).
By using this method, I could easily conclude if the equation is positive or not, depending on the variables. Or determine the positiveness conditions.

Iniciar sesión para comentar.

Categorías

Más información sobre Common Operations en Help Center y File Exchange.

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by