Simplify Symbolic Expressions - MATLAB & Simulink (2024)

Simplify Symbolic Expressions

Simplification of a mathematical expression is not a clearlydefined subject. There is no universal idea as to which form of anexpression is simplest. The form of a mathematical expression thatis simplest for one problem turns out to be complicated or even unsuitablefor another problem. For example, the following two mathematical expressionspresent the same polynomial in different forms:

(x + 1)(x - 2)(x + 3)(x - 4),

x4 -2x3 - 13x2 +14x + 24.

The first form clearly shows the roots of this polynomial. Thisform is simpler for working with the roots. The second form servesbest when you want to see the coefficients of the polynomial. Forexample, this form is convenient when you differentiate or integratepolynomials.

If the problem you want to solve requires a particular formof an expression, the best approach is to choose the appropriate simplificationfunction. See Choose Function to Rearrange Expression.

Besides specific simplifiers, Symbolic Math Toolbox™ offersa general simplifier, simplify.

If you do not need a particular form of expressions (expanded,factored, or expressed in particular terms), use simplify toshorten mathematical expressions. For example, use this simplifierto find a shorter form for a final result of your computations.

simplify works on various types of symbolicexpressions, such as polynomials, expressions with trigonometric,logarithmic, and special functions. For example, simplify these polynomials.

syms x ysimplify((1 - x^2)/(1 - x))simplify((x - 1)*(x + 1)*(x^2 + x + 1)*(x^2 + 1)*(x^2 - x + 1)*(x^4 - x^2 + 1))
ans =x + 1 ans =x^12 - 1

Simplify expressions involving trigonometric functions.

simplify(cos(x)^(-2) - tan(x)^2)simplify(cos(x)^2 - sin(x)^2)
ans =1 ans =cos(2*x)

Simplify expressions involving exponents and logarithms. Inthe third expression, use log(sym(3)) instead of log(3).If you use log(3), then MATLAB® calculates log(3) withthe double precision, and then converts the result to a symbolic number.

simplify(exp(x)*exp(y))simplify(exp(x) - exp(x/2)^2)simplify(log(x) + log(sym(3)) - log(3*x) + (exp(x) - 1)/(exp(x/2) + 1))
ans =exp(x + y) ans =0 ans =exp(x/2) - 1

Simplify expressions involving special functions.

simplify(gamma(x + 1) - x*gamma(x))simplify(besselj(2, x) + besselj(0, x))
ans =0 ans =(2*besselj(1, x))/x

You also can simplify symbolic functions by using simplify.

syms f(x,y)f(x,y) = exp(x)*exp(y)f = simplify(f)

Simplify Using Options

By default, simplify uses strict simplification rules and ensures that simplified expressions are always mathematically equivalent to initial expressions. For example, it does not combine logarithms for complex values in general.

syms xsimplify(log(x^2) + log(x))
ans =log(x^2) + log(x)

You can apply additional simplification rules which are notcorrect for all values of parameters and all cases, but using which simplify canreturn shorter results. For this approach, use IgnoreAnalyticConstraints.For example, simplifying the same expression withIgnoreAnalyticConstraints,you get the result with combined logarithms.

simplify(log(x^2) + log(x),'IgnoreAnalyticConstraints',true)
ans =3*log(x)

IgnoreAnalyticConstraints provides a shortcutallowing you to simplify expressions under commonly used assumptionsabout values of the variables. Alternatively, you can set appropriateassumptions on variables explicitly. For example, combining logarithmsis not valid for complex values in general. If you assume that x isa real value, simplify combines logarithms without IgnoreAnalyticConstraints.

assume(x,'real')simplify(log(x^2) + log(x))
ans =log(x^3)

For further computations, clear the assumption on x by recreating it using syms.

syms x

Another approach that can improve simplification of an expressionor function is the syntax simplify(f,'Steps',n),where n is a positive integer that controls howmany steps simplify takes. Specifying more simplificationsteps can help you simplify the expression better, but it takes moretime. By default, n = 1. For example, create andsimplify this expression. The result is shorter than the originalexpression, but it can be simplified further.

