Four arithmetic operations, factorization, simplification, expansion and combination of symbolic objects and obtaining numerator and denominator of symbolic expressions

In MATLAB, like digital symbols, symbol objects can also perform certain calculations, simplifications and other related operations.

Table of contents

1. Four arithmetic operations of symbolic objects

(1) Simple four arithmetic operations

(2) Operate the symbols in the matrix

2. Factorization of symbolic elements

(1) Decompose the prime factor

(2) Decomposing polynomials

3. Simplification of symbolic expressions

4. Expansion and merging of symbolic polynomials

(1), Expansion of symbolic expressions

(2) merger

5. Calculate the coefficient of a symbolic expression

6. Find the numerator and denominator of a symbolic expression


1. Four arithmetic operations of symbolic objects

(1) Simple four arithmetic operations

For symbolic objects, you can also perform addition (+), subtraction (-), multiplication (*), division (/) and power (^) operations on them. First, give the following example for illustration:

syms x y z
a=3*x^3+4*y^2+z;
b=4*x^2+6*y+z^2;
c=x;
d=3*x^3+5*x^2+7*x;
sum=a+b
sub=a-b
mul=a*c
she=d/c
pow=a^2

The result after running is as follows:

sum =
    3*x^3 + 4*x^2 + 4*y^2 + 6*y + z^2 + z
sub =
    3*x^3 - 4*x^2 + 4*y^2 - 6*y - z^2 + z
mul =
    x*(3*x^3 + 4*y^2 + z)
she =
    (3*x^3 + 5*x^2 + 7*x)/x
pow =
    (3*x^3 + 4*y^2 + z)^2

From the above results, it can be seen that for symbol objects, simple arithmetic operations can be performed, and the operation rules are the same as the results obtained by our daily calculations.

(2) Operate the symbols in the matrix

MATLAB can not only perform calculation operations on symbolic objects but also perform related operations on symbolic objects in matrices, including matrix multiplication and division operations (*, \, /) and power operations (^) and matrix element multiplication and division operations (. *, ./, .\) and exponentiation (.^).

The first is the matrix multiplication and division operation:

syms x y
A=[3,x,2*x;4,6*y,x*y;2,10+x,2+y];
B=[5,2+x,10+y;2*x,8*y,2*x;12,1+y,x*y];
C=A*B

The running results are as follows:

C =
    [2*x^2+24*x +15,3*x+8*x*y+2*x*(y+1)+6,3*y+2*x^2*y+2*x^2+30]
    [24*x*y+20,4*x +48*y^2+x*y*(y+1)+8,x^2*y^2+12*x*y+4*y+40]
    [12*y+2*x*(x+10)+34,2*x+(y+1)*(y+2)+8*y*(x+10)+4,2*y+2*x*(x+10)+x*y*(y+2)+20]

For elements, assume that the following two matrices are operated:

syms x y
A=[2*x,5+y;10,24+x];
B=[12*x+y,8*y+x;16-x,18];
C=A/B
D=A\B
E=A^2

The running results are as follows:

C =
 
[(41*x-16*y+x*y-80)/(200*x-110*y+8*x*y+x^2),(-2*x^2-4*x*y+60*x+y^2+5*y)/(200*x-110*y +8*x*y +x^2)]
[(x^2+8*x-204)/(200*x-110*y+8*x*y+x^2),(278*x-56*y+x*y+12*x^2)/(200*x-110*y + 8*x*y + x^2)]
 
 
D =

[(293*x+8*y+2*x*y+12*x^2-80)/(2*(x^2+24*x-5*y-25)),(24*x+174*y+8*x*y+x^2-90)/(2*(x^2+24*x- 5*y-25))]
[-(x^2+44*x+5*y)/(x^2+24*x-5*y-25),(13*x-40*y)/(x^2+24*x-5*y-25)]
 

E =
 
[4*x^2 + 10*y + 50, (x + 24)*(y + 5) + 2*x*(y + 5)]
[       30*x + 240,         10*y + (x + 24)^2 + 50]
 

It should be noted that \ and / represent different division operations in MATLAB, assuming there are matrices A and B, then A\B represents, AB^{-1}and A/B=A^{-1}B.

Symbolic variables in MATLAB can not only perform calculations on matrices, but also perform operations on symbolic elements of matrices.

For example, perform calculations between matrix elements on the above matrix:

syms x y
A=[2*x,5+y;10,24+x];
B=[12*x+y,8*y+x;16-x,18];
C=A./B
D=A.\B
E=A.^2

The result of the operation is as follows:

C =
    [(2*x)/(12*x + y), (y + 5)/(x + 8*y)]
    [    -10/(x - 16),        x/18 + 4/3]
