main content

create symbolic numbers, variables, and expressions -凯发k8网页登录

this example shows how to create symbolic numbers, variables, and expressions. to learn how to work with symbolic math, see .

create symbolic numbers with exact representations

you can create symbolic numbers by using . symbolic numbers are exact representations, unlike floating-point numbers.

create symbolic numbers by using sym and compare them to the same floating-point numbers.

a1sym = sym(1/3)
a1sym = 

13

a1 = 1/3
a1 = 0.3333
a2sym = sym(pi)
a2sym = π
a2 = pi
a2 = 3.1416

the symbolic numbers are represented in exact rational form, while the floating-point numbers are decimal approximations.

calculations on symbolic numbers are exact. demonstrate this exactness by finding sin(pi) symbolically and numerically. the symbolic result is exact, while the numeric result is an approximation.

bsym = sin(sym(pi))
bsym = 0
b = sin(pi)
b = 1.2246e-16

when you use sym on a numeric input, the numeric expression is first evaluated to the matlab® default double-precision number that can be less accurate. then, sym is applied on that double-precision number. to represent an exact number without evaluating it to double precision, use a character vector with quotes. for example, create a symbolic number to represent a very large integer exactly.

inaccuratenum = sym(123456789012345678)
inaccuratenum = 123456789012345680
accuratenum = sym('123456789012345678')
accuratenum = 123456789012345678

you can also create symbolic complex numbers, by specifying the imaginary part of a number as 1i, 2i, and so on.

sym('1234567   1i')
ans = 1234567 i

to learn more about symbolic representation of numbers, see .

create symbolic numbers with variable precision

you can create symbolic numbers with variable-precision floating-point arithmetic by using . by default, vpa calculates values to 32 significant digits.

pivpa = vpa(pi)
pivpa = 3.1415926535897932384626433832795

when you use vpa on a numeric expression, such as log(2), the expression is first evaluated to the matlab default double-precision number that has less than 32 significant digits. then, vpa is applied on that double-precision number, which can be less accurate. for more accurate results, convert double-precision numbers in an expression to symbolic numbers with sym and then use vpa to evaluate the results with variable precision. for example, find log(2) with 17- and 20- digit precision.

vpaondouble = vpa(log(2))
vpaondouble = 0.69314718055994528622676398299518
vpaonsym_17 = vpa(log(sym(2)),17)
vpaonsym_17 = 0.69314718055994531
vpaonsym_20 = vpa(log(sym(2)),20)
vpaonsym_20 = 0.69314718055994530942

when you convert large numbers, use quotes to represent them exactly.

inaccuratenum = vpa(123456789012345678)
inaccuratenum = 123456789012345680.0
accuratenum = vpa('123456789012345678')
accuratenum = 123456789012345678.0

create symbolic variables

you can create symbolic variables using either or . typical uses of these functions include:

  • sym – create numbered symbolic variables, symbolic variables in matlab functions, or symbolic numbers whose values differ from their names in the matlab workspace.

  • syms – create fresh symbolic variables for interactive symbolic workflows, that is, for symbolic variable creation at the matlab command line or in matlab live scripts. a fresh symbolic variable does not have any assumptions.

the syms command is shorthand for the sym syntax, but the two functions handle assumptions differently. syms clears the assumptions when creating variables. however, recreating a variable using sym does not clear its assumptions. for more details about the differences of these two functions, see .

create the symbolic variables x and y using syms and sym, respectively.

syms x
y = sym('y')
y = y

the first command creates a symbolic variable x in the matlab workspace with the value x assigned to the variable x. the second command creates a symbolic variable y with the value y.

with syms, you can create multiple variables in one command. create the variables a, b, and c.

syms a b c

create array of symbolic variables

if you want to create a matlab array of numbered symbolic variables, the sym syntax is more convenient than the syms syntax. therefore, use sym to create an array of many numbered symbolic variables.

clear the workspace. create a row vector containing the symbolic variables a1,,a20 and assign it to the matlab variable a. display the variable in the matlab workspace.

clear
a = sym('a',[1 20])
a = (a1a2a3a4a5a6a7a8a9a10a11a12a13a14a15a16a17a18a19a20)
whos
  name      size            bytes  class    attributes
  a         1x20                8  sym                

a is a 1-by-20 array of 20 symbolic variables.

by combining sym and syms, you can create many fresh symbolic variables with corresponding variable names in the matlab workspace.

clear the workspace. create the fresh symbolic variables a1, ..., a10 and assign them the matlab variable names a1, ..., a10, respectively. display the variables in the matlab workspace.

clear
syms(sym('a',[1 10]))
whos
  name      size            bytes  class    attributes
  a1        1x1                 8  sym                
  a10       1x1                 8  sym                
  a2        1x1                 8  sym                
  a3        1x1                 8  sym                
  a4        1x1                 8  sym                
  a5        1x1                 8  sym                
  a6        1x1                 8  sym                
  a7        1x1                 8  sym                
  a8        1x1                 8  sym                
  a9        1x1                 8  sym                

the matlab workspace contains 10 matlab variables that are symbolic variables.

the syms command is a convenient shorthand for the sym syntax, and its typical use is to create fresh symbolic variables for interactive symbolic workflows. use the sym syntax to create the following:

  • symbolic variables in matlab functions

  • many numbered symbolic variables

  • symbolic variable whose value differs from its name in the matlab workspace

  • symbolic number, such as sym(5)

  • symbolic variable that inherits the assumptions from a previously used symbolic variable having the same name

create symbolic expressions

suppose you want to use a symbolic variable to represent the golden ratio φ=1 52.

use sym to create the golden ratio.

phi = (1   sqrt(sym(5)))/2;

now you can perform various mathematical operations on phi. for example:

f = phi^2 - phi - 1
f = 

52 122-52-32

next, suppose you want to study the quadratic function f=ax2 bx c. first, create the symbolic variables a, b, c, and x.

syms a b c x

then, create a symbolic expression f that represents the arithmetical expression ax2 bx c.

f = a*x^2   b*x   c
f = ax2 bx c

solve the quadratic equation f=0 for x by using solve.

x_0 = solve(f == 0,x)
x_0 = 

(-b b2-4ac2a-b-b2-4ac2a)

you can also apply a mathematical function to an arithmetical expression. for example, apply the bessel function of the first kind j0 to the arithmetical expression f and find its derivative with respect to x.

j_0 = besselj(0,f)
j_0 = j0(ax2 bx c)
dj_0 = diff(j_0,x)
dj_0 = -j1(ax2 bx c)b 2ax

to create a symbolic number in a symbolic expression, use sym. do not use syms to create a symbolic expression that is a constant. for example, to create an expression whose value is 5, enter f = sym(5). the command f = 5 does not define f as a symbolic expression.

you can also create symbolic expressions from strings by using when reading expressions from text files or when specifying numbers exactly.

reuse names of symbolic objects

if you set a variable equal to a symbolic expression and then apply the syms command to the variable, matlab removes the previously defined expression from the variable.

for example, create a symbolic expression f.

syms a b
f = a   b
f = a b

if you recreate f, then matlab removes the value a b from the expression f.

syms f
f
f = f

you can use the syms command to clear variables of definitions that you previously assigned to them in your matlab session. syms clears the assumptions of the variables. these assumptions (which can be real, integer, rational, and positive) are stored separately from the symbolic object. however, recreating a variable using sym does not clear its assumptions. for more information, see .

see also

| |

related topics

    网站地图