Numerical integration of a second-order ordinary differential equation (ODE)

33 views
Skip to first unread message

Brenton

unread,
May 12, 2015, 4:48:10 AM5/12/15
to sage-s...@googlegroups.com
Hi, I'd like to numerically integrate a simple pendulum problem:

y'' = -9.8*cos(y)

y(0)  = 0
y'(0) = 0

I have tried this code (based on the Lotka-Volterra example in the Sage documentation):

from sage.calculus.desolvers import desolve_odeint
y
,yd = var('y,yd')
f    
= [0,-9.8*cos(y)]
v    
= [y,yd]
t    
= srange(0,10,0.1)
ci  
= [0,1]
sol
= desolve_odeint(f,ci,t,v,rtol=1e-3,atol=1e-4,h0=0.1,hmax=1,hmin=1e-4,mxstep=1000)
p2
= line(zip(t,sol[:,0]))
p2
.show()
which gave this output:
Traceback (most recent call last):    sol = desolve_odeint(f,ci,t,v,rtol=1e-3,atol=1e-4,h0=0.1,hmax=1,hmin=1e-4,mxstep=1000)
 
File "", line 1, in <module>
   
 
File "/tmp/tmpowXVlu/___code___.py", line 9, in <module>
    sol
= desolve_odeint(f,ci,t,v,rtol=_sage_const_1en3 ,atol=_sage_const_1en4 ,h0=_sage_const_0p1 ,hmax=_sage_const_1 ,hmin=_sage_const_1en4 ,mxstep=_sage_const_1000 )
 
File "/home/sage/sage-6.6/local/lib/python2.7/site-packages/sage/calculus/desolvers.py", line 1487, in desolve_odeint
    all_vars
.update(set(de.variables()))
 
File "sage/structure/element.pyx", line 430, in sage.structure.element.Element.__getattr__ (build/cythonized/sage/structure/element.c:4625)
 
File "sage/structure/misc.pyx", line 257, in sage.structure.misc.getattr_from_other_class (build/cythonized/sage/structure/misc.c:1772)
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'variables'

Vegard Lima

unread,
May 12, 2015, 5:04:40 AM5/12/15
to sage-s...@googlegroups.com
Hi,

On Tue, May 12, 2015 at 10:48 AM, Brenton <mowan...@bigpond.com> wrote:
> Hi, I'd like to numerically integrate a simple pendulum problem:
>
> y'' = -9.8*cos(y)
> y,yd = var('y,yd')
> f = [0,-9.8*cos(y)]
> v = [y,yd]

I think your f is wrong. Shouldn't it be:
f = [yd, -9.8*cos(y)]


Cheers,
--
Vegard
Reply all
Reply to author
Forward
0 new messages