Accuracy of linspace using low integers...

19 次查看(过去 30 天)
Matlab Maci64 R2012a
This bothers me greatly. I am creating an evenly spaced array on the fly for several particles that I integrate using ode45. I then place the answer on an interpolated timeline so that i can easily align the particles for animations, etc...
After having collected a large data set, I noticed the following, linspace is not accurate, even when it really should be.
try this:
linspace(0,1,401)'
then
linspace(0,10,401)'
This is a problem as I tried using unique to help me determine the final number of interpolated steps I had generated using the solver, only to find that there were several values on my timeline which differed by 1e-15.
Why can't linspace behave? When dealing with fractions of low integers, it really should not introduce artifacts in the numbers. Of course, I can solve my problem in a number of other manners, but still, linspace should generate artifacts, or perhaps it should have an added option to place the numbers within a certain accuracy?
-Kevin Mcilhany

回答(2 个)

Jan
Jan 2013-7-9
Welcome to the world of floating point numerics with limited precision. Please read one of the most frequently asked questions: FAQ: Why is 0.3-0.2-0.1 ~= 0
So linspace is behaving exactly as expected, but decimal numbers cannot be converted to the binary format without a distinct lack of accuracy.

Matt J
Matt J 2013-7-9
编辑:Matt J 2013-7-9
Never mind linspace. What about this:
>> format long;
>> 39.8
ans =
39.799999999999997
or this,
>> 9.8
ans =
9.800000000000001
  2 个评论
Kevin
Kevin 2013-7-9
Exactly! So instead, I use str2num and sprintf and I waste cpu cycles. Mathworks really needs to work on long format.
Matt J
Matt J 2013-7-9
I don't think it's an issue with long format. Compare,
>> 39.8-39.7
ans =
0.099999999999994
but,
>> 0.2-0.1
ans =
0.100000000000000

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by