D =
    [(12*x + y)/(2*x), (x + 8*y)/(y + 5)]
    [      8/5 - x/10,       18/(x + 24)]
E =
    [4*x^2,  (y + 5)^2]
    [  100, (x + 24)^2]

2. Factorization of symbolic elements

MATLAB can perform factorization operations on expressions, and the factor function is provided in MATLAB to complete this function. factor can be used to decompose prime factors and polynomials.

(1) Decompose the prime factor

The operation of decomposing prime factors in MATLAB is as follows:

x=factor(24)

The result looks like this:

x =
     2     2     2     3

As shown in the code segment obtained above, the factor function is mainly used to operate on prime numbers, and if it is a prime number, the returned result is the prime number itself.

x=factor(19)

The result looks like this:

x =
    19

(2) Decomposing polynomials

MATLAB's factor function can not only decompose prime numbers, but also decompose polynomials. For example:

y=x^2-5*x+6;
factor(y)

The running results are as follows:

ans =
     [x - 2, x - 3]

It can be seen from the running results y=x^2-5x+6that it is decomposed into y=(x-2)(x-3), and the function of factorization is completed.

3. Simplification of symbolic expressions

When performing mathematical calculations, it is often necessary to simplify symbolic expressions, and the simplify function is provided in MATLAB for simplification.

For example, simplify the following symbolic expression:

z=\frac{3xy}{x^2+4x}

 Simplify it using MATLAB code:

z=(3*x*y)/(x^2+4*x);
simplify(z)

The running results are as follows:

ans =
    (3*y)/(x + 4)

This is a simplification of a simple symbolic expression. The process of simplification of a symbolic expression is actually to extract the common factor and then delete the same common factor. For example, simplify the following formula:

y=\frac{x^2-4x+3}{x^2-5x+6}

 The simplification process of this symbolic expression is as follows:

y=\frac{x^2-4x+3}{x^2-5x+6}=\frac{(x-3)(x-1)}{(x-3)(x-2)}=\frac{x-1}{x-2}

And the calculation process through MATLAB is:

syms x
y=(x^2-4*x+3)/(x^2-5*x+6);
simplify(y)

The running results are as follows:

ans =
     (x - 1)/(x - 2)

It can be seen that the result is the same as that of human calculation. Another example is to simplify the following formula:

z=\frac{x^3-y^3}{x-y}

  The MATLAB code looks like this:

syms x y
z=(x^3-y^3)/(x-y)
simplify(z)

The running results are as follows:

ans =
    x^2 + x*y + y^2

It can be seen from the above results that MATLAB can simplify a single independent variable or multiple independent variables, which is convenient for users to operate.

4. Expansion and merging of symbolic polynomials

Symbolic polynomials can be combined and expanded in MALTAB.

(1), Expansion of symbolic expressions

The expand function is provided in MATLAB to expand a symbolic expression into a symbolic polynomial.

For example:

A=(3*x^2+4*x*y+2)*(x*2);
B=(4*x^3+8*y^2)*(4*x^2);
C=expand(A)
D=expand(B)

The running results are as follows:

C =
    6*x^3 + 8*y*x^2 + 4*x
D =
    16*x^5 + 32*x^2*y^2

Using the expand function, the fractions obtained by factorization can be combined again. As follows:

syms x
A=x^2-5*x+6;
B=factor(A)
C=expand(B(1,1)*B(1,2))

The result of running it is as follows:

B =
    [x - 2, x - 3]
C =
    x^2 - 5*x + 6

It can be seen that the value of C obtained is the same as that of A, so expand can perform an inverse operation on the decomposed factors obtained by factor, that is, a merge operation.

expand can also operate on trigonometric functions:

syms x y
A=expand(sin(x+y))
B=expand(sin(x-y))
C=expand(cos(x+y))
D=expand(cos(x-y))

The running results are as follows:

A = 
    cos(x)*sin(y) + cos(y)*sin(x)
B =
    cos(y)*sin(x) - cos(x)*sin(y)
C =
    cos(x)*cos(y) - sin(x)*sin(y)
D =
    cos(x)*cos(y) + sin(x)*sin(y)

It can be seen that the result of the parentheses seen by the trigonometric function is the same as the result of the formula we wrote down.

(2) merger

After the symbolic expressions are expanded, it is also necessary to combine the symbolic expressions at some point. The collect function is provided in MATLAB for merging operations. For example:

syms x
y=(x-2)*(x-3);
y
y=collect(y)

The running results are as follows:

y =
    (x - 2)*(x - 3)
y = 
    x^2 - 5*x + 6

Through the comparison before and after, it can be found that the collect function merges the expressions in the two brackets.

The collect function can specify arguments when merging, for example:

