difference between symbolic and non symbolic variables in MATLAB?

27 次查看(过去 30 天)
Please kindly clear the difference between symbolic and non symbolic variables and also please guide when and where we have to use symbolic variables and where non symbolic variables

采纳的回答

Walter Roberson
Walter Roberson 2019-5-27
The difference between symbolic variables and non-symbolic variables is that
isa(SomeVariable, 'sym')
is true only for symbolic variables and symbolic functions, and not for any other kind of variable.
In MATLAB, every variable is a member of some class, and there are class methods for every operation. For example,
>> which plus(3)
built-in (/Applications/MATLAB_R2019a.app/toolbox/matlab/ops/@double/plus) % double method
This is true for all variables, whether they are symbolic or not.
There are some built-in numeric classes, namely int8, uint8, int16, uint16, int32, uint32, int64, uint64, single, double . All of those end up using CPU arithmetic instructions for most operations. There are also the almost-numeric classes logical and char, which can be automatically converted to double precision in many circumstances. MATLAB also has built in support for struct and cell arrays. These together are the "primitive" data types.
Beyond that, MATLAB extends operations by permitting "classes" to be defined. Any variable that is a member of a class that is not one of the primitive data types, is considered an "object".
Symbolic variables are not one of the primitive data types: they are objects. Arithmetic operations on symbolic variables are not handled in any direct way by machine language arithmetic operations.
What you observe as symbolic variables have two components. The symbolic names and symbolic values and the implementation of many symbolic operations, live inside a different process, in the symbolic engine, which is a fairly complete programming environment of its own. Inside the symbolic engine, values can be of several different data types, with the most common being integers of up to 10000 digits; the second most common is the ratio of two such integers, and the third most common is symbolic decimal numbers that are not floating point. All of the actual symbolic numeric computation takes place inside the symbolic engine, which does all of the arithmetic computations in software to permit extended precision or exact results.
The component that lives at the MATLAB level is an object that mostly says "This is a symbolic variable", and which internally contains a character vector. The character vector in turn contains a special variable name in a format that is not a legal MATLAB variable name. When the symbolic variable is referred to at the MATLAB level, then the class methods create character vectors of commands, substituting those special variable names for each variable, and passing the constructed command to the symbolic engine. The MATLAB level makes no attempt to track the values of the contents of symbolic variables or their printable representations. If, for example, you were to define A = sqrt(sym(2)) + sym(1).then the component that lives at the MATLAB level would just know that it was a symbolic object with internal reference something like __SYM_2783_19258_430 and would not know anything about the content. To display the variable, the class methods at the MATLAB level would construct a call evalin(symengine, '_print(__SYM_2783_19258_430)' which would examine the expression associated with __SYM_2783_19258_430 and figure out how to format it and would return an appropriate character vector.
You need symbolic variables or symbolic functions if you want to invoke the symoblic mathematics routines. For example, int() is a purely symbolic routine to do integration, and must be passed a symbolic expression or symbolic function . If you wanted to know sin(pi/7) to 75 decimal places, then you would make sure you invoked symbolic routines such as by sin(sym(pi)/7) because sin(pi/7) otherwise would evaluate the pi/7 as double precision and sin() of that would be limited to 53 bits floating point precision.
There are a few operations such as besselj that are defined inside the symbolic toolbox but which you might want to use with floating point numbers. In older versions of MATLAB you sometimes had to deliberately convert to symbolic numbers, such as besselj(sym(0.8351)) but in more modern versions, MATLAB has convenience functions that do the conversion for you so that besselj(0.8351) internally invokes besselj(sym(0.8351)) and converts the results back to floating point.
Be careful with diff(): when applied to a symbolic expression or symbolic function then it is the calculus differentiation operator, but when applied to primitive numeric values it is the arithmetic differences operation.
You would typically use symbolic variables if you wanted extended precision values, or if you wanted to do mathematical reasoning, such as finding general solutions to differential equations.
  4 个评论
Steven Lord
Steven Lord 2023-1-9
If you want to perform computations symbolically, use symbolic variables.
syms x % x is a symbolic variable
f = x.^2+2*x+1
f = 
df = diff(f, x)
df = 
In this case df is the symbolic derivative of f.
If you want to perform computations numerically, use regular numeric variables.
x = 0:10; % x is a numeric variable
f = x.^2+2*x+1
f = 1×11
1 4 9 16 25 36 49 64 81 100 121
df = diff(f)
df = 1×10
3 5 7 9 11 13 15 17 19 21
In this case df is the numeric difference between consecutive elements of f.
Walter Roberson
Walter Roberson 2023-1-10
Symbolic numeric calculations have greater range and precision possibilities than single() or double(), but are typically significantly slower.
Symbolic calculations are not done at the MATLAB level, and MATLAB does not "understand" symbolic calculations. Instead the inputs are passed over to a separate process that returns a reference to the appropriate symbolic entities. If MATLAB is asked to display the values, it tells the separate process to do the work of converting to character.
Symbolic expressions can be operated on through algebra and calculus. For example the toolbox can integrate sin(x)+1 to get x - cos(x)

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by