Ask Your Question
3

Numeric multivariable ode solver in Sage?

asked 2010-08-19 19:51:31 +0100

ngativ gravatar image

updated 2011-05-05 15:36:10 +0100

Kelvin Li gravatar image

hi everyone, is there any numeric multivariable ode solver in sage? i want to solve the double pendulum problem, so i need to solve 4 first order differential equations which deppends on theta_1(t) amd thetha_2(t). I need something like a multivariable runge kutta algorithm

edit retag flag offensive close merge delete

Comments

You should also ask on ask.scipy.org (but ask about scipy - a component of sage - instead of sage). Post a link here.

William Stein gravatar imageWilliam Stein ( 2010-08-19 20:22:26 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted
2

answered 2011-03-03 07:57:40 +0100

Joaquim Puig gravatar image

Hi,

Another option is to use the function desolve_odeint (from version 4.6 on, see the docs on desolve_odeint?), which uses the odeint solver internally, but takes as an input symbolic funcions . For instance, to integrate the Lorenz attractor, you would do

 sage: x,y,z=var('x,y,z') # Declare the variables 
 sage: lorenz=[10.0*(y-x),x*(28.0-z)-y,x*y-(8.0/3.0)*z]
 sage: times=srange(0,50.05,0.05) # Integration Time 
 sage: ics=[0,1,1] # and initial conditions
 sage: sol=desolve_odeint(lorenz,ics,times,[x,y,z]) #integrate

to plot the attractor, simply:

sage: line3d(sol)

I do not know what you mean exactly by the double pendulum, but here it is something similar:

sage: theta1,theta2,x1,x2=var('theta1,theta2,x1,x2')
sage: dpendulum=[x1,x2,-sin(theta1)-0.1*sin(theta2),-sin(theta2)+0.1*sin(theta1)]
sage: # Time and initial conditions
sage: times=srange(0,50.05,0.05) 
sage: ics=[1,1,0,0] 
sage: sol=desolve_odeint(dpendulum,ics,times,[theta1,theta2,x1,x2])

and a nice plot is here

sage: graphics_array([line(zip(sol[:,0],sol[:,2])),line(zip(sol[:,1],sol[:,3]))])

Joaquim Puig

edit flag offensive delete link more
2

answered 2010-08-19 20:43:14 +0100

Most of Sage's numerics is done via Numpy/Scipy which is included in Sage and usable from the Sage Notebook. To solve an ODE using Scipy first reduce your problem to a system of 1st degree ODEs: $u'(x,y,t) = f(t,x,y,u)$. Then, follow the instructions on the relevant Scipy documentation: Ordinary differential equations (odeint). The key components to solving the numerical ODE is (1) writing a Python function for the right-hand side, $f$, (2) writing a function for its Jacobian, $J_f$, (3) and calling the scipy.integrate.odeint function.

Consult the scipy.integrate.odeint documentation for more information on its use. You can read this documentation by entering

sage: from scipy.integrate import odeint
sage: odeint?

Finally, I providing the Jacobian for your problem is an optional argument but it helps with convergence.

edit flag offensive delete link more

Comments

Thanks!, but.. it works for a multivariable diferential equation system like $u'(x,y,t) = f(t,x,y,u)$. ? i only see that odeint only works for du/dt=f(u,t) or im wrong?

ngativ gravatar imagengativ ( 2010-08-19 22:03:17 +0100 )edit

**odeint** allows passing of arguments. These arguments are supposed to match those of $f$ and $J_f$ in the same order. For example, if $f$ and $J_f$ depend on a parameter, $\alpha$, then just include $\alpha$ in the **args** parameter of **odeint**. Spatial vecotrs $x$ and $y$ can behave similarly.

cswiercz gravatar imagecswiercz ( 2010-08-20 00:11:15 +0100 )edit
1

answered 2010-08-20 01:46:13 +0100

ngativ gravatar image

i make it! using desolve_system_rk4[w1,w2,aw1,aw2],[s1,s2,w1,w2],ics=[0,0.5,0,0.0,0.5],ivar=t,step=0.01,end_points=20)

I compared these results with the results from a maple worksheet that i downloaded from internet and they match very well. The Sage's speed is very good

But i would like to learn how to use odeint from scipy and ode_solver from gsl to solve this system :S

I just started to using Sage 3 days ago and i love it. Thanks!

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

1 follower

Stats

Asked: 2010-08-19 19:51:31 +0100

Seen: 2,545 times

Last updated: Mar 03 '11