ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 30 Oct 2015 02:45:33 +0100Python function in ODE not producing needed resultshttps://ask.sagemath.org/question/30343/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[0],a[1]] for a in ans]
tv=[[a[0],a[2]] for a in ans]
line(ty)+line(tv,color='red') Thu, 29 Oct 2015 16:22:05 +0100https://ask.sagemath.org/question/30343/python-function-in-ode-not-producing-needed-results/Comment by kcrisman for <p>I am using the <code>dirac_delta</code> 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?</p>
<pre><code>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[0],a[1]] for a in ans]
tv=[[a[0],a[2]] for a in ans]
line(ty)+line(tv,color='red')
</code></pre>
https://ask.sagemath.org/question/30343/python-function-in-ode-not-producing-needed-results/?comment=30354#post-id-30354I think you are right. It's possible that Maxima would have an implementation of Dirac delta that you could use directly within Maxima, though.Thu, 29 Oct 2015 20:31:20 +0100https://ask.sagemath.org/question/30343/python-function-in-ode-not-producing-needed-results/?comment=30354#post-id-30354Comment by calc314 for <p>I am using the <code>dirac_delta</code> 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?</p>
<pre><code>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[0],a[1]] for a in ans]
tv=[[a[0],a[2]] for a in ans]
line(ty)+line(tv,color='red')
</code></pre>
https://ask.sagemath.org/question/30343/python-function-in-ode-not-producing-needed-results/?comment=30357#post-id-30357Is 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.Thu, 29 Oct 2015 21:28:53 +0100https://ask.sagemath.org/question/30343/python-function-in-ode-not-producing-needed-results/?comment=30357#post-id-30357Answer by calc314 for <p>I am using the <code>dirac_delta</code> 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?</p>
<pre><code>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[0],a[1]] for a in ans]
tv=[[a[0],a[2]] for a in ans]
line(ty)+line(tv,color='red')
</code></pre>
https://ask.sagemath.org/question/30343/python-function-in-ode-not-producing-needed-results/?answer=30359#post-id-30359 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[2], -omega^2*y[1]+A*f(y[0]) ]
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[1] for a in ans]
ty=[[a[0],a[1]] for a in ans]
tv=[[a[0],a[2]] for a in ans]
p=line(ty,legend_label='y (position)')+line(tv,color='red',legend_label='v (velocity)')Fri, 30 Oct 2015 02:45:33 +0100https://ask.sagemath.org/question/30343/python-function-in-ode-not-producing-needed-results/?answer=30359#post-id-30359