[Tutor] hi

Vick vick1975 at orange.mu
Thu Aug 8 19:22:57 CEST 2013


Hi,

Thanks !

What is the best numerical ODE that you have coded then? Can you solve for
the following equation and tell me the error it gives:

N(ODE) = (4 * (exp(1) ** (0.8 * t))) - (0.5 * x)      for     f(t , x)

The true value should be : TF =  (4/1.3)*EXP(0.8*t)-(1.4/1.3)*EXP(-0.5*t)

The error after your numerical ODE should be Er = ABS((TF-N(ODE))/TF)*100

For my n-body problem, I will need to see how to implement the animation API
in my code and see if it works!

Regards
Vick


-----Original Message-----
From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] 
Sent: Thursday, 08 August, 2013 18:14
To: Vick
Cc: tutor at python.org
Subject: Re: [Tutor] hi

On 7 August 2013 22:43, Vick <vick1975 at orange.mu> wrote:
> Hi,
>
> It just plot one line. How to print several lines?

Just do:

line1, = plot([], [])
line2, = plot([], [])

And then

line1.set_data(x1, y1)
line2.set_data(x2, y2)

and so on.

> I've attached your example and I've corrected my own 3d code which 
> works now but it is not an animation. So how can I plot it using the code
you gave me?

Just change the data that you use for the line.

> By the way, I think you know that I'm trying to plot for n-body problem?
> Have you made a python code for the n-body problem?

No I haven't but I have written code for computing numerical solutions of
ODEs.

> Mine works, but I'm just
> having trouble with 3d plotting of the orbits.

Once you have the lists of x, y, and z coordinates for the orbit just call:

plot(x, y, z)

(after creating an axes with projection='3d' and importing mplot3d).

What's not working for you?

> And also I have searched the
> web on how to integrate conservation of energy as you suggested to me, 
> but I couldn't find any article that talks about 1st order ODEs for 
> energy, and the annotation is different from mine.

I think you misunderstood. What I meant was if you implement your own
routine for solving ODEs you need to check its accuracy. In a 2-body problem
you would have an analytic solution in closed form. You can then check the
error between your numerical solution and the analytic one. Investigating
how this error scales with the step size dt enables you to confirm the order
of your integrator. The reason I suggested this was that the original code
you sent implemented the Runge-Kutta algorithm incorrectly and I wanted you
to check your code properly before assuming it works.

In the case of the N-body problem you don't have an analytic solution but,
assuming Hamiltonian dynamics, energy is conserved. You can compute the
energy of your system at each state and compare how constant it is over time
in the output from your integrator. This gives you a proxy for not having an
analytic solution that you can use to check the accuracy of your integrator:
if the energy of your system is not (approximately) maintained during
integration then your integration must be inaccurate.

> PS: Also how do you make the plot run only once without having to do 
> it over and over again?

I'm not sure TBH. I normally use the animation API to save video files for
presentations.

You may be more interested in the approach here rather than using the
animation API:
http://stackoverflow.com/questions/11874767/real-time-plotting-in-while-loop
-with-matplotlib


Oscar



More information about the Tutor mailing list