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.

simplify(y,'Steps',50)
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

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Simplify Symbolic Expressions
- MATLAB & Simulink (2024)

FAQs

How to get simplified answers in Matlab? ›

S = simplify( expr ) performs algebraic simplification of expr . If expr is a symbolic vector or matrix, this function simplifies each element of expr . S = simplify( expr , Name,Value ) performs algebraic simplification of expr using additional options specified by one or more Name,Value pair arguments.

How do you write a symbolic expression in Matlab? ›

Create Symbolic Expressions

First, create the symbolic variables a , b , c , and x . Then, create a symbolic expression f that represents the arithmetical expression a x 2 + bx + c . Solve the quadratic equation f = 0 for x by using solve . You can also apply a mathematical function to an arithmetical expression.

How do you check if symbolic expressions are equal Matlab? ›

tf = isequal( A,B ) returns logical 1 ( true ) if A and B are the same size and their contents are of equal value (from a coding perspective). Otherwise, isequal returns logical 0 ( false ). isequal does not consider NaN (Not a Number) values to be equal.

How to convert symbolic expression to function matlab? ›

ht = matlabFunction( f ) converts the symbolic expression or function f to a MATLAB® function with handle ht . If there is an equivalent MATLAB function operating on the double data type for the symbolic expression or function, then the converted function can be used without Symbolic Math Toolbox™.

How to solve expressions in MATLAB? ›

Solve an Equation

If eqn is an equation, solve(eqn, x) solves eqn for the symbolic variable x . Use the == operator to specify the familiar quadratic equation and solve it using solve . solx is a symbolic vector containing the two solutions of the quadratic equation.

What is an example of simplify in math? ›

For example, 1/2 (x + 4) can be simplified as x/2 + 2. Let us take one more example to understand it. Example: Simplify the expression: 3/4x + y/2 (4x + 7). By using the distributive property, the given expression can be written as 3/4x + y/2 (4x) + y/2 (7).

What is symbolic function in Simulink? ›

Symbolic functions accept array inputs. Calculate f for multiple values of x and y . You can differentiate symbolic functions, integrate or simplify them, substitute their arguments with values, and perform other mathematical operations. For example, find the derivative of f(x,y) with respect to x .

What is an example of a symbolic expression? ›

Symbolic speech consists of nonverbal, nonwritten forms of communication, such as flag burning, wearing arm bands and burning of draft cards. It is generally protected by the First Amendment unless it causes a specific, direct threat to another individual or public order.

What is an example of a Syms function in MATLAB? ›

For example, syms f(x) [1 2] creates the symbolic array f(x) = [f1(x) f2(x)] , the symbolic functions f1 and f2 , and the symbolic scalar variable x in the MATLAB workspace. For multidimensional arrays, these elements have the prefix f followed by the element's index using _ as a delimiter, such as f1_3_2 .

How to differentiate symbolic expression in MATLAB? ›

Df = diff( f , var , n ) computes the n th derivative of f with respect to var . Df = diff( f , var1,...,varN ) differentiates f with respect to the parameters var1,...,varN . Df = diff( f , mvar ) differentiates f with respect to the symbolic matrix variable or symbolic matrix function.

What is symbolic calculations in MATLAB? ›

Symbolic Math Toolbox™ enables you to perform symbolic computations from the MATLAB® command line by defining a special data type — symbolic objects. Functions are called using the familiar MATLAB syntax and are available for integration, differentiation, simplification, equation solving, and other mathematical tasks.

How do you extract coefficients from symbolic expressions in MATLAB? ›

To extract symbolic coefficients of a polynomial, use coeffs . This function returns a symbolic vector of coefficients and omits all zeros. For example, syms a b x; c = coeffs(a*x^3 - 5*b,x) returns c = [ -5*b, a] .

How do you write a symbolic function? ›

f( inputs ) = formula creates the symbolic function f . For example, f(x,y) = x + y . The symbolic variables in inputs are the input arguments. The symbolic expression formula is the body of the function f .

How do I change from symbolic to numeric in MATLAB? ›

If you want to use the results of your symbolic computations in numeric computations using MATLAB® (without Symbolic Math Toolbox), you can convert symbolic values to numeric values by using the double and matlabFunction functions.

How do I change the answer format in MATLAB? ›

To format the way numbers display, do one of the following:
  1. On the Home tab, in the Environment section, click Preferences. Select MATLAB > Command Window, and then choose a Numeric format option.
  2. Use the format function, for example: format short format short e format long.

How do you quickly get help about a given function in MATLAB? ›

Select a function name in the Editor, Command Window, or Help browser; right-click; and then select Help on Selection. After you type an open parenthesis for function inputs, pause or press Ctrl + F1. Use the help command. Click the function icon to the left of the command prompt.

What is simplify fraction in MATLAB? ›

simplifyFraction( expr ) simplifies the rational expression expr such that the numerator and denominator have no divisors in common. simplifyFraction( expr ,'Expand',true) expands the numerator and denominator of the resulting simplified fraction as polynomials without factorization.

How do you output an answer in MATLAB? ›

There are three common ways:
  1. Type the name of a variable without a trailing semi-colon.
  2. Use the “disp” function.
  3. Use the “fprintf” function, which accepts a C printf-style formatting string.

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 6259

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.