Ask Your Question
1

Variable range()

asked 2010-10-16 08:57:24 -0500

Lynda gravatar image

Hello, I have the following code:

def f(a,b,c,x):
    xtemp=a
    for i in range(b,x):
        xtemp=xtemp-xtemp*c
    return xtemp   
var('t')
plot(f(10000000,1,0.01,t),3,20)

and got the error

TypeError: range() integer end argument expected, got
sage.symbolic.expression.Expression.

I understand that range does nor support 'var' object, but how can I have a variable range in this kind of function?

edit retag flag offensive close merge delete

Comments

Thank you Niles it works well for my needs

Lynda gravatar imageLynda ( 2010-10-17 08:39:14 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2010-10-16 09:10:10 -0500

niles gravatar image

A lambda function might be the quickest workaround:

plot(lambda t: f(10000000,1,0.01,t),3,20)

And if you don't want to get the deprecation warning about range not taking float inputs, you could modify your function to:

def f(a,b,c,x):
    xtemp=a
    for i in range(b,ceil(x)):
        xtemp=xtemp-xtemp*c
    return xtemp
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2010-10-16 08:57:24 -0500

Seen: 239 times

Last updated: Oct 16 '10