Symbolic objects and symbolic operations in MATLAB

Source: Internet
Author: User

Symbolic objects (symbolic Objects, unlike ordinary numerical computations) are a special data type in MATLAB that can be used to represent symbolic variables, expressions, and matrices, allowing for algebraic analysis and symbolic computation without regard to the specific values of the symbols ( Symbolic math operations), such as solving algebraic equations, differential equations, and matrix operations. Symbolic objects need to be specified by sym or syms functions, and normal numbers can be treated as symbolic objects after they are converted into symbolic types. We can use a simple example to illustrate the difference between a numerical calculation and a symbolic calculation: The result of 2/5+1/3 is 07333 (double type numeric operation), while Sym (2)/sym (5) +sym (1)/sym (3) results in 11/15, and here 11/ 15 are still belonging to the sym type, which is the number of symbols.

1. Definition of Symbolic objects

The symbol object is defined with Syms x y z; The form, for example

  

In addition, it is possible to use X=sym (' x '); Y=sym (' Y '); The format defines the symbolic variable, which is followed by Syms X y; Have the same meaning. MATLAB also supports a one-time definition of a symbolic expression, such as F=sym (' A*x^2+b*x+c '), after the definition of the expression will be considered as a whole, MATLAB does not automatically put each of the items a,x,b, C is defined as a symbolic variable. So if you want to evaluate a symbolic expression, you still have to define the item that participates in the calculation separately.

2. Number of symbols

MATLAB supports converting a number to a symbolic object , and the number of symbols converted (symbolic Numbers) still has the meaning of a numeric value , except that MATLAB does not make floating-point operations on it. The definition of the number of symbols is also implemented by the SYM command. For example, MATLAB can be used for fractional operation 1/2+1/3=5/6,

  

If you enter 1/2+1/3 directly in the command window, you will get 0.8333 results. The number of symbols takes more time and space than floating-point calculations, but it is an accurate calculation with no errors.

3. Symbolic Calculation

Symbolic variables can be used to construct symbolic expressions, symbolic functions, symbolic equations and symbolic matrices, and then the factorization, derivation, equation and solution of matrix function value can be obtained.

(1) factorization factoring

The command format for factoring The symbolic expression F is factor (f). For example, a command that breaks down an expression (x^9-1) into multiple

syms x; 
Factor (x^9-1)

Run the result as

  

(2) derivation/differential differentiation

The command for the derivation of the symbolic expression f (x) is diff (f). matlab can automatically recognize the variables in the expression , if the expression a contains only one variable, then the diff (f) function is relative to this unique variable derivative, if a contains more than one variable, then diff (f) is the default variable in F derivative, The default variable is the variable in F that is closest to the x in the alphabet. The default variable for the expression F can be derived using the function Findsym (f,1).

Use diff (f,n) If you want the N-order number of F (x) to be obtained. For expressions with multiple independent variables, diff can be used to achieve a bias , such as the expression F (s,t) relative to the S Bias Guide with diff (f,s), if the relative s for the 2-Order Bias Guide is diff (f,s,2).

(3) seeking integral integral

The indefinite integral of the symbolic expression f (x) is expressed in int (f). For an expression F (x,s,t) that contains more than one symbolic variable, use an int (f,t) If you want it to be integral with respect to T. an int (f,0,1) or an int (f,x,0,1) is used to calculate the definite integral of f (x) from 0 to 1.

(4) seeking limit limits

The limit (f,x,0) of the function f (x) or F (x, Y) at which the X approaches 0 is obtained (1+x/n) n is limited by the limit ((1+x/n) ^n in n approaching Infinity ,n,inf), ask a (x) to use limit (f,x,0, ' left ') or limit (0-) when x approaches to the 0+ ( right limit ) orx ( f,x,0, 'right ').

(5) symbol accumulation symbolic summation

The general arithmetic accumulates with sum, while the number of symbols or the accumulation of symbolic expressions is symsum. For example, seeking 1+1/22+1/32+ ... and 1+x+x2+ ... Use the following code

Syms x K;
S1=symsum (1/k^2,1,inf)
s2=symsum (X^k,k,0,inf)

(6) simplified simplification of symbolic expressions

Using some of the expression transformation functions provided by MATLAB, we can simplify symbolic expressions or convert them into different forms based on requirements. Collect the expression can be arranged from high to low by the power of its default symbolic variable, and the coefficients of the symbolic variable of the same power are merged together. If the expression contains more than one variable, you can use the second parameter to indicate which variable is whichever. For example, for f=x^3*y+x* (y^2+6) +x*y+8*x^2+y^2*x, the result of Collect (f) is x^3*y+8*x^2+ (2*y^2+6+y) *x, The result of Collect (F,y) isy^2*x+ (x+x^3) *y+8*x^2+6*x. Expand expands the expression and applies some basic formulas (equality identities). For example expand ((x-1) * (x-2) * (x-3)) results for x^3-6*x^2+11*x-6; The result of expand (exp (A+B)) is exp (a) *exp (b); The result of expand (cos (x+y)) is cos (x) *cos (y)-sin (x) *sin (y); The result of expand (cos (3*acos (x))) is 4*x^3-3*x. Horner decomposes the expression into nested form.  For example, the result of Horner (X^3-6*X^2+11*X-6) is -6+ (11+ ( -6+x) *x) *x. Factor factorization, the expression is decomposed into several forms of the product, if the expression cannot be decomposed into the form of product, then the decomposition result is the original expression remains unchanged. Simplify uses a variety of built-in basic formulas (algebraic identities) to simplify expression. Simpe the expression in different ways, trying to get the shortest result (the result expression contains the fewest number of characters).

(7) Solving algebraic equations

The order of a (x) =0 for solving algebraic equations is solve (a), for example

  

(8) The function value of the symbol matrix is evaluated

A

Symbol matrix is a matrix in which a matrix element contains a symbolic variable, and a matrix of this type can be computed in the same way as a normal numerical matrix, such as a transfer from http://www.cnblogs.com/zhouqing/p/3306076.html 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.