Weird answer while using Log

2 views (last 30 days)
Sharath Rudrapatna Venkatesh
Answered: Walter Roberson on 6 Sep 2015
syms n
x = some value
why do i get weird answer when i do n*log10(x)?
for ex: if i do 10*log10(8)i get output as 9.03. But when i include n*10*log10(8) i get
(20335778644494715*n)/2251799813685248
as the output

Answers (2)

Star Strider
Star Strider on 5 Sep 2015
Different variable classes:
syms n
Q1 = 10*log10(8)
ClassQ1 = class(Q1)
Q2 = n*10*log10(8)
ClassQ2 = class(Q2)
Q1 =
9.0309e+000
ClassQ1 =
double
Q2 =
(20335778644494715*n)/2251799813685248
ClassQ2 =
sym
If you want to change the appearance of the output, use the vpa function:
Q2 = vpa(Q2, 5)
Q2 =
9.0309*n

Walter Roberson
Walter Roberson on 6 Sep 2015
Look at http://www.mathworks.com/help/symbolic/sym.html#input_argument_flag and notice that the flag 'r', "rational" is the default. That means that all floating point numbers in MATLAB are automatically converted to rational form by default when they become associated with a symbolic expression. You can force a different interpretation by using sym() around the number with the appropriate conversion flag, such as
n*10*sym(log10(8),'d')

Tags

Community Treasure Hunt

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

Start Hunting!