iKnowNothing
iKnowNothing

Reputation: 175

Express an expression in terms of other expressions in MATLAB

Is there any way to express an expression in terms of other expressions in MATLAB?

For example, the following expressions have been written as sum (X + Y) and product (XY)

1/X + 1/Y = (X + Y)/XY

1/X^2 + 1/Y^2 + 2/(XY) = (X + Y)^2/(XY)

2*X/Y + 2*Y/X = 2*((X + Y)^2 - 2*X*Y)/(XY)

I know about the rewrite() function but I couldn't find how it can be used to do what I want?

Upvotes: 0

Views: 122

Answers (1)

gnovice
gnovice

Reputation: 125874

There are a few different functions you can try to change the format of your symbolic expression:

  • collect: collects coefficients (can specify an expression to collect powers of):

    >> collect(1/X + 1/Y)
    
    ans =
    
    (X + Y)/(Y*X)
    
  • simplify: perform algebraic simplification:

    >> simplify(1/X^2 + 1/Y^2 + 2/(X*Y))
    
    ans =
    
    (X + Y)^2/(X^2*Y^2)
    
  • numden: convert to a rational form, with a numerator and denominator:

    >> [n, d] = numden(2*X/Y + 2*Y/X)
    
    n =
    
    2*X^2 + 2*Y^2
    
    d =
    
    X*Y
    
    >> n/d
    
    ans =
    
    (2*X^2 + 2*Y^2)/(X*Y)
    

Upvotes: 1

Related Questions