The difference between symbolic operation and numerical operation in MATLAB

The fundamental difference between the two is that undefined free variables are not allowed in numerical calculation expressions and matrix variables, while symbolic calculations can contain undefined symbolic variables . For general programming software such as C, C + + and other languages, it is okay to implement numerical calculations, but it is not easy to implement symbolic calculations . And Matlab has its own symbolic toolbox Symbolic Math Tooibox, and can use the mathematical software Maple, so Matlab also has a powerful symbolic operation function.
1. The definition of string
MATLAB uses single quotes to define string. For example, in the command window input: A='hello, this is a string', it will output A=hello, this is a string
2. Define symbol variables and symbol expressions
in the MATLAB command window, the input numeric variables must be assigned in advance, Otherwise, an error will be prompted. Only symbol variables can legally appear in expressions without prior assignment, but symbol variables must be defined in advance.
Among the data types of Matlab, character type and symbol type are two important and easily confused data types.
Symbol variables are created by using the commands sym and syms .
Their format is:
S = sym( ′ A ′) defines a single symbol variable S
syms abc defines multiple symbol variables a, b, c
The use of the syms command is easier than sym , It can define multiple symbol variables at one time, and the format is concise. Therefore, syms is generally used to create symbol variables. Note that each symbol variable must be separated by a space.
Statement Syms xyzw
Indicates that x, y, z and w are defined as symbolic variables. If you enter: p = sin (x) + cos (x) + z + 2 * w, it means that P after performing the expression sin (x) + cos (x ) + z + 2 * w
Third, the numerical expression Convert to Symbolic Expression The
command sym can convert a numeric expression into a symbolic expression , and its syntax is:
Sym('Numerical expression')
For example, in the command window, enter:
P=sym('2+sqrt(5)')
Then get the output:
P=2+sqrt(5), at this time P is a symbolic expression, not a numeric expression.
4. Calculate the value of the symbolic expression
If you want to calculate the value of the previous symbolic expression P, you need to use eval_r (P) to calculate the approximate value of P. You can enter:
eval_r(P) to
get the output: ans=4.2361
Since P=sym('2+sqrt(5)') is actually a symbolic constant, you can also use the vpa command to calculate (use vpa(P) to be more accurate , Which means to convert the sign quantity into a numerical quantity with 32 significant digits, and 5 decimal places can also be set (digits(5), vpa(P)).
5. The mutual conversion of
numerical variables, symbol variables, and character variables The mutual conversion of numerical variables, symbol variables, and character variables In the Matlab workspace, numerical values, symbols, and characters are the three main data types.
Matlab can use commands to realize the conversion between different types of data.

Guess you like

Origin blog.csdn.net/Dust_Evc/article/details/109005459