Accuracy of linspace using low integers...

조회 수: 26 (최근 30일)
Kevin
Kevin 2013년 7월 9일
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