Gradual increasing of step size for a range of values

Hi, I want to create an array of values ranging from 1 to 5000. For this, I use LinRange(0, 5000, 200). However, for my purpose, I need more points for the lower regime compared to the higher ones. For this I tried,

params_1 = LinRange(0, 100, 100)
params_2 = LinRange(101, 5000, 100)
params = vcat(params_1, params_2)

But while plotting this showcases a clear discontinuity, which I want to avoid. So I was wondering if there was any better way to get gradually increase the step size in this interval?

You could use an appropriate pair of inverse functions to do the smooth mapping.

A simple example using square root and power of 2:

using Plots
n = 100
x1, x2 = 0.0, 5000.0
x = LinRange(√x1, √x2, n).^2
plot(scatter(x), scatter(diff(x)))
2 Likes

Here is another approach, where the range between x1 and x2 has a variable step increasing linearly between dx1 and dx2:

# x1, x2 are respected; while dx1, dx2 approximately so:
function vrange(x1,x2,dx1,dx2)
    n = round(Int, 2*(x2 - x1)/(dx1 + dx2))
    dx = LinRange(dx1,dx2,n-1)
    x = cumsum([x1; dx])
    return (x2 - x1)/(x[end] - x[1])*(x .- x1) .+ x1
end

using Plots
x1, x2 = 0, 5000
dx1, dx2 = 1, 50
x = vrange(x1,x2,dx1,dx2)
plot(scatter(x), scatter(diff(x)), ms=3)

The range function gives MethodError, but LinRange works. I appreciate this simple approach though. Thank you!

Sorry, the function mean() required using Statistics. This was not necessary and I modified the code above.

Actually, I was referring to your first approach above, the one with a slowly varying function. The range function is giving an error there.

1 Like

Which Julia version are you using? It was working here for 1.7.2:

julia> versioninfo()
Julia Version 1.7.2

In any case, edited the code.

I see. I have 1.6.3, that’s why.

By the way, any quick way to update on Windows?

It is recommended to use the Windows Store and run Juliaup.

There is also UpdateJulia, which looks good but did not try it because using the former.

1 Like

Yes, I am actually trying that, because I am not sure, but it seems for using Juliaup, I need to reinstall Julia from the Microsoft store, which seems cumbersome.

1 Like