Graphics Programs Reference
In-Depth Information
to about 15 digits, not its exact value. To compute an exact answer, instead
of an approximate answer, we must create an exact symbolic representation
of π/ 2 by typing sym('pi/2') . Now let's take the cosine of the symbolic
representation of π/ 2:
>> cos(sym('pi/2'))
ans =
0
This is the expected answer.
The quotes around pi/2 in sym('pi/2') create a string consisting of the
characters pi/2 and prevent MATLAB from evaluating pi/2 as a floating
point number. The command sym converts the string to a symbolic expression.
The commands sym and syms are closely related. In fact, syms x is equiv-
alent to x = sym('x') . The command syms has a lasting effect on its argu-
ment (it declares it to be symbolic from now on), while sym has only a tempo-
rary effect unless you assign the output to a variable, as in x = sym('x') .
Here is how to add 1 / 2 and 1 / 3 symbolically:
>> sym('1/2') + sym('1/3')
ans =
5/6
Finally,youcanals od o variable-precisionarithmetic with vpa .Forexample,
to print 50 digits of 2, type
>> vpa('sqrt(2)', 50)
ans =
1.4142135623730950488016887242096980785696718753769
You should be wary of using sym or vpa on an expression that
MATLAB must evaluate before applying variable-precision
arithmetic. To illustrate, enter the expressions 3ˆ45, vpa(3ˆ45) ,
and vpa('3ˆ45') . The first gives a floating point approximation to
the answer, the second — because MATLAB only carries 16-digit
precision in its floating point evaluation of the exponentiation —
gives an answer that is correct only in its first 16 digits, and the
third gives the exact answer.
See the section Symbolic and Floating Point Numbers in Chapter 4 for details
about how MATLABconverts between symbolic and floating point numbers.
Search WWH ::




Custom Search