linspace

linspace(start, stop, n=100)

Construct a range of n linearly spaced elements from start to stop.

Examples

julia> linspace(0,2*pi,10)
10-element Array{Float64,1}:
 0.0     
 0.698132
 1.39626 
 2.0944  
 2.79253 
 3.49066 
 4.18879 
 4.88692 
 5.58505 
 6.28319 
julia> linspace(0,20,4)
4-element Array{Float64,1}:
 0.0
 6.66667
 13.3333
 20.0
  1. Generate a range of linearly spaced values:

    julia> linspace(0, 1, 5)
    5-element LinRange{Float64}:
    0.0,0.25,0.5,0.75,1.0

    This example generates a range of 5 linearly spaced values from 0 to 1.

  2. Generate a range with a different number of elements:

    julia> linspace(1, 10, 3)
    3-element LinRange{Float64}:
    1.0,5.5,10.0

    It creates a range with 3 linearly spaced values from 1 to 10.

  3. Use linspace with negative values:
    julia> linspace(-2, 2, 4)
    4-element LinRange{Float64}:
    -2.0,-0.6666666666666666,0.6666666666666666,2.0

    It generates a range of 4 linearly spaced values from -2 to 2.

Common mistake example:

julia> linspace(10, 1, 5)
ERROR: DomainError with -4.0: "start > stop and step < 0 or start < stop and step > 0"

In this example, the start value is greater than the stop value, which is not allowed in linspace. Always ensure that the start value is less than the stop value to avoid this error.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: