x=linspace(0,pi,100) and x=0:0.01:pi

5 views (last 30 days)
what is different with x=linspace(0,pi,100) and x=0:0.01:pi??

Accepted Answer

Image Analyst
Image Analyst on 26 Mar 2019
Does this help?
x1 = linspace(0,pi,100) % 100 elements with spacing to be determined (it's pi/100)
x2 = 0:0.01:pi % N elements with spacing of 0.01, but N to be determined (it's 315)
fprintf(' x1 x2\n')
for k = 1 : length(x2)
if k <= length(x1)
fprintf('%10.4f', x1(k));
else
fprintf(' ');
end
fprintf('%10.4f\n', x2(k));
end
You'll see in the command window:
x1 x2
0.0000 0.0000
0.0317 0.0100
0.0635 0.0200
0.0952 0.0300
0.1269 0.0400
0.1587 0.0500
Does that clarify it?

More Answers (1)

madhan ravi
madhan ravi on 26 Mar 2019
linspace creates vector from to 0 to pi with 100 numbers
The other creates a vector with a step .01.
Please read the below documentations:
doc linspace
doc colon
  3 Comments
Walter Roberson
Walter Roberson on 26 Mar 2019
Also, there are some differences in how the two handle round-off error.
linspace calculates the average step size, and then each entry is the first entry plus an integer multiple of the step size.
The colon operator controls for round off from the middle, calculating the first half and second half separately to try to reduce drift.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!