How to use linspace and what is it's purpose?

Hi all,
What is the purpose of linspace?
eg linspace(2,7,100) what will happen when i type in this code?
Thanks

3 Comments

Why not try it? Rather than asking what will happen, TRY IT. See what happens. Your computer won't explode.
Why not read the help? This is a BASIC question. So you should be reading the help of tools that you want to learn about.
Give it a go and see(*)...or, there's always
doc linspace % when in doubt
(*) altho linspace(2,7,10) may make enlightenment simpler to comprehend.
Thanks for your replies :)

Sign in to comment.

 Accepted Answer

There are two principal ways to create vectors in MATLAB. One is the colon (:) operator, and the other is the linspace function. The principal difference is that with the colon operator, you define the interval between successive elements and let the length of the resulting vector vary, and in linspace, you define the length of the vector and the function calculates the interval to fit the length.

4 Comments

shot in the dark for a reply in 2020 but I'm not understanding the real difference between the two? is one more advantageous? ex:
doesnt
x = [1:1:10]
mean the same thing as:
x = linspace(1,10,10)?
or is there some advantage in memory? im studying finite difference methods and saw some code using linspace and was wondering if it actually had any added benefit. thanks for your time.
For example,
1:0.5:10 %interval
linspace(1,10,19) %total number of values including endpoints
Wanting a certain number of points is pretty common. For example,
linspace(min(x), max(x), 50)
divides the space up into 50 points including endpoints (49 intervals)
"is one more advantageous?"
No, they do two different things. Using two posts I mark out a distance on the ground, then I tell you:
  1. to walk between the posts using exactly 50 identically-sized steps. You would have figure out the distance, divide it up to find the step size (which is unknown in advance). You might need tiny steps or seven-league boots to walk the distance, but you can only take exactly 50 steps.
  2. to walk towards the second post taking steps of a particular length that I specify. You start walking from the first post using the step size that I gave you, and use as many steps are required, stopping when you reach the other post or cannot fit any more steps of that length. You might be able to do it with one step, or two steps, or ... 23 steps, or 1729 steps, or zero steps (unknown in advance).
Are these the same thing? No.
A more visual way to understand is to graph it.
x = linspace(0, 1, 20)
y = sin (2*pi*x)
plot (x, y)
You should see a sine wave.
now use the sample graph.
stem (x, y)
you should see 20 samples. Now try 50 in linspace instead of 20.
x = linspace(0, 1, 50)
y = sin (2*pi*x)
stem = (x,y)
I hope this answers your question :)

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!