Crazy, malformatted numbers as answer to simple equation.
12 views (last 30 days)
Show older comments
I'm doing work for an engineering course, and what seems to be a simple equation is becoming a big problem very quickly. Essentially, I multiply any number (except for 1) with a negative value for it's engineering notation by any symbol, and the answer I get is, in a word, strange. For instance, if I multiply 2e-6 by s, then I expect 2.0000e-06 * s as the answer. Instead I get (4722366482869645*s)/2361183241434822606848. I'll provide examples of the issue below.
>> syms s t;
>> % first example of strange answer
>> 2*10^(-6) * s
ans =
(4722366482869645*s)/2361183241434822606848
>> % second example of strange answer
>> 2e-6 * t
ans =
(4722366482869645*t)/2361183241434822606848
>> % conditions that do not reproduce the problem
>> 2e6*s
ans =
2000000*s
>> 1e-6*s
ans =
s/1000000
If I turn in any work with answers that look like this, it'll cost me a fair amount of points if not all of them. Is there anyway to get these answers to format correctly?
0 Comments
Accepted Answer
John D'Errico
on 24 Jun 2016
Edited: Stephen23
on 25 Jun 2016
Oh, really, it'll cost you a lot of points? To turn in a mathematically correct answer?
When you type in a number like 2e-6, that number is not actually exactly representable as a floating point number. So MATLAB must approximate it in floating point, as a BINARY fraction, which will be a repeating binary. Then it passes that result to the symbolic toolbox, which turns it into a symbolic number, that represents the approximate value that was passed in!
You can force MATLAB to recognize 2e-6 as a symbolic constant however.
sym('2e-6') * s
ans =
0.000002*s
Or, you can learn to use vpa.
help vpa
Or both.
5 Comments
John D'Errico
on 24 Jun 2016
Edited: John D'Errico
on 24 Jun 2016
Just to make sure you understand what is happening, try this:
sym( 2*10^(-6) )
ans =
4722366482869645/2361183241434822606848
vpa(ans,50)
ans =
0.0000019999999999999999094962236517725173712278774473816
As you can see, MATLAB took the number 2e-6, as a floating point number, represented in double precision. Then it passes that into sym, which does the very best that it can.
Better is to force sym to do the right thing in the first place.
sym('2*10^(-6)')
ans =
1/500000
As for my professor problem, I still got an A in the class. :)
Walter Roberson
on 24 Jun 2016
Note that the sym('2*10^(-6)') returning 1/500000 is an example of using the default 'r' conversion. You get this more accurate answer than you do with sym(2*10^(-6)) because in the latter case the 2*10^(-6) is converted to floating point before being passed to sym() so the exact value has already been lost.
More Answers (1)
Walter Roberson
on 24 Jun 2016
Those are correctly formatted for what you asked MATLAB to do.
Have a look at http://www.mathworks.com/help/symbolic/sym.html#inputarg_flag sym's "flag" parameter:
'r' (default) | 'd' | 'e' | 'f'
"When sym uses the rational mode, it converts floating-point numbers obtained by evaluating expressions of the form p/q, p*pi/q, sqrt(p), 2^q, and 10^q for modest sized integers p and q to the corresponding symbolic form. This effectively compensates for the round-off error involved in the original evaluation, but might not represent the floating-point value precisely. If sym cannot find simple rational approximation, then it uses the same technique as it would use with the flag 'f'."
Add this together with the fact that
syms s t;
means the same thing as
s = sym('s');
t = sym('t');
So you are working with symbolic expressions and floating point values, and when you do that, the default is to convert the floating point values according to 'r' (rational).
The work-around is to explicitly sym() all of your floating point constants according to the method that you want to be used, such as
sym(2*10^(-6), 'd') * s
Your choice of numeric conversions affects how the calculation is done! Rational expressions are treated differently by the symbolic engine than decimal expressions are!
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!