swifty
swifty

Reputation: 1282

Generate exponentially increasing list of numbers (from start to stop)

I would like to generate a exponentially increasing list of numbers between a start and an end number.

So for example I would to take: start_num = 700,000 end num = 750,000 num_steps = 100

and generate a list of 100 numbers roughly like so:[700000,700100,700250,700700.....746000,750000]

I can do this for evenly spaced intervals with np.arange or even slightly exponentially increasing with np.logspace but the "sharpness" of the increase is not enough with this method.

Ideally I could also control how sharp the increase in distance would be.\\

Thanks for your time.

Upvotes: 0

Views: 1885

Answers (3)

user8354377
user8354377

Reputation:

Try this code:

num=0
numlist=[]
for i in range(700000,750000):
    if num==100:
        break

    if i+num*100>750000:
        num=num/2
    numlist.append(i+num*100)
    num+=1
print(numlist)  

This code dependse of number place and it can increasly generate numbers

Upvotes: 0

FHTMitchell
FHTMitchell

Reputation: 12157

As I said in the comments, your premise is mathematically flawed. Essentially you want, given fixed start, stop and num (num_steps) an equation (this is maths, not python)

y = f(x) = Ab^x

with "boundary conditions"

f(0) = start
f(num) = stop

Now this is relatively easy to solve for the only two constants -- the coefficient A and the base b:

f(0) = Ab^0 = A => A = start

f(num) = start * b^(num) => b = (stop / start) ^ (1 / num)

So if we chose a start, stop and num we cal solve for all constants in our equation y. There is nothing to vary, so you must always get the same result.

This can be seen by doing:

import numpy as np
import math

for b in range(2, 6):
    # np.logspace takes start and stop as the exponent of base
    # so use math.log for consistency -- same as np.geomspace
    print(np.logspace(math.log(2, b), math.log(10, b), num=5, base=b))

Outputs

[ 2.          2.99069756  4.47213595  6.68740305 10.        ]
[ 2.          2.99069756  4.47213595  6.68740305 10.        ]
[ 2.          2.99069756  4.47213595  6.68740305 10.        ]
[ 2.          2.99069756  4.47213595  6.68740305 10.        ]

Edit:

n.b. the equation given is the general exponential equation -- any equation of the form y = ab^(kx + c) can be rewritten as y = (ab^c)(b^k)^x = AB^x.

Note that this does not include the non-exponential equation y = C + Ab^x (see comments). If you want to use this version with a variable base, then this function will do the trick

def varlogspace(start, stop, num, base, **kwargs):
    n = num - 1
    c = (start - stop) / (1 - base**n)
    a = start - c
    def f(x):
        return a + (c * base**x)
    return np.fromfunction(f, (num,), **kwargs)


for b in range(2, 6):
    print(varlogspace(2, 5, 5, base=b))

outputs (see that increasing base increases the curvature of the curve)

[2.  2.2 2.6 3.4 5. ]
[2.    2.075 2.3   2.975 5.   ]
[2.         2.03529412 2.17647059 2.74117647 5.        ]
[2.         2.01923077 2.11538462 2.59615385 5.        ]

Upvotes: 3

ikuamike
ikuamike

Reputation: 335

start = 0
stop = 100
factor = 2
step = 0
numlist = []

while start < stop:
    numlist.append(start)
    step += factor
    start += step    

print(numlist)

# Output
[0, 2, 6, 12, 20, 30, 42, 56, 72, 90]

I made this simple code that would increase the step as per the factor you chose like in this case it is step increases by 2 as it goes to the end. Unfortunately you have to sacrifice landing exactly on stop if you want if you want to control the factor. Hope it helps in solving your problem.

Upvotes: -1

Related Questions