Ask Your Question
1

desolve_rk4 cannot handle a piecewise function

asked 2014-02-20 18:26:00 +0100

Groenendael72 gravatar image

updated 2014-02-20 18:31:21 +0100

I am trying to get a numeric solution to a differential equation involving a function that is piecewise (actually defined using an if/else statement). I successfully generated a slopefield using the lambda function, but desolve_rk4 seemingly cannot cope with this syntax. It seems to me that a numeric solver should be able to handle a function defined this way, but perhaps I am just doing something wrong?

Code:

var('t')
def f(t):
    if t<5:
        return 3
    else:
        return 0
V=function('V',t)
desolve_rk4(lambda t,V: ((f)-V)/(2.3*1.2),V, ics=(0,0),ivar=t)
edit retag flag offensive close merge delete

Comments

Short answer is that Maxima doesn't know what to do with such a Python lambda function, and this uses a Maxima functionality.

kcrisman gravatar imagekcrisman ( 2014-02-20 20:31:21 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-03-26 23:34:01 +0100

calc314 gravatar image

You can do this using desolve_odeint. I'm not sure why it's not working in desolve_rk4. Try this:

var('t s V')
def f(t):
    if t<5:
        return 3
    else:
        return 0
def rhs(V,s,t):
    return ((f(t)-V)/(2.3*1.2))
F=[SR(1),rhs]
tlist=srange(0,10,0.1)
ans=desolve_odeint(F,[0,10],tlist,[s,V],ivar=t)
line(ans)
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: 2014-02-20 18:26:00 +0100

Seen: 419 times

Last updated: Mar 26 '14