Python function in ODE not producing needed results

I am using the dirac_delta function in an ODE, and since it is treated like a Python function, it is not evaluated as I would like in the ODE solver. I think this is a matter of having a Python function when I need a symbolic function, but I cannot figure out how to get around it. My code is below. Any ideas?

var('A p y t s v')
A=3
p=2
alpha = 1
beta = -1/sqrt(3)
omega = 1
F(t) = [SR(1),v,-omega^2*y+A*dirac_delta(t-p)]
ans=desolve_odeint(F(t),[0,alpha,beta],srange(0,5,0.1),[s,y,v],ivar=t)
ty=[[a,a] for a in ans]
tv=[[a,a] for a in ans]
line(ty)+line(tv,color='red')
edit retag close merge delete

I think you are right. It's possible that Maxima would have an implementation of Dirac delta that you could use directly within Maxima, though.

Is there a way to use the ode solver with a python function in the vector field? I think I've done this before but what I tried in the past does not seem to work.

Sort by » oldest newest most voted

In the end, I used ode_solver and an approximation of the delta function to get things working. Here is the code:

@interact(layout=[['A','p','interval'],['alpha','beta','omega']])
def lab(A=input_box(2,label='A',width=15),p=input_box(2,label='p',width=15),alpha = input_box(1,label='alpha',width=15),beta = input_box(1/sqrt(3),label='beta',width=15),omega = input_box(1,label='omega',width=15),interval=input_box((0,6),label='interval',width=20) ):
def f(q):
w=0.001
if q>p-w/2 and q<p+w/2:
return(1/w)
else:
return 0

def dy(t,y):
return [ 1, y, -omega^2*y+A*f(y) ]

T=ode_solver()
T.function=dy
T.ode_solve(y_0=[0,alpha,beta],t_span=interval,num_points=500)
ans=T.solution
ans=[a for a in ans]
ty=[[a,a] for a in ans]
tv=[[a,a] for a in ans]
p=line(ty,legend_label='y (position)')+line(tv,color='red',legend_label='v (velocity)')
more