Wednesday, May 12, 2010

Symbolic expressions,variable precision and exact arithmatic in MATLAB

Symbolic Expressions, Variable Precision, and Exact Arithmetic

As we have noted,MATLAB uses floating point arithmetic for its calculations.
Using theSymbolic Math Toolbox,you can also do exact arithmetic with symbolic expressions. Consider the following example:

>> cos(pi/2)
ans =

The answer is written in floating point format and means 6.1232×10-17 .
However, we know that cos(p/2) is really equal to 0. The inaccuracy is due
to the fact that typing pi in MATLAB gives an approximation to pi accurate
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 p/2 by typing sym(’pi/2’). Now let’s take the cosine of the symbolic
representation of p/2:
>> cos(sym(’pi/2’))
ans =
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 asymbolic 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 =

Finally,you can also dovariable-precision arithmetic with vpa.For example,
to print 50 digits of 2, type

>> vpa(’sqrt(2)’, 50)
ans =


Related Posts Plugin for WordPress, Blogger...

civil engineering,electrical and other engg. software video tutorials.ebook and setups for free.... SAP2000,ETABS,SAFE,Autocad,revit Architecture,MS Project,Primavera ,Matlab,PSpice and much more

  © Blogger templates Newspaper III by 2008

Back to TOP