syms xy = (cos(x)^2 - sin(x)^2)*sin(2*x)*(exp(2*x) - 2*exp(x) + 1)/... ((cos(2*x)^2 - sin(2*x)^2)*(exp(2*x) - 1));simplify(y)
ans =(sin(4*x)*(exp(x) - 1))/(2*cos(4*x)*(exp(x) + 1))

Specify the number of simplification steps for the same expression.First, use 25 steps.

simplify(y,'Steps',25)
ans =(tan(4*x)*(exp(x) - 1))/(2*(exp(x) + 1))

Use 50 steps to simplify the expression even further.

ans =(tan(4*x)*tanh(x/2))/2

Suppose, you already simplified an expression or function, but you want the other forms of the same expression. To do this, you can set the 'All' option to true. The syntax simplify(f,'Steps',n,'All',true) shows other equivalent results of the same expression in the simplification steps.

syms xy = cos(x) + sin(x)simplify(y,'Steps',10,'All',true)
ans = 2^(1/2)*sin(x + pi/4) 2^(1/2)*cos(x - pi/4) cos(x) + sin(x) 2^(1/2)*((exp(- x*1i - (pi*1i)/4)*1i)/2 - (exp(x*1i + (pi*1i)/4)*1i)/2)

To return even more equivalent results, increase the number of steps to 25.

simplify(y,'Steps',25,'All',true)
ans = 2^(1/2)*sin(x + pi/4) 2^(1/2)*cos(x - pi/4) cos(x) + sin(x) -2^(1/2)*(2*sin(x/2 - pi/8)^2 - 1) 2^(1/2)*(exp(- x*1i + (pi*1i)/4)/2 + exp(x*1i - (pi*1i)/4)/2) 2^(1/2)*((exp(- x*1i - (pi*1i)/4)*1i)/2 - (exp(x*1i + (pi*1i)/4)*1i)/2)

Simplify Using Assumptions

Some expressions cannot be simplified in general, but becomemuch shorter under particular assumptions. For example, simplifyingthis trigonometric expression without additional assumptions returnsthe original expression.

syms nsimplify(sin(2*n*pi))
ans =sin(2*pi*n)

However, if you assume that variable n representsan integer, the same trigonometric expression simplifies to 0.

assume(n,'integer')simplify(sin(2*n*pi))
ans =0

For further computations, clear the assumption.

syms n

Simplify Fractions

You can use the general simplification function, simplify,to simplify fractions. However, Symbolic Math Toolbox offers amore efficient function specifically for this task: simplifyFraction.The statement simplifyFraction(f) represents theexpression f as a fraction, where both the numeratorand denominator are polynomials whose greatest common divisor is 1.For example, simplify these expressions.

syms x ysimplifyFraction((x^3 - 1)/(x - 1))
ans =x^2 + x + 1
simplifyFraction((x^3 - x^2*y - x*y^2 + y^3)/(x^3 + y^3))
ans =(x^2 - 2*x*y + y^2)/(x^2 - x*y + y^2)

By default, simplifyFraction does not expandexpressions in the numerator and denominator of the returned result.To expand the numerator and denominator in the resulting expression,use the Expand option. For comparison, first simplifythis fraction without Expand.

simplifyFraction((1 - exp(x)^4)/(1 + exp(x))^4)
ans =(exp(2*x) - exp(3*x) - exp(x) + 1)/(exp(x) + 1)^3

Now, simplify the same expressions with Expand.

simplifyFraction((1 - exp(x)^4)/(1 + exp(x))^4,'Expand',true)
ans =(exp(2*x) - exp(3*x) - exp(x) + 1)/(3*exp(2*x) + exp(3*x) + 3*exp(x) + 1)

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Simplify Symbolic Expressions- MATLAB & Simulink (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Simplify Symbolic Expressions
- MATLAB & Simulink (2024)
Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 5871

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.