[SciPy-user] integrate.ode 2 dimensional simple harmonic oscillator code
Bastian Weber
bastian.weber@gmx-topmail...
Mon Jan 19 11:20:46 CST 2009
Hello Scott,
> Do ode and odeint work in multiple dimensions?
of course they do. Here is an example of a 2-dim problem (the 1-D
harmonic oscillator, which is a _second_ order system), I wrote some
time ago.
#!/usr/bin/python
# -*- coding: utf8 -*-
from scipy import *
from scipy.integrate import odeint
import pylab
#parameters
delta=0.1 # damping
omega_2=100 # means omega**2
t=r_[0:100:.01]# time vector
x0=[0,10] # initial state
def rhs(x,t):
"""
right hand side of the statespace equation
in this cas two-dimensional
"""
x1_dot=x[1]
x2_dot=-(2*delta*x[1]+omega_2*x[0])
return [x1_dot, x2_dot]
x=odeint(rhs, x0, t,)
print shape(x)
x1=x[:,0]
x2=x[:,1]
pylab.plot(t, x1, "k-")
pylab.show()
Scott Askey schrieb:
>
> I could not any examples with more than one degree of freedom. And
> from the doc string it how to solve simultaneous ode's was not obvious.
> The code for modelling a 2d simple harmonic oscillator or spherical
> pendulum would give me the insight I need.
>
> I found and understand the following 1 D harmonic oscillator model
from the scipy cookbook.
>
> V/R
>
> Scott
>
Reading your message again now, I get the impression, the code above
might not fit your needs..
However, what you have to do is to get your system of interest in a
*state space* representation, i. e. writing down a system of first order
differential equations. In the case of 2-D oscillator your x array would
consist of 4 elements and rhs would return the time derivate of these 4
magnitudes, i. e. also an array of length 4.
Regards,
Bastian.
More information about the SciPy-user
mailing list