matlabwolfram-mathematicaequations

Eliminate variables from equations in MATLAB


I want to eliminate variables from an equation using MATLAB. For example, lets consider the following equations:

p = (m + n)
q = (m - n)
r = (m^3 - n^3)

Now, r could be expressed in terms of p and q by entirely eliminating m and n like this: r = (3*p^2*q + q^3)/4.

This can be achieved in Mathematica using the following method:

Eliminate[{p == (m + n), q == (m - n), r == (m^3 - n^3)}, {m, n}]

How can I get the same result in MATLAB if it is possible at all. Switching between different applications just for this is very inconvenient.


Solution

  • % Declare symbolic variables
    syms m n p q
    
    % Solve m,n 
    s1=solve(m+n-p==0,m-n-q==0,m,n);
    
    % Substitute variables with obtained solution
    r = (m^3 - n^3);
    r2=subs(subs(r,m,s.m),n,s.n);
    
    % simplify answer
    r3=simplify(r2)