1

how to setup the following odes with the corresponding initial conditions in python?

x'(t) =x(t) - y(t) - e^t

y'(t) =x(t) + y(t) + 2e^t

with x(0)= -1 and y(0)= -1 and 0 <= t <= 4

The following is what I have so far:

def f(u, t):
    x, y = u
    return [x+y-e**t, x+y+2*e**t]

x0, y0 = [-1.0,-1.0]
t = numpy.linspace( 0,4,50 )
4
  • What are you trying to do? What is the problem? Please be a lot more specific.
    – Blubber
    May 25, 2013 at 8:03
  • Also, you got that e defined somewhere? In any case, you should use math.exp() for that.
    – Blubber
    May 25, 2013 at 8:08
  • 2
    You should use numpy.exp because it's a vectorized version of math.exp (at least in spirit, because numpy.exp is much more efficient ).
    – deufeufeu
    May 25, 2013 at 8:38
  • I don't have any experience with numpy so I'll have to take your word for it :).
    – Blubber
    May 25, 2013 at 8:40

1 Answer 1

1

I guess you're trying to solve them with odeint. First I'm assuming you use this prelude in you script :

import numpy as np
from scipy.integrate import odeint

Your equation is :

def equation(X, t):
    x, y = X
    return [ x+y-np.exp(t), x+y+2*np.exp(t) ]

and then you can solve them with

init = [ -1.0, -1.0 ]
t = np.linpsace(0, 4, 50)
X = odeint(equation, init, t)

You can extract x(t) and y(t) with

x = X[:, 0]
y = X[:, 1]
6
  • The system is already of first-order. You are solving a second order problem in your post. May 25, 2013 at 9:05
  • Your post solves the problem x' = vx; vx' = x+y-exp(t); ... which is equivalent to x'' = x+y-exp(t); .... Hence, you solve a second order problem by rewriting it as first-order equations. This is also apparent from the fact that your function returns a fourth-dimensional vector. May 25, 2013 at 9:33
  • Hi Thanks for your effort.
    – PeAcE
    May 25, 2013 at 9:54
  • 1
    OMG : Does it answers your question?
    – deufeufeu
    May 25, 2013 at 10:38
  • @deufeufeu: There is no mention of scipy in the question, so you should add from scipy.integrate import odeint to your answer. May 25, 2013 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.