Fortune Telling Collection - Comprehensive fortune-telling - Hello, everyone. What's the difference between symbolic operation and numerical operation in matlab?

Hello, everyone. What's the difference between symbolic operation and numerical operation in matlab?

The fundamental difference between them is that undefined free variables is not allowed in the expressions and matrix variables of numerical calculation, while symbolic calculation can contain undefined symbolic variables. For general programming software in C, c++ and other languages, it is possible to realize numerical calculation, but it is not an easy task to realize symbolic calculation. Matlab has its own symbolic toolbox Symbolic Math Tooibox, which can use the mathematical software Maple, so Matlab also has powerful symbolic operation function.

First, the definition of string

MATLAB uses single quotation marks to define strings. For example, if you enter a =' hello, this is a string' in the instruction window, you will output A=hello, which is a string.

Second, define symbolic variables and symbolic expressions.

In the instruction window of MATLAB, the input numerical variables must be assigned in advance, otherwise an error will be prompted. Only symbolic variables can legally appear in the expression, without prior assignment, but symbolic 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.

Symbolic variables are created by instructions sym and syms.

They are used in the following formats:

S = sym(' A') defines a single symbolic variable s.

A number of symbolic variables a, b and c are defined.

The syms command is easier to use than sym, and it can define multiple symbolic variables at one time, with a simple format. Therefore, syms is generally used to create symbolic variables. Note that symbol variables must be separated by spaces.

Report symbol x y z w

X, y, z and w are defined as symbolic variables. If you input again: p=sin(x)+cos+z+2*w, p represents the expression sin(x)+cos(x)+z+2*w after execution.

3. Convert numerical expression into symbolic expression.

Command sym can convert numerical expressions into symbolic expressions, and its syntax is:

Symbol ("numeric expression")

For example, in the command window, enter:

P=sym('2+sqrt(5)')

Then you will get the output:

P=2+sqrt(5), when p is a symbolic expression, not a numerical expression.

Fourth, calculate the value of 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)

Get the output: ans=4.236 1.

Since p = sym ('2+sqrt (5)') is actually a symbolic constant, it can also be calculated by vpa command.

Five, numerical variables, symbolic variables, character variables mutual conversion

Transformation of Numerical Variables, Symbolic 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.