syms x y
z=(x^2+3*y-4)*(x+4*y^2-3*y+5);
z1=collect(z,x)
z2=collect(z,y)

The running results are as follows:

z1 = 
     x^3 + (4*y^2 - 3*y + 5)*x^2 + (3*y - 4)*x + (3*y - 4)*(4*y^2 - 3*y + 5)
z2 =
     12*y^3 + (4*x^2 - 25)*y^2 + (- 3*x^2 + 3*x + 27)*y + (x^2 - 4)*(x + 5)

By running the results, it can be found that when a symbolic expression contains multiple arguments, the results obtained by selecting different arguments for the symbolic expression are also different.

5. Calculate the coefficient of a symbolic expression

In MATLAB you can use the coeffs function to obtain the coefficients of a symbolic expression. The most basic usage of the coeffs function in MATLAB is as follows:

c=coeffs(A,var)

 In the above formula, A means that there is a symbolic expression, and var means that the return result is to return the coefficient of the symbolic constant in the order of the ascending power of var:

syms x
y=3*x^4+2*x^3-8*x^2+4*x+10;
coeffs(y,x)

The result of the operation is as follows:

ans = 
    [10, 4, -8, 2, 3]

Another form is:

[c,num]=coeffs(A,var)

 In this form, c represents each coefficient matrix, and num represents which item each coefficient corresponds to.

For example:

syms x
y=3*x^4+2*x^3-8*x^2+4*x+10;
[c,num]=coeffs(y,x)
M=c.*num

The running results are as follows:

c =
    [3, 2, -8, 4, 10]
num =
    [x^4, x^3, x^2, x, 1]
M =
    [3*x^4, 2*x^3, -8*x^2, 4*x, 10]
 

By comparing the elements of the two matrices c and num, it can be found that the elements in c are the coefficients of the elements at the same position as num, and the matrix M can be obtained by dot multiplying the elements in the two matrices. The original matrix can be obtained by adding all the elements of .

When there are multiple variables in a symbolic expression, the coeffs function can also sort the multi-symbolic expressions. For example, when there are both variable x and variable y in the symbolic expression, the coefficient is obtained according to the symbol x as an independent variable:

syms x y
A=2*x^2+3*x+5*y+4;
B=2*y;
C=expand(A*B)
coeffs(C,x)

The running results are as follows:

C =
    4*x^2*y + 6*x*y + 10*y^2 + 8*y
ans =
    [10*y^2 + 8*y, 6*y, 4*y]

As can be seen from the above results, the elements of the returned matrix are all marked with y. So, what happens if we set both x and y as arguments of a symbolic expression? For example:

syms x y
A=2*x^2+3*x+5*y+4;
B=2*y;
C=expand(A*B)
coeffs(C,[x,y])

The result looks like this:

C =
    4*x^2*y + 6*x*y + 10*y^2 + 8*y
ans =
    [8, 10, 6, 4]

 It can be seen from the above results that when x and y are set as independent variables at the same time, the result returned at this time is a matrix containing only numbers, which is the coefficient of the double independent variables of x and y.

6. Find the numerator and denominator of a symbolic expression

When operating on a symbolic expression, it is often necessary to obtain the numerator or denominator of the symbolic expression for operation. The numden function is provided in MATLAB to obtain the numerator or denominator in a symbolic expression. The method of use is:

[numerator,denominator]=numden(M)

 In the above formula, M can represent a single number, expression, vector or matrix.

For example:

syms x y
%如果是数字时
[num1,den1]=numden(sym(12/19))
%如果是表达式时
[num2,den2]=numden((3*x^2+4*y^3)/(4*x*y))
%如果是向量时
[num3,den3]=numden([(5*x+4*y)/(6*x*y),(8*x^4)/(7*y^2)])
%如果是矩阵时
[num4,den4]=numden([(3*x*y)/(5*x+6*y),(4*x^2)/(4*y);(4*y)/(x^3),(4*x+6*y)/(4*x+5*y)])

The running results are as follows:

num1 =
    12
den1 =
    19
num2 =
    3*x^2 + 4*y^3
den2 =
    4*x*y
num3 =
    [5*x + 4*y, 8*x^4]
den3 =
    [6*x*y, 7*y^2]
num4 =
    [3*x*y,       x^2]
    [  4*y, 4*x + 6*y]
den4 =
    [5*x + 6*y,         y]
    [      x^3, 4*x + 5*y]
 

It can be seen from the running results that the symbolic expression being operated is a number, expression, vector or matrix, and the returned type is also a number, a symbolic expression, a vector or a matrix, and the result obtained and the symbolic expression being operated of the same type.

Guess you like

Origin blog.csdn.net/qq_54186956/article/details/127474004