How to get equispaced values with a more exact result than using linspace and column functions?

2 views (last 30 days)
Hi all,
I just realized that the linspace function does not give equispaced values (they are somewhat equispaced, but not exactly), However, I want to use the output values in another function which requires equispaced input. I was wondering is there a function that can produce equispaced output over an interval? I want exactly equispaced output, not something like linspace and coloum which does not produce exactly equispaced output. Is there a way to do that with interp1 function for example?
Thanks in advance!

Answers (2)

Matt J
Matt J on 31 Jan 2024
Edited: Matt J on 31 Jan 2024
I want exactly equispaced output
There is no such thing as exactly anything in floating point computation. The inaccuracies you refer to in linspace are presumably due to finite floating point precision. There is no way you will be able to avoid that issue in any alternative method you pursue to subdivide an interval, unless of course it is an integer-valued interval with integer-valued partition widths. But in that case, linspace will be exact, e.g.,
linspace(1,9,5) == [1,3,5,7,9]
ans = 1×5 logical array
1 1 1 1 1

Steven Lord
Steven Lord on 31 Jan 2024
How are you determining that the vector is not equally spaced? And can you prove that for your values the points that are exactly the same distance apart are even representable as double precision values?
x = linspace(0, 1, 4)
x = 1×4
0 0.3333 0.6667 1.0000
Is one-third exactly representable in double precision? [The answer is no. It is exactly representable neither in a finite number of decimal digits nor a finite number of bits.]
d = diff(x)
d = 1×3
0.3333 0.3333 0.3333
d(1) == d(3)
ans = logical
0
differenceInSpacing = d(1)-d(3)
differenceInSpacing = -5.5511e-17
But is it uniformly spaced to within roundoff? The isuniform function, introduced in release R2022b, can tell us that.
[isUniformlySpaced, distanceBetweenPoints] = isuniform(x)
isUniformlySpaced = logical
1
distanceBetweenPoints = 0.3333

Categories

Find more on Vector Fields